I am trying to do a search in elastic search
server.
Below is my case:
Given a term "Hello World"
to the search API, return me all the documents with:
I want to do the above in a single query. I am aware that individually all of them can be done using match_phrase, and default_operator for OR/AND. But I want all the three to be done in a single query.
I want the results to be sorted based on relevancy. So if a document contains the exact phrase, it is most relevant.If the document contains both the words (AND) some where in it, it is moderately relevant. And if the document contains at least one of the word(OR), it is least relevant.
Is it possible in elastic search as of now ?
This is something you might require:
Query String
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
{
"query":
{
"query_string" :
{
"query" : "Hello World"
}
}
}
This will fetch you all combinations :
You want to check Bool Query and possibly Boosting Query Clauses .
For your example, you can do
{
"query": {
"bool": {
"should": [
{
"match": {
"FIELDNAME": {
"query": "Hello",
"boost": 1
}
}
},
{
"match": {
"FIELDNAME": {
"query": "World",
"boost": 1
}
}
},
{
"match_phrase": {
"FIELDNAME": {
"query": "Hello World",
"boost": 2
}
}
}
],
"minimum_should_match" : 1
}
}
}
This is asking that at least 1 between your three conditions is true ( minimum_should_match
). The more conditions are satisfied, the higher score the document will have (so it will be returned first), like you wished. You can boost
the importance of a condition, here I went for doubling the importance of whole phrase match, but it's just an example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.