0

I'm using elastic search 6.5.

I am trying to put together a query that will do the following:

search for documents where

  • primary key values must be either 85, 0, 95, or 3.
  • where a sub key1 value must start with 4444 or empty string. OR,
  • where a second sub key2 value must be start with 5555 or empty string.

it also includes some date range criteria.

I feel like i need two groups of 'should'. because at least 1 match must occur in both groups. but they can't be combined.. because the at least 1 of the subkeys must be matched too. The "must" below is incorrect, because it 'should' be a Should but two groups of should aren't possible.

Any idea how i can be done?

I have put together this query:

{
  "size" : 200, "from": 0,
  "query":{
        "bool": {
            "should": [{ "term": { "primary_key": "84"}},  
                       { "term": { "primary_key": "3"}},
                       { "term": { "primary_key": "0"}},
                       { "term": { "primary_key": "95"}}],
            
            "must":   [{ "match_phrase_prefix": { "subkey1" : "65200010100780"}}, 
                       { "match_phrase_prefix": { "subkey2" : "00228315303"}}],
                     
          "filter": [  { "range": { "start_date": { "lte": "2018-11-01" }}},
                       { "range": { "end_date": { "gt": "2018-11-01"}}}] 
        }
  }

1 Answer 1

2

Since at least one match must occur in both groups of "should", I would do it like this, with two bool/should in the bool/must clause:

{
  "size": 200,
  "from": 0,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "primary_key": "84"
                }
              },
              {
                "term": {
                  "primary_key": "3"
                }
              },
              {
                "term": {
                  "primary_key": "0"
                }
              },
              {
                "term": {
                  "primary_key": "95"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "match_phrase_prefix": {
                  "subkey1": "65200010100780"
                }
              },
              {
                "match_phrase_prefix": {
                  "subkey2": "00228315303"
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "range": {
            "start_date": {
              "lte": "2018-11-01"
            }
          }
        },
        {
          "range": {
            "end_date": {
              "gt": "2018-11-01"
            }
          }
        }
      ]
    }
  }
}
2
  • thanks.. didn't know you can nest those clauses... that helps. Nov 19, 2018 at 15:35
  • A bool query is a regular query that you can definitely nest
    – Val
    Nov 19, 2018 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.