public static FunctionScoreQuery BuildFunctionScoreQuery(Aliyun.TableStore.DataModel.Search.Query.FunctionScoreQuery query) { FunctionScoreQuery.Builder builder = FunctionScoreQuery.CreateBuilder(); builder.SetQuery(SearchQueryBuilder.BuildQuery(query.Query)); builder.SetFieldValueFactor(BuildFieldValueFactor(query.FieldValueFactor)); return(builder.Build()); }
public void TestBoostFactor_ObjectInitializer_WrongProperty() { IFunctionScoreFunction boost = new BoostFactorFunction <ElasticsearchProject>(2); boost.Weight = 1; boost.Filter = new TermFilter { Field = Property.Path <ElasticsearchProject>(p => p.Name), Value = "termValue" }; QueryContainer q = new FunctionScoreQuery() { Weight = 4, Functions = new[] { boost } }; var json = TestElasticClient.Serialize(q); var expected = @"{ function_score: { functions : [ { boost_factor: 2.0, filter:{ term : { ""name"":""termValue"" } }, weight: 1.0 } ], weight: 4.0 } }"; Assert.True(json.JsonEquals(expected), json); }
public static void HowToUseFunctionQuery(SearchDescriptor <ProductListEsIndexModelExample> sd) { var qs = new FunctionScoreQuery() { Name = "named_query", Boost = 1.1, Query = new MatchAllQuery { }, BoostMode = FunctionBoostMode.Multiply, ScoreMode = FunctionScoreMode.Sum, MaxBoost = 20.0, MinScore = 1.0, Functions = new List <IScoreFunction> { new ExponentialDecayFunction { Origin = 1.0, Decay = 0.5, Field = "", Scale = 0.1, Weight = 2.1 }, new GaussDateDecayFunction { Origin = DateMath.Now, Field = "", Decay = 0.5, Scale = TimeSpan.FromDays(1) }, new LinearGeoDecayFunction { Origin = new GeoLocation(70, -70), Field = "", Scale = Distance.Miles(1), MultiValueMode = MultiValueMode.Average }, new FieldValueFactorFunction { Field = "x", Factor = 1.1, Missing = 0.1, Modifier = FieldValueFactorModifier.Ln }, new RandomScoreFunction { Seed = 1337 }, new GaussGeoDecayFunction() { Origin = new GeoLocation(32, 4) }, new RandomScoreFunction { Seed = "randomstring" }, new WeightFunction { Weight = 1.0 }, new ScriptScoreFunction { Script = new ScriptQuery { Source = "x" } } } }; sd = sd.Query(x => qs); sd = sd.Sort(x => x.Descending(s => s.UpdatedDate)); sd = sd.Skip(0).Take(10); new ElasticClient().Search <ProductListEsIndexModelExample>(_ => sd); }
public void Function_score_query_must_transform_correclty_to_ES() { var functions = new IScoreFunction[] { new FilterScoreFunction(new MatchQuery("type", "text"), 2), new DecayScoreFunction(DecayFunction.Gauss, "date", "2013-09-17", "10d", "5d", 0.5) }; var query = new FunctionScoreQuery(new MatchQuery("headline", "Yuri Metelkin", true), functions); Assert.IsTrue(query.Query.Type == QueryType.MatchQuery); Assert.IsTrue(((MatchQuery)query.Query).Field == "headline"); Assert.IsTrue(((MatchQuery)query.Query).Value.ToString() == "Yuri Metelkin"); Assert.IsTrue(((MatchQuery)query.Query).IsAnd); var f = query.Functions[0]; Assert.IsTrue(f.Type == QueryType.FilterScoreFunction); var fsf = f as FilterScoreFunction; Assert.IsTrue(fsf.Weight == 2); f = query.Functions[1]; Assert.IsTrue(f.Type == QueryType.DecayScoreFunction); var dsf = f as DecayScoreFunction; Assert.IsTrue(dsf.Function == DecayFunction.Gauss); var json = query.ToString(); var jo = JsonObject.Parse(json); var q = jo.ToQuery(); Assert.IsTrue(q.Type == QueryType.FunctionScoreQuery); var fs = q as FunctionScoreQuery; var match = fs.Query as MatchQuery; Assert.IsTrue(match.Field == "headline"); Assert.IsTrue(match.Value.ToString() == "Yuri Metelkin"); Assert.IsTrue(match.IsAnd); f = query.Functions[0]; Assert.IsTrue(f.Type == QueryType.FilterScoreFunction); fsf = f as FilterScoreFunction; Assert.IsTrue(fsf.Weight == 2); f = query.Functions[1]; Assert.IsTrue(f.Type == QueryType.DecayScoreFunction); dsf = f as DecayScoreFunction; Assert.IsTrue(dsf.Function == DecayFunction.Gauss); }
public static void ReScore(IElasticClient client) { var rescore = new RescoringDescriptor <ProductListEsIndexModelExample>(); var query = new QueryContainer() && new MatchAllQuery(); var function_query = new FunctionScoreQuery() { Query = new MatchAllQuery(), ScoreMode = FunctionScoreMode.Average, Functions = new List <IScoreFunction>() { new GaussDateDecayFunction() { }, new ScriptScoreFunction() { } } }; var script_query = new ScriptQuery() { }; var script_query_ = new ScriptScoreFunction() { }; rescore = rescore.Rescore(x => x.RescoreQuery(d => d.Query(q => query) .QueryWeight(0.5) .RescoreQueryWeight(0.5) .ScoreMode(ScoreMode.Average)).WindowSize(10)) .Rescore(x => x.RescoreQuery(d => d.Query(q => function_query) .QueryWeight(0.5) .RescoreQueryWeight(0.5) .ScoreMode(ScoreMode.Average)).WindowSize(10)); //use rescore var sd = new SearchDescriptor <ProductListEsIndexModelExample>(); sd = sd.Rescore(x => rescore).Source(false); }
private static IQuery Transform(FunctionScoreQuery query, IDictionary <string, string[]> aliases, IQueryExpander qe) { return(new FunctionScoreQuery(query.Query.Transform(aliases, qe), query.Functions)); }
public void TestBoostFactor_ObjectInitializer_WrongProperty() { IFunctionScoreFunction boost = new BoostFactorFunction<ElasticsearchProject>(2); boost.Weight = 1; boost.Filter = new TermFilter {Field = Property.Path<ElasticsearchProject>(p => p.Name), Value = "termValue"}; QueryContainer q = new FunctionScoreQuery() { Weight = 4, Functions = new[] {boost} }; var json = TestElasticClient.Serialize(q); var expected = @"{ function_score: { functions : [ { boost_factor: 2.0, filter:{ term : { ""name"":""termValue"" } }, weight: 1.0 } ], weight: 4.0 } }"; Assert.True(json.JsonEquals(expected), json); }
public IActionResult Search([FromBody] Requests.SearchRequest request) { var sfConfig = new Dictionary <string, int>() { { "title", 10 }, { "code", 20 }, { "target", 2 } }; var splitTexts = request.SearchText.Split(' '); var query = new QueryContainer(); var titleField = Infer.Field <MyIndexDocument>(p => p.Title, 4); var bodyField = Infer.Field <MyIndexDocument>(p => p.Code, 10); var targetField = Infer.Field <MyIndexDocument>(p => p.Target, 2); query |= new MultiMatchQuery { Fields = titleField.And(bodyField).And(targetField), Query = request.SearchText }; query |= new QueryStringQuery { Query = $"\"{request.SearchText}\"", Boost = 50 }; var functions = new List <IScoreFunction>(); foreach (var item in sfConfig) { foreach (var text in splitTexts) { functions.Add(new WeightFunction { Filter = new MatchQuery { Field = item.Key, Query = text }, Weight = item.Value }); } } var fsQuery = new FunctionScoreQuery() { Name = "named_query", Boost = 1.1, Query = query, BoostMode = FunctionBoostMode.Sum, ScoreMode = FunctionScoreMode.Sum, MinScore = 1.0, Functions = functions }; var searchDescriptor = new SearchDescriptor <MyIndexDocument>() .Index("my-index") .Query(q => q.FunctionScore(fs => fsQuery)) .Explain(); var json = _elasticClient.RequestResponseSerializer.SerializeToString(searchDescriptor); var result = _elasticClient.Search <MyIndexDocument>(searchDescriptor); return(Ok(result.Documents)); }