May I search among some fields, but use another field's matching score for sorting?


(Yang Ke) #1

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.


(Camilo Sierra) #2

are you already test to use minimum-should-match ?
you can show us your query and the mapping for your index ?


(Yang Ke) #3

Hi Sierra,

I've edited my question, adding some samples to make it more clear to unserstand.


(Nik Everett) #4

You can make a query's score not count by wrapping it in a constant score query or a filter like a query_filter. Maybe you can just wrap the bits you don't want to score in one of those?


(system) #5