Home > Back-end >  Is there a way to match the exact record in a multiple-Condition elasticsearch BoolQuery?
Is there a way to match the exact record in a multiple-Condition elasticsearch BoolQuery?

Time:12-16

I have an application in which I use elasticsearch as my search engine and I have a problem that I need help with.

In my search query, I want to get a document that contains a reference type with the value "NVE" and a reference value with the value "8005267293"

Below is a snippet of the document I want to get looks like this

.......
  "addresses": [],
  "references": [
                    {
                        "type": "FBA",
                        "value": "0000765855"
                    },
                    {
                        "type": "GEN",
                        "value": "0082467914"
                    },
                    {
                        "type": "GEN",
                        "value": "0000765855"
                    },
                    {
                        "type": "GEN",
                        "value": "0006418559"
                    },
                    {
                        "type": "NVE",
                        "value": "8005267293"
                    },
                    {
                        "type": "NVE",
                        "value": "000000008005267291"
                    }
                ],
........

My issue is my query actually returns the document as expected but if I change the value of references.type to "GEN" or "FBA", it still returns the document but I want it only to return the document if the references.type is "NVE" and the references.value is "8005267293"

For my search query, this is what I used

{
    "query": {
        "bool": {
            "must": [
              {
                "bool": {
                    "must": [{
                        "match": {
                            "references.type": "NVE"
                        }
                    }]
                }
            }, 
            {
                "bool": {
                    "must": [
                      {
                        "wildcard": {
                            "references.value": "*8005267293*"
                        }
                    }]
                }
            }]
        }
    }
}

Any help will be appreciated, thanks

CodePudding user response:

You must be using the default mapping or defined references field as an object data type which looses the relationship between the properties as mentioned in the official documentation.

You need to use the nested data type for your references field and use nested query for getting expected results.

  • Related