Help with searching in a nested property of a non-nested field

Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):

Opensearch: 1.3.3

Describe the issue:

I’m trying to search on a nested property of another property that isn’t nested. My mappings looks like:

{
  "mappings": {
    "properties": {
      "my-non-nested-property": {
        "properties": {
          "my-nested-property": {
            "type": "nested",
            "properties": {
              "prop1": {
                "type": "text"
              },
              "prop2": {
                "type": "text"
              },
              "prop3": {
                "type": "keyword"
              },
              "prop4": {
                "properties": {
                  "type": {
                    "type": "text"
                  },
                  "value": {
                    "type": "text"
                  }
                }
              },
              "prop5": {
                "type": "text"
              },
              "prop6": {
                "type": "date"
              },
              "prop7": {
                "type": "date"
              }
            }
          },
          "index": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

And this is the search I’m trying to use:

{
  "query": {
    "nested": {
      "path": "my-non-nested-property.my-nested-property",
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "my-non-nested-property.my-nested-property.prop1": "AVALUE"
              }
            }
          ]
        }
      }
    }
  }
}

I’ve populated the index with a single document that has a my-non-nested-property.my-nested-property.prop1 set as “AVALUE”, but I cannot find the value using the filter I described. I’m new to opensearch, so I might be doing something wrong. I can confirm that if “my-nested-property” is a first-order property (on the same level as my-non-nested-property), I can search using nested queries, the problem seems when I have a nested property as a child of non-nested properties. I appreciate any advice about how to search on the described use-case.

Thank you,
Best regards.

The reason is that prop1 is a text field which will be analyzed when indexing and searching, the original string in the text field will be tokenized, filtered, normalized and generate terms, in your case, AVALUE will be lowercased and generate term avalue, so if you want to use term query(mostly perform on keyword type), you can search as this:

{
              "term": {
                "my-non-nested-property.my-nested-property.prop1": "avalue"
              }
            }

, or use match query on the text field:

{
              "match": {
                "my-non-nested-property.my-nested-property.prop1": "AVALUE"
              }
            }
1 Like