Home > other >  Matching records that do not contain a substring in a array of nested fields elasticsearch
Matching records that do not contain a substring in a array of nested fields elasticsearch

Time:02-16

I am working with Elasticsearch 8, lets say we have two documents in our index:

[
    {
        "field1": "value1",
        "field2": [
            {
                "nestedField1": "nested value 1",
                "nestedField2": "rock paper scissors"
            },
            {
                "nestedField1": "nested value 2",
                "nestedField2": "paper scissors"
            }
        ]
    },
    {
        "field1": "value2",
        "field2": [
            {
                "nestedField1": "nested value 1",
                "nestedField2": "rock scissors"
            },
            {
                "nestedField1": "nested value 2",
                "nestedField2": "scissors"
            }
        ]
    }
]

Now I want to write a query such that we can fetch the record in which field2.nestedField2 does not contain the word paper.

Below is what I have so far:

{
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "match": {
                                "field2.nestedField2": "paper"
                            }
                        }
                    ]
                }
            }
        }
    }
}

I can't seem to get it to work. Can someone help me point out where I am going wrong?

CodePudding user response:

The query block is not correct. Nested fields can not search like this. you can find your answer here https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html.

  • Related