-1

I have documents like this:

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

I want to get documents with these conditions:

  • body contains all of: ['a', 'b', 'c']
  • and contains one of: ['d', 'e', 'f']
  • and contains exactly these phrases: ['g h i', 'j k l']
  • and not contains: ['m', 'n']

How can I create this query?

halfer
  • 19,824
  • 17
  • 99
  • 186
ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112
  • You've asked a few more questions since receiving an answer on this post. Would you take a look at the help below that has been generously offered, and see if it worked for your use case? While it is not mandatory to accept or respond to answers, we'd have no community here at all if no question authors replied to help rendered. Thanks! – halfer Dec 23 '17 at 23:08

1 Answers1

1

You need to use bool queries. Important to note that for your "contains exactly these phrases" how that works depends on what analyzers you have applied to the body field.

https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-bool-query.html

For example:

{
  "query": {
    "bool": {
      "must": [
        {"match": {"body": "a"}},
        {"match": {"body": "b"}},
        {"match": {"body": "c"}},
        {"match_phrase": {"body": "g h i"}},
        {"match_phrase": {"body": "j k l"}}
      ],
      "should": [
        {"match": {"body": "d"}},
        {"match": {"body": "e"}},
        {"match": {"body": "f"}}
      ],
      "must_not": [
        {"match": {"body": "m"}},
        {"match": {"body": "n"}}
      ]
    }
  }
}
Ryan Widmaier
  • 7,948
  • 2
  • 30
  • 32