0

I am trying to generate a nested must and should query in elasticsearch to mimic the following condition:

(  (brandsvisited ilike '%adidas%' or   brandsvisited ilike '%skechers%' ) or( placecategory ilike '%Pizza Restaurant Visitors%' or   placecategory ilike '%Grocery Store Visitors%'  ) and( gender='Male'  )  ) and polygonid = '1465'

I created the following query in elasticsearch.

"query": {
      "bool": {
          "must": [
              {"term": {"polygonid": "1465"}},
              {"term": {"gender": "Male"}},
              {
                  "bool": {
                      "should": [
                          {"term": {"brandsvisited": "adidas"}},
                          {"term": {"brandsvisited": "skechers"}}
                      ],"minimum_should_match": 1
                  }
              },
              {
                  "bool": {
                      "should": [
                          {"term": {"placecategory": "Pizza Restaurant Visitors"}},
                          {"term": {"placecategory": "Grocery Store Visitors"}}
                      ],"minimum_should_match": 1
                  }
              }
          ]
      }
  }

The above query returns zero results. However, if I remove the last boolean should query, it returns the results. Not sure where I am going wrong.

4
  • Why are you using terms query if you have to search for '%Pizza Restaurant Visitors%'? Commented Jun 16, 2021 at 5:11
  • @TusharShahi I could have used match also...I am completely new to elasticsearch and not sure which one is right. Commented Jun 16, 2021 at 5:29
  • Check this stackoverflow.com/questions/26001002/…. Check what ESCoder has mentioned about matchphrase. Commented Jun 16, 2021 at 5:31
  • @TusharShahi Thank you for that question...I tried with match and it started returning data Commented Jun 16, 2021 at 5:31

1 Answer 1

0

Assuming that the placecategory field is of text type. You need to replace that with placecategory.keyword field (if you have not defined any explicit index mapping)

Term query returns documents that contain an exact term in a provided field. If you have not defined any explicit index mapping, then you need to add .keyword to the field. This uses the keyword analyzer instead of the standard analyzer.

Modify your query as shown below

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "polygonid": "1465"
          }
        },
        {
          "term": {
            "gender": "Male"
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "brandsvisited": "adidas"
                }
              },
              {
                "term": {
                  "brandsvisited": "skechers"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "placecategory.keyword": "Pizza Restaurant Visitors"
                }
              },
              {
                "term": {
                  "placecategory.keyword": "Grocery Store Visitors"
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}

If you want to search for an exact term in the placecategory then you can either use term query with should clause or terms query, otherwise, if you want to match phrase in the placecategory field, then you can use match_phrase query

Sign up to request clarification or add additional context in comments.

3 Comments

@Apricot match query will not give you the correct answer, as it would match all the documents that have either Pizza or Restaurant or Visitors in the placecategory field. However according to your SQL query match_phrase would give you the correct answer
Many thanks, I tried all combinations for my use case terms with keyword, match and match_phrase....you suggestion on match_phrase works perfectly...Thank you for your continuous support and detailing.
@Apricot glad I could help you :-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.