1

I have 2 documents in elasticsearch in the below structure:

Document 1:

{
  "specification": [
                    {
                     "name": "Processor",
                     "value": "Intel"
                    },
                    {
                     "name": "RAM",
                     "value": "2GB"
                    }
                   ]
}

Document 2:
{
  "specification": [
                    {
                     "name": "Processor",
                     "value": "Intel"
                    },
                    {
                     "name": "RAM",
                     "value": "3GB"
                    }
                   ]
}

I want to get the document that have a specification with values intel and 2GB (i.e) 1st document. But when i try to use must (AND operator) i am getting nothing. If i use should (OR operator) i am getting both the documents. Can anyone help me on this? Below is my query..

{
  "query": {
    "nested": {
      "path": "specification",
      "query": {
        "bool": {
          "must": [

                {
                    "bool": {
                        "must": [
                            { "match": { "specification.name": "Processor" }},
                            { "match": { "specifications.value": "Intel" }}
                        ]
                    }
                },
                {
                    "bool": {
                        "must": [
                            { "match": { "specification.name": "RAM" }},
                            { "match": { "specifications.value": "2GB" }}
                        ]
                    }
                }
              ]
        }
      }
    }
  }
}

1 Answer 1

1

Try this one:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "specification",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "specification.name": "Processor"
                    }
                  },
                  {
                    "match": {
                      "specification.value": "Intel"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "specification",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "specification.name": "RAM"
                    }
                  },
                  {
                    "match": {
                      "specification.value": "2GB"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
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.