1

I have 3 document,

{
"category":[{"id":"1"},{"id":"2"},{"id":"3"}]
}

{
"category":[{"id":"1"},{"id":"4"}]
}
{
"category":[]
}

How I can find document which have category.id in (2,3) like mysql, also which type of DSL query I need to use in java api querybuilder

3 Answers 3

1

You can use the bool query with should clause to find all documents which contain either of the requested ids. Here is how the Query DSL would like

{
    "query": {
        "bool": {
            "should": [
               {"term": {
                  "category.id": {
                     "value": "2"
                  }
               }},
               {"term": {
                  "category.id": {
                     "value": "3"
                  }
               }
               }
            ]
        }
    }
}

Here's how you would use the Java API

QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("category.id", "2"))
                .should(QueryBuilders.matchQuery("category.id", "1"));

If the id field is not-analysed, you can also use the terms query. More info here https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

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

Comments

0

Here's how your query should look like:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "category.id": "1"
          }
        },
        {
          "term": {
            "category.id": "2"
          }
        }
      ]
    }
  }
}

Comments

0

Like this:

{
   "filter": {
        "terms": {
            "category.id": ["2", "3"]
        }
    }
}

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.