Chris
Chris

Reputation: 18876

Elastic Java Boost on Date

I am looking to follow up on this question of boosting results based on date. However, in this question the decay function seems to take precedence over the actual search content.

QueryBuilder qb = QueryBuilders.multiMatchQuery(term,"title", "description","url").field("title", 1.75f).field("url", 1.55f).field("description", 1.35f);
FunctionScoreQueryBuilder builder = QueryBuilders.functionScoreQuery(qb);
builder.add(ScoreFunctionBuilders.exponentialDecayFunction("postDate","14d"));

So for example, if I search my index for tweets on Mario Draghi with the above builder, IF there are newer tweets in the last 14 days mentioning 'Mario Draghi' they come in first followed by tweets in the last 14 days mentioning anything else that matched 'Mario' (As opposed to showing older 'Mario Draghi' tweets).

What Im looking to do is basically boost a document if its date field 'postDate' is new-ish (lets say 14 days) but I dont want the date to take precedent over the accuracy of search. I would rather older 'Mario Draghi' tweets over 'Super Mario Bros' tweets. In other words, there are thousands of tweets in my index mentioning Mario Draghi - but in the above code I only get them if they fall within 14 days.

Can someone point me to a more accurate way to accomplish? I see in this post where boosts come within ranges. That makes sense to me -- but Im wondering if this is the preferred way to accomplish? Im hoping there is a more elegant way somehow?

Upvotes: 1

Views: 744

Answers (1)

Chris
Chris

Reputation: 18876

In many cases, the desired outcome is to keep your relevance score and give the more recent matches an extra boost (a higher score) because the data is fresher. In order to achieve this, you can use Elasticsearch’s function scoring. Webpage Source.

final MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("Bababooey", "title^0.8", "url^0.6", "description^0.3").type(MultiMatchQueryBuilder.Type.BEST_FIELDS);

final FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(multiMatchQuery);
functionScoreQuery.scoreMode("multiply");
functionScoreQuery.boostMode(CombineFunction.MULT);
functionScoreQuery.add(ScoreFunctionBuilders.gaussDecayFunction("postDate","130w").setOffset("26w").setDecay(0.3));

This example boosts all documents published the past half year. Documents older than six months will score gradually less until a threshold of two and a half years is reached. Documents older than two and a half years will not get any extra scoring based on recency.Shifting this to two weeks or whatever may be your boost window is simple by changing setOffet and setDecay...

Upvotes: 2

Related Questions