0
votes

I want to build an ElasticSearch query where I query multiple fields, but one of the words in the query MUST match one of the fields.

For instance, suppose I query for "holiday party food", I want it to return all documents that have at least 1 of the terms in the title, and the rest in the html_source.

If a document has:

title: Holiday, html_source: party food => MATCH
title: Party, html_source: food holiday => MATCH
title: Food, html_source: holiday party => MATCH
title: RANDOM TITLE, html_source: holiday party food => NO MATCH.

This is what I have constructed so far, and it matches documents that have all the terms in the html_source, but NOT in the title, which is not what I want.

{
   "query":{
      "query_string":{
         "query":"holiday party food",
         "default_operator":"AND",
         "fields":[
            "title","html_source"
         ]
      }
   },
   "sort":[
      {
         "total_shares":"desc"
      }
   ]
}
1
what is your analyzer? - shyos

1 Answers

0
votes

Here is a query syntax version that I think will do want you want:

+(
  (+title:holiday +(body:party   body:food  -body:holiday))
  (+title:party   +(body:holiday body:food  -body:party)
  (+title:food    +(body:holiday body:party -body:food))
)

If this does not work, I think it points you in the right direction. The trick in this kind of query is to enumerate all of the choices explicitly.