I have some documents like this
{"id":1,"city":"London","content":"soccer","continent":"Europe"},
{"id":2,"city":"New York","content":"basketball","continent":"North America"},
{"id":3,"city":"Tokyo","content":"baseball","continent":"Asia"},
...
I need to search keywords among some fields(excluding city
field), e.g. a query like
{"query":{"bool":{
"should":[ //SHOULD_CLAUSE
"match":{"continent":"America"},
"term":{"content":"soccer"}
]
}}}
To make the results more "personalized", I want to make matched documents whose city
field is the same as the visiting user's city
property.
However, if I make city
as a query field(something like "match":{"city":"Tokyo"}
) in should
boolean clause, it may return some documents that only match the city
field, which mismatch the fields I need to search. When using boost
to make city
field more "important" for sorting things goes worse.
How can I achieve my goal?
It seems that a possible way write the SHOULD_CLAUSE
part twice and make one of it combined with city
clause using and
{"query":{"bool":{
"should":[
{"bool":{"must":[
{"bool":{SHOULD_CLAUSE}},
{"match":{"city":{"query":"Tokyo","boost":4.0}}}
]}},
{"bool":{SHOULD_CLAUSE}}
]
}}}
But under the real circumstance the SHOULD_CLAUSE
part may be more complicated and the whole query seems too long to write. I wonder if there is a better way.