-1

I have documents like this:

{
'body': '',
'date': '',
'agency_id': ''
}

I want to get documents with these conditions:

body contains :

all of ['word1', 'word2 word3', 'word4']  
Or all of: ['word5 word6', 'word7']  
or  all of: ['word8 word9', 'word10']  
And agency_id in ['id1', 'id2', 'id3']  

Would you please tell me how to create this query?

ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112

1 Answers1

0

To achieve what you want you need to use two must clauses, one for body and other for agency_id. In case of body you can specify your three conditions and use a minimum should match as 1. Should be something like this:

{
  "size" : 10,
  "query" : {
    "function_score" : {
      "query" : {
        "bool" : {
          "must" : {
            "bool" : {
              "should" : [ {
                "query_string" : {
                  "query" : "word1 worrd2 word3 word4",
                  "fields" : [ "body" ],
                  "default_operator" : "and"
                }
              }, {
                "query_string" : {
                  "query" : "word5 worrd6 word7",
                  "fields" : [ "body" ],
                  "default_operator" : "and"
                }
              }, {
                "query_string" : {
                  "query" : "word8 worrd9 word10",
                  "fields" : [ "body" ],
                  "default_operator" : "and"
                }
              } ],
              "minimum_should_match" : "1"
            }
          },
          "must" : {
            "query": {
              "terms" : { "agency_id" : [ "id1", "id2", "id3" ]}
            }
          }
        }
      }
    }
  }
}

Just make sure you use some analyzer to generate tokens for each word inside body. If you don't need any special feature you can just use standard analyzer.

Bruno dos Santos
  • 1,361
  • 1
  • 12
  • 21