private async Task <IEnumerable <AuditEventDescriptor> > GetEventsAsync(AuditSearchRequest request) { QueryContainer query = new QueryContainer(); if (!string.IsNullOrWhiteSpace(request.EventType)) { query &= new MatchPhraseQuery { Field = "eventType", Query = request.EventType }; } if (request.StartDate.HasValue || request.EndDate.HasValue) { query &= new DateRangeQuery { GreaterThanOrEqualTo = request.StartDate, LessThanOrEqualTo = request.EndDate }; } var response = await _elasticClient.SearchAsync <AuditEventIntercept>(s => s .Index("demoapi*") .From(request.From) .Size(request.Size) .Query(q => q .Bool(b => b .Filter(f => f .MatchAll() && query)))); return(response.Hits.Select(MapHit)); }
public static MatchPhraseQuery BuildMatchPhraseQuery(Aliyun.TableStore.DataModel.Search.Query.MatchPhraseQuery query) { MatchPhraseQuery.Builder builder = MatchPhraseQuery.CreateBuilder(); builder.SetFieldName(query.FieldName); builder.SetText(query.Text); return(builder.Build()); }
/// <summary> /// Search by Match Phrase Query /// all the terms must appear in the field /// they must have the same order as the input value /// </summary> /// <typeparam name="T"></typeparam> /// <param name="matchPhraseQuery"></param> /// <param name="indexName"></param> /// <param name="typeName"></param> /// <param name="field"></param> /// <param name="total"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public IEnumerable <T> SearchByMatchPhrase <T>(string matchPhraseQuery, string indexName, string typeName, string field, out long total, int pageIndex = 0, int pageSize = 100) where T : class { total = 0; if (!string.IsNullOrEmpty(indexName)) { indexName = indexName.ToLower(); } if (!string.IsNullOrEmpty(typeName)) { typeName = typeName.ToLower(); } var indices = Indices.Index(indexName); var types = Types.Type(typeName); var request = new SearchRequest(indices, types) { From = pageIndex, Size = pageSize, }; if (!string.IsNullOrEmpty(matchPhraseQuery)) { var queryString = new MatchPhraseQuery() { Name = field, Query = matchPhraseQuery, }; request.Query = new QueryContainer(queryString); } var sort = new List <ISort> { new SortField() { Field = new Field() { Name = field }, Order = SortOrder.Descending } }; request.Sort = sort; ISearchResponse <T> result = _client.Search <T>(request); if (result != null) { total = result.Total; return(result.Documents); } return(new List <T>()); }
public void Export_Omits_Field_If_Not_Provided() { var query = new MatchPhraseQuery("phrase"); var expected = JsonConvert.SerializeObject(new { match_phrase = "phrase", }, Formatting.None); Assert.Equal(expected, query.Export().ToString(Formatting.None)); }
public IEnumerable <HomeEvent> GetEventsWindow(DateTime endDateTime) { var match = new MatchPhraseQuery { Field = new Field("doc"), Query = "event" }; var rangeQuery = new DateRangeQuery { Field = "@timestamp", LessThanOrEqualTo = endDateTime.ToUniversalTime(), GreaterThanOrEqualTo = endDateTime.ToUniversalTime().AddSeconds(-config.EventsWindowsSeconds), }; var boolQuery = new BoolQuery { Must = new List <QueryContainer> { match, rangeQuery } }; var searchRequest = new SearchRequest(Indices.Parse("history-*")) { Query = boolQuery, Sort = new List <ISort>() { new SortField { Field = new Field("@timestamp"), Order = SortOrder.Descending } } }; var result = elastic .Request <ElasticSearchEvent>(searchRequest) .Select(ConvertToLocalDateTime) .ToList(); result.ForEach(r => log.Debug(r.ToString())); return(result.Select(r => new HomeEvent { DateTime = r.timestamp, Id = r.Id, Sensor = r.sensor.display, Status = r.status, SensorType = r.sensor.type, })); }
public void Export_Returns_Valid_Json() { var query = new MatchPhraseQuery("phrase") .Field("field"); var expected = JsonConvert.SerializeObject(new { match_phrase = "phrase", field = "field" }, Formatting.None); Assert.Equal(expected, query.Export().ToString(Formatting.None)); }
public void Match_phrase_query_must_transform_correclty_to_ES() { var query = new MatchPhraseQuery("headline", "Yuri Metelkin"); Assert.IsTrue(query.Field == "headline"); Assert.IsTrue(query.Value.ToString() == "Yuri Metelkin"); string json = query.ToString(); var jo = JsonObject.Parse(json); var q = jo.ToQuery(); Assert.IsTrue(q.Type == QueryType.MatchPhraseQuery); query = q as MatchPhraseQuery; Assert.IsTrue(query.Field == "headline"); Assert.IsTrue(query.Value.ToString() == "Yuri Metelkin"); }
/// <summary> /// 搜索文档 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="size"></param> /// <param name="queryWord"></param> /// <param name="accountId"></param> /// <param name="masterId"></param> /// <returns></returns> public IEnumerable <T> Search <T>(int size, string queryWord, long accountId = 0, long masterId = 0) where T : class { var matchPhraseQuery = new MatchPhraseQuery() { Field = "_all", Query = queryWord.ToLower(), Slop = 0, Analyzer = "charSplit", MaxExpansions = 1 }; var termQuery = new TermQuery(); if (masterId != 0) { //全部分店 termQuery.Field = "master_id"; termQuery.Value = masterId; } else { //限制店铺 termQuery.Field = "account_id"; termQuery.Value = accountId; } var queryContainer = new QueryContainer[] { termQuery, matchPhraseQuery }; return(_client.Search <T>(s => s .Type(_typeName) .Index(_indexName) .Size(size) .Query(q => q .Bool(b => b .Must(queryContainer)) ) .Sort(sort => sort.Ascending("_score")) ).Documents); }
public static QueryContainer QueryBuilder(JObject json) { List <QueryContainer> rules = new List <QueryContainer>(); foreach (JObject obj in json["rules"]) { var field = obj["field"]; if (field != null) { var match_phrase_query = new MatchPhraseQuery { Field = field.ToString(), Query = obj["value"].ToString(), Slop = 10 }; rules.Add(match_phrase_query); } else { rules.Add(QueryBuilder(obj)); } } var condition = json["condition"].ToString(); BoolQuery bool_query; if (condition == "AND") { bool_query = new BoolQuery { Must = rules }; } else { bool_query = new BoolQuery { Should = rules, MinimumShouldMatch = 1 }; } return(new QueryContainer(bool_query)); }
private QueryContainer CreateMustClause(string recipeName) { var MustContainer = new QueryContainer(); var wildIngredient = new WildcardQuery { Field = "ingredients.IngredientName", Value = $"*{recipeName.ToLower()}*" }; MustContainer |= wildIngredient; var fuzzyIngredient = new FuzzyQuery { Field = "ingredients.IngredientName", Value = $"{recipeName.ToLower()}" }; MustContainer |= fuzzyIngredient; var wildRecipe = new WildcardQuery { Field = "name", Value = $"*{recipeName.ToLower()}*" }; MustContainer |= wildRecipe; var fuzzyRecipe = new FuzzyQuery { Field = "name", Value = $"{recipeName.ToLower()}" }; MustContainer |= fuzzyRecipe; var match = new MatchPhraseQuery { Field = "name", Query = recipeName }; MustContainer |= match; return(MustContainer); }
/// <summary> /// Gets the elastic query for the given feeds. /// </summary> /// <param name="relevantFeeds">The relevant feeds to determine their query.</param> /// <param name="blackList">The blacklist to not violate.</param> /// <returns>The query for the feed.</returns> private static QueryContainer GetFeedQuery(IEnumerable <Feed> relevantFeeds, IEnumerable <string> blackList) { QueryContainer finalContainer = null; if (relevantFeeds == null || !relevantFeeds.Any()) { throw new InvalidOperationException(nameof(relevantFeeds)); } blackList = blackList?.Select(x => x.ToLowerInvariant()).ToList(); foreach (var feed in relevantFeeds) { var keywordList = feed.Filter.Keywords.Select(x => x.ToLowerInvariant()).ToList(); QueryBase basicTermsOperator; if (blackList != null && blackList.Any()) { basicTermsOperator = !(new TermsQuery { Field = "ArticleTitle", Terms = blackList } || new TermsQuery { Field = "ArticleLongText", Terms = blackList }) && (new TermsQuery { Field = "ArticleTitle", Terms = keywordList } || new TermsQuery { Field = "ArticleLongText", Terms = keywordList }); } else { basicTermsOperator = new TermsQuery { Field = "ArticleTitle", Terms = keywordList } || new TermsQuery { Field = "ArticleLongText", Terms = keywordList }; } foreach (var guid in feed.Sources.Select(x => x.Id)) { finalContainer |= new MatchPhraseQuery { Field = "IndexingSourceId", Query = (guid.ToString()) } && basicTermsOperator; } } return(finalContainer); }
public static QueryBase GetDefaultQuery(this TermNode node, IQueryVisitorContext context) { if (!(context is IElasticQueryVisitorContext elasticContext)) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } QueryBase query; string nodeFullName = node.GetFullName(); if (elasticContext.IsPropertyAnalyzed(nodeFullName)) { var fields = !String.IsNullOrEmpty(nodeFullName) ? new[] { nodeFullName } : elasticContext.DefaultFields; if (!node.IsQuotedTerm && node.UnescapedTerm.EndsWith("*")) { query = new QueryStringQuery { Fields = fields, AllowLeadingWildcard = false, AnalyzeWildcard = true, Query = node.UnescapedTerm }; } else { if (fields.Length == 1) { if (node.IsQuotedTerm) { query = new MatchPhraseQuery { Field = fields[0], Query = node.UnescapedTerm }; } else { query = new MatchQuery { Field = fields[0], Query = node.UnescapedTerm }; } } else { query = new MultiMatchQuery { Fields = fields, Query = node.UnescapedTerm }; if (node.IsQuotedTerm) { (query as MultiMatchQuery).Type = TextQueryType.Phrase; } } } } else { query = new TermQuery { Field = nodeFullName, Value = node.UnescapedTerm }; } return(query); }
public void Boost_WhenBoostIsLessThanZero_ThrowsArgumentOutOfRangeException() { var query = new MatchPhraseQuery("phrase"); Assert.Throws <ArgumentOutOfRangeException>(() => query.Boost(-.1)); }
public void Boost_ReturnsMatchPhraseQuery() { var query = new MatchPhraseQuery("phrase").Boost(2.2); Assert.IsInstanceOf <MatchPhraseQuery> (query); }
public override QueryContainer GetQuery(QueryContainer query) { switch (ExpressOperator) { case ExpressOperator.Eq: query = new TermQuery { Field = PropertyName, Value = Value }; break; case ExpressOperator.Gt: query = new TermRangeQuery { Field = PropertyName, GreaterThan = Value.ToString() }; break; case ExpressOperator.Ge: query = new TermRangeQuery { Field = PropertyName, GreaterThanOrEqualTo = Value.ToString() }; break; case ExpressOperator.Lt: query = new TermRangeQuery { Field = PropertyName, LessThan = Value.ToString() }; break; case ExpressOperator.Le: query = new TermRangeQuery { Field = PropertyName, LessThanOrEqualTo = Value.ToString() }; break; case ExpressOperator.Like: query = new MatchPhraseQuery { Field = PropertyName, Query = Value.ToString() }; break; case ExpressOperator.In: query = new TermsQuery { Field = PropertyName, Terms = (List <object>)Value }; break; default: throw new ElasticsearchException("构建Elasticsearch查询谓词异常"); } return(query); }
/// <summary> /// 构建查询条件 /// </summary> /// <param name="fields"></param> /// <returns>{item1:must,item2:should}</returns> public ESConditionModel BuildQueryContainer(List <ESField> fields, string analyzer) { List <QueryContainer> must = new List <QueryContainer>(), should = new List <QueryContainer>(), mustNot = new List <QueryContainer>(); if (fields != null && fields.Count > 0) { foreach (var item in fields) { QueryContainer container = null; //1.数据类型辨析(暂时不做实现) if (item.DataType == ESDataType.GeoPoint) { //switch (item.QueryType) //{ // case ESQueryType.Geo_Distance: // container = new GeoDistanceQuery() // { // Location = new GeoLocation(0.1, 0.2), // Distance =new Distance(), // }; // break; // case ESQueryType.Geo_Polygon: // container = new GeoPolygonQuery(){ // Points=, // }; // break; // case ESQueryType.Geo_Bounding_Box: // container = new GeoBoundingBoxQuery() // { // BoundingBox = new BoundingBox() // { // BottomRight =, // TopLeft =, // } // }; // break; //} } else { switch (item.QueryType) { case ESQueryType.Match: container = new MatchQuery() { Analyzer = analyzer, Field = item.Key, Query = item.Value.Item1.ToString(), }; break; case ESQueryType.All: container = new MatchAllQuery() { }; break; case ESQueryType.Match_Phrase: container = new MatchPhraseQuery() { Field = item.Key, Analyzer = analyzer, Query = item.Value.Item1.ToString() }; break; case ESQueryType.Match_Phrase_Prefix: container = new MatchPhrasePrefixQuery() { Field = item.Key, Analyzer = analyzer, Query = item.Value.ToString() }; break; case ESQueryType.Term: container = new TermQuery() { Field = item.Key, Value = item.Value.Item1 }; break; case ESQueryType.Range: switch (item.DataType) { case ESDataType.Text: break; case ESDataType.Number: container = new NumericRangeQuery() { Field = item.Key, GreaterThanOrEqualTo = Convert.ToDouble(item.Value.Item1), LessThanOrEqualTo = Convert.ToDouble(item.Value.Item2), }; break; case ESDataType.Date: container = new DateRangeQuery() { Field = item.Key, GreaterThanOrEqualTo = DateMath.FromString(item.Value.Item1.ToString()), LessThanOrEqualTo = DateMath.FromString(item.Value.Item2.ToString()) }; break; default: break; } break; } } //2.条件类型解析 if (container != null) { switch (item.ConditionType) { case ESConditionType.Must: must.Add(container); break; case ESConditionType.Should: should.Add(container); break; case ESConditionType.MustNot: mustNot.Add(container); break; default: break; } } } } return(new ESConditionModel() { Must = must, Should = should, MustNot = mustNot }); }
public QueryContainer GetQuery() { QueryContainer query; switch (Operator) { case Operator.Eq: query = new TermQuery { Field = Infer.Field(PropertyInfo), Value = Value }; break; case Operator.Like: query = new MatchPhraseQuery { Field = Infer.Field(PropertyInfo), Query = Value.ToString() }; break; case Operator.Gt: query = new TermRangeQuery { Field = Infer.Field(PropertyInfo), GreaterThan = Value.ToString() }; break; case Operator.Gte: query = new TermRangeQuery { Field = Infer.Field(PropertyInfo), GreaterThanOrEqualTo = Value.ToString() }; break; case Operator.Lt: query = new TermRangeQuery { Field = Infer.Field(PropertyInfo), LessThan = Value.ToString() }; break; case Operator.Lte: query = new TermRangeQuery { Field = Infer.Field(PropertyInfo), LessThanOrEqualTo = Value.ToString() }; break; case Operator.In: query = new TermsQuery { Field = PropertyName, Terms = (List <object>)Value }; break; default: throw new ArgumentOutOfRangeException(); } return(query); }
public static QueryBase GetDefaultQuery(this TermNode node, IQueryVisitorContext context) { if (!(context is IElasticQueryVisitorContext elasticContext)) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } QueryBase query; string field = node.Field; var defaultFields = node.GetDefaultFields(elasticContext.DefaultFields); if (field == null && defaultFields != null && defaultFields.Length == 1) { field = defaultFields[0]; } if (elasticContext.MappingResolver.IsPropertyAnalyzed(field)) { var fields = !String.IsNullOrEmpty(field) ? new[] { field } : defaultFields; if (!node.IsQuotedTerm && node.UnescapedTerm.EndsWith("*")) { query = new QueryStringQuery { Fields = fields, AllowLeadingWildcard = false, AnalyzeWildcard = true, Query = node.UnescapedTerm }; } else { if (fields != null && fields.Length == 1) { if (node.IsQuotedTerm) { query = new MatchPhraseQuery { Field = fields[0], Query = node.UnescapedTerm }; } else { query = new MatchQuery { Field = fields[0], Query = node.UnescapedTerm }; } } else { query = new MultiMatchQuery { Fields = fields, Query = node.UnescapedTerm }; if (node.IsQuotedTerm) { ((MultiMatchQuery)query).Type = TextQueryType.Phrase; } } } } else { if (!node.IsQuotedTerm && node.UnescapedTerm.EndsWith("*")) { query = new PrefixQuery { Field = field, Value = node.UnescapedTerm.TrimEnd('*') }; } else { query = new TermQuery { Field = field, Value = node.UnescapedTerm }; } } return(query); }
internal static QueryContainer QueryItem(Field field, EsValue queryValue, EnQueryType queryType, Field nestedField = null, double?boost = null) { //extract string parts between "" and use PhraseMatch bool exactMatch = false; if (queryValue.Value is string && queryType == EnQueryType.Match) { exactMatch = ((string)queryValue.Value).Contains("\""); } if (exactMatch) { var tokens = Parser.SplitQuotes(((string)queryValue.Value), new char[] { ' ' }); QueryContainer container = null; foreach (var token in tokens) { QueryContainer query; if (token.HasQuotes) { query = new MatchPhraseQuery { Field = field, Query = token.Token, Boost = boost }; } else { if (token.Token.Contains("*")) { return(new WildcardQuery { Field = field, Value = token.Token, Boost = boost }); } else { query = new MatchQuery { Field = field, Query = token.Token, Fuzziness = Fuzziness.Auto, Boost = boost }; } } if (container is null) { container = query; } else { container |= query; } } return(container); } if (nestedField != null) { return(new NestedQuery { Path = field, Query = SimpleQueryItem(nestedField, queryValue, queryType, boost) }); } else { return(SimpleQueryItem(field, queryValue, queryType, boost)); } }