Unexpected results when filtering docs in nested field

Hi,

suppose we have the following docs: (index name is test_index)

{
    session_id: "123",
    "session_details":[
        {
            "page": "/abc"
        },
        {
            "page": "/xyz"
        }
    ]
}

{
    session_id: "456",
    "session_details":[
        {
            "page": "/abc"
        },
        {
            "page": "/qwe"
        }
    ]
}

mappings is like this:

{
    "session_id" : {
        "type" : "keyword"
    },
    "session_details" : {
        "type" : "nested",
        "properties" : {
          "page" : {
            "type" : "keyword"
          }
        }
    }
}

if I run the following query I expect to receive the doc with the session_id = 123
but I receive both docs, any explanation ?

GET test_index/_search?ignore_unavailable=true
{
  "track_total_hits": true,
  "size": 10,
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "session_details",
            "query": {
              "bool": {
                "filter": [
                  {
                    "wildcard": {
                      "session_details.page": "*/abc*"
                    }
                  },
                  {
                    "bool": {
                      "must_not": [
                        {
                          "wildcard": {
                            "session_details.page": "*/xyz*"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Hi!!

This post is a little similar:

Perhaps the solution is to use a script.

That post was 4 years ago, so many Elastic versions came out during this time.
painless scripts are not recommended in terms of performance.

I understand that each document in nested field is considered and indexed as separate document. The question is, given the above mappings, is it possible to query a nested field as a regular array or something like that ?

One solution is to change the field type from nested to a regular object like this:

PUT test_prop
{
  "mappings": {
    "properties": {
      "session_id": {
        "type": "keyword"
      },
      "session_details": {
        "properties": {
          "page": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

But this will result in a huge data duplication, also suppose we have another field session_details: page, path:

session_details: [
     { page: "xyz", path: "qqq" },
     { page: "jkl", path: "aaa" }
]

a query like: page = "xyz" and path != "aaa" will still return the doc.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.