Weird OpenSearch query results when using nested objects and some questions

107 views Asked by At

Initially, I had some documents with inner objects in them and I was performing queries that returned reasonable results.

In order to perform queries on those inner objects correctly with multiple required conditions (like ConditionA AND ConditionB) I have decided to convert some of the inner objects into nested objects and reindex the data.

Now, I am noticing many weird behaviors while performing queries. One is this:

Query:

POST documents\_search
{
  "query": {
    "term": {
     "copies.idOwnedByGroup": 1
    }
  }
}

Result:

results1

Query:

POST documents\_search
{
  "query": {
    "match_all": {
    }
  }
}

One of the results:

results2

The copies object is a nested object. Here is the mapping:

mapping

Why doesn't the first query find this document? This seemed to work with copies not being a nested object.

I am also using the OpenSearch.Client library and that also produces similar queries that don't seem to work anymore.

When I try this, it seems to work (it returns many matching results):

POST documents\_search
{
  "query": {
    "nested": {
      "path": "copies",
      "query":{
        "term": {
         "copies.idOwnedByGroup": 1
        }
      }
    }
  }
}

But I have some questions:

  1. Does this mean I have to perform ONLY nested queries on data inside nested objects? It is weird that no error is returned from the non-nested query if that is the case...
  2. Do I continue using non-nested queries for data outside of nested objects?
  3. What if I have a nested object inside another nested object? Do I specify the first nested object's path as path and OpenSearch figures things out when it comes to the inner nested object?
  4. I have many other queries that fail now because of nested objects, and I am not sure if the above change will solve them all. Is there anything else I need to pay attention to in this case?
1

There are 1 answers

0
user2173353 On BEST ANSWER

I have the answers:

  1. Yes. Bad design copied from ElasticSearch.
  2. Yes.
  3. When using nested objects inside nested objects, you need to specify the inner-most nested object as path.
  4. Probably not.