Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch taking word order into account in match query

Suppose that my index have two documents:

  1. "foo bar"
  2. "bar foo"

When I do a regular match query for "bar foo", both documents match correctly but they get equal relevance scores. However, I want the order of words to be significant during scoring. In other words, I want "bar foo" to have a higher score.

So I tried putting my match query inside the must clause of a bool query and included a match_phrase (with the same query string) as the should clause. This seems to score hits correctly, until I do a search with "bar test foo". In that case match_phrase query doesn't seem to match, and the hits are returned with equal scores again.

How can I construct my index/query so that it takes word order into account but does not require all searched words to exist in document?

like image 575
Can Avatar asked Jan 14 '15 22:01

Can


People also ask

How do I sort in Elasticsearch query?

Sort mode optioneditPick the highest value. Use the sum of all values as sort value. Only applicable for number based array fields. Use the average of all values as sort value.

How does match query work in Elasticsearch?

The match query analyzes any provided text before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term. (Optional, string) Analyzer used to convert the text in the query value into tokens. Defaults to the index-time analyzer mapped for the <field> .

What is the difference between term and match query in Elasticsearch?

As the match_phrase queries, the input is analyzed according to the analyzer set on the queried field. Unlike the match_phrase , the terms obtained after analysis don't have to be in the same order, unless the user has used quotes around the input.

Does Elasticsearch support text queries?

ElasticSearch is a search engine based on Apache Lucene, a free and open-source information retrieval software library. It provides a distributed, full-text search engine with an HTTP web interface and schema-free JSON documents.


1 Answers

Have a look at SpanNearQuery, it allows specifying order with or without slop (limit of how far the terms should be apart each other).

Elasticsearch documentation is here.

like image 53
mindas Avatar answered Sep 18 '22 07:09

mindas