Multi match query does not work for nested types

Hi,

I'm trying to perform a multi match query on some data I have structured as nested types. When I search for something like "gadget plushy" I get no matches for a document with two nested fields "gadget" and "plushy". The following is an example what's not working for me

PUT /testing
{
  "mappings": {
    "properties": {
      "items": {
          "type": "nested",
          "properties": {
              "name": {"type": "text"}
          }
      }
    }
  }
}

Then add three documents

PUT /testing/_doc/1
{
  "items": [{"name": "gadget"}, {"name": "plushy"}]
}

PUT /testing/_doc/2
{
  "items": [{"name": "gadget"}]
}

PUT /testing/_doc/3
{
  "items": [ {"name": "plushy"}]
}

And finally search for both gadget and plushy, this gives zero hits for me. Searching for either gadget or plushy in the same query yields two results each though.

POST /testing/_search
{
  "query": {
    "nested": {
      "path": "items",
      "query": {
        "multi_match": {
          "fields": [
            "items.name"
          ],
          "operator": "and",
          "query": "gadget plushy",
          "type": "cross_fields"
        }
      }
    }
  }
}

I'm expecting this multi match query for "gadget plushy" should return the first document since the first nested item is named gadget and the second nested item is named plushy. Am I not understanding how this multi match queries should work for nested types or did I do something wrong?

Hi @Teqqan

The "AND" operator requires that both query terms exist in the field. In your case the items.name field should have the words "gadget plushy".

You can change the AND operator to OR and get results.

Hello @RabBit_BR and thank you for taking your time to answer me.

If I do OR I will get 3 results, I expect to only get a hit when at least one field includes plushy and one field includes gadget. This works for non nested fields. The following code sets up something similar like my original post but without nested fields. Running the last query yields exactly one result, document 1. This is what I expect for nested fields as well.

## Multi match without nested fields
PUT /testing
{
  "mappings": {
    "properties": {
      "item1": {"type": "text"},
      "item2": {"type": "text"}
    }
  }
}

PUT /testing/_doc/1
{
  "item1": "gadget",
  "item2": "plushy"
}

PUT /testing/_doc/2
{
  "item1": "gadget",
  "item2": "nothing"
}

PUT /testing/_doc/3
{
  "item1": "plushy",
  "item2": "nothing"
}

POST /testing/_search
{
  "query": {
    "multi_match": {
      "fields": [
        "item1", "item2"
      ],
      "operator": "and",
      "query": "gadget plushy",
      "type": "cross_fields"
    }
  }
}

if you search each term separately you will get doc 1 with the query below but I don't know if this will solve all your scenarios.

{
   "query":{
      "bool":{
         "must":[
            {
               "nested":{
                  "path":"items",
                  "query":{
                     "bool":{
                        "should":[
                           {
                              "match":{
                                 "items.name":"gadget"
                              }
                           }
                        ]
                     }
                  }
               }
            },
            {
               "nested":{
                  "path":"items",
                  "query":{
                     "bool":{
                        "should":[
                           {
                              "match":{
                                 "items.name":"plushy"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

Normally you would use nested fields only when you want the isolation you’re experiencing.
It’s designed for criteria that needs to test properties of nested objects in isolation.
Without nested fields you get the “cross matching” style that doesn’t care which contained objects provide a match.

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