I am struggling with understanding the behavior of "must" and "should" .
Following is my search query,
GET listings/_search?
{
"query": {
"bool": {
"must": [
{ "match": { "city": "Terre Haute" }},
{ "match": { "state": "IN" }},
{ "match": { "listing" : "wal mart pharmacy"}}
]
, "should": [
{"match": {
"caplist": "main general information customer service store line"
}}
]
}
},
"_source": [ "listing", "caplist","city","state","telnum"]
}
and the top two results it returns are as follows,
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 46,
"max_score": 8.381018,
"hits": [
{
"_index": "listings",
"_type": "listings",
"_id": "e62e6314f3a5951a27e6954d519c7431",
"_score": 8.381018,
"_source": {
"city": "Terre Haute",
"state": "IN",
"caplist": "Main Store Line;",
"listing": "Wal Mart",
"telnum": "8128722520"
}
},
{
"_index": "listings",
"_type": "listings",
"_id": "4d3dbd1e1012fae7efb9b86b032f258a",
"_score": 5.925434,
"_source": {
"city": "Terre Haute",
"state": "IN",
"caplist": "",
"listing": "Wal-Mart Pharmacy -10-4235",
"telnum": "8128722314"
}
I had expected the order to be the other way round ( That is what I want) i.e. the document where "listing" : "Wal-Mart Pharmacy -10-4235" to be ranked higher as the "must" field has all the three words "wal","mart" and "pharmacy".
I read the documentation and I understood that all the "must" clauses must be matched and "should" can be optional.
What I want is to rank the documents higher where all words in the "must" clauses match and "should" clause is just to boost the score once all the "must" is matched.
what changes should I make to me search query to score the "listing": "Wal-Mart Pharmacy -10-4235" higher than "listing": "Wal Mart".
I am just a beginner in Elasticsearch. Any help would be appreciated.
Thanks!