1

Looking for help formulating a query for the following use case. I have a text field and an integer field which is an ID. I need to search for matching text but expecting the response to contain results that match one of the IDs. As an example, I have two fields. One is product ID which is text. And an owner ID which is an integer. Owners must be allowed to view only those products that are owned by them. To add on, an owner can have multiple IDs.

Sample records in Elasticsearch:

{
  "product": "MKL89ADH12",
  "ownerId" : 98765
},
{
  "product": "POIUD780H",
  "ownerId" : 12345
},
{
  "product": "UJK87TG89",
  "ownerId" : 98765
},
{
  "product": "897596YHJ",
  "ownerId" : 98765
},
{
  "product": "LKGGN764HH",
  "ownerId" : 784512
}

If 98765 and 12345 belong to the same owner, they should be able to view first 4 products only. And no results should be returned if they search for LKGGN764HH .

I tried following query but it gives me no results.

{
  "size": 24,
  "query": {
    "bool": {
        "must":[{
          "match" : {
              "product": {
                "query": "MKL89ADH12"
              }
            }
        },
        {
          "match" : {
              "product": {
                "query": "LKGGN764HH"
              }
            }
        },
        {
          "terms": {
          "ownerId": [98765, 12345],
          "boost": 1.0
          }
        }
        ]
    }
  }
}

I am expecting the response to contain MKL89ADH12 because I am filtering by the ownerId. Looking for help formulating the right query for my use case.

1 Answer 1

1

You need to filter on "ownerId": [98765, 12345] and then use "should" clause to return documents which match any of the text.

{
  "size": 24,
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "ownerId": [
              98765,
              12345
            ],
            "boost": 1
          }
        }
      ],
      "should": [
        {
          "match": {
            "product": {
              "query": "MKL89ADH12"
            }
          }
        },
        {
          "match": {
            "product": {
              "query": "LKGGN764HH"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

Above query will translate to

select * from index 
where  ownerid in ( 98765, 12345)
AND (product IN ("MKL89ADH12","LKGGN764HH))

while your works like

select * from index 
where  ownerid in ( 98765, 12345)
AND product = "MKL89ADH12"
AND product = "LKGGN764HH"
Sign up to request clarification or add additional context in comments.

Comments

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.