Ivan Mushketyk
Ivan Mushketyk

Reputation: 8285

Elasticsearch - multi_match does not work on nested fields

I have records that can have multiple translations for a single text fields, e.g.:

{
  "type": "movie",
  "title": {
    "en": "Dark Knight",
    "de": "Der dunkle Ritter"
  }
}

To represent these records I've created the following index:

{
  "mappings": {
    "_doc": {
      "properties": {
        "type": {
          "type": "text",
          "analyzer": "english"
        },
        "title": {
          "type": "nested",
          "properties": {
            "de": {
              "type": "text",
              "analyzer": "german"
            },
            "en": {
              "type": "text",
              "analyzer": "english"
            }
          }
        }
      }
    }
  }
}

But when I try to use multi_map query it does not returns the expected result. This query finds the record (search by the top-level type field):

{
    "query": { 
        "multi_match" : {
            "query" : "movie"
        }
    }
}

But this query does not (search by the nested title.en field):

{
  "query": {
    "multi_match" : {
      "query": "dark"
    }
  }
}

This is surprising, since if I get term vectors for the title.en field it seems that the record was indexed properly:

GET /test_with_lang/_doc/1/_termvectors?pretty=true&fields=*

{
    "_index": "test_with_lang",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "found": true,
    "took": 1,
    "term_vectors": {
        "title.en": {
            "field_statistics": {
                "sum_doc_freq": 2,
                "doc_count": 1,
                "sum_ttf": 2
            },
            "terms": {
                "dark": {
                    "term_freq": 1,
                    "tokens": [
                        {
                            "position": 0,
                            "start_offset": 0,
                            "end_offset": 4
                        }
                    ]
                },
                "knight": {
                    "term_freq": 1,
                    "tokens": [
                        {
                            "position": 1,
                            "start_offset": 5,
                            "end_offset": 11
                        }
                    ]
                }
            }
        }
    }
}

It also seems that the query is using correct fields and it should match one of the tokens:

Request:
GET /test_with_lang/_doc/1/_explain
{
  "query": {
    "multi_match" : {
      "query": "dark"
    }
  }
}


Reply:
{
    "_index": "test_with_lang",
    "_type": "_doc",
    "_id": "1",
    "matched": false,
    "explanation": {
        "value": 0.0,
        "description": "Failure to meet condition(s) of required/prohibited clause(s)",
        "details": [
            {
                "value": 0.0,
                "description": "no match on required clause ((type:dark | title.en:dark | title.de:dark))",
                "details": [
                    {
                        "value": 0.0,
                        "description": "No matching clause",
                        "details": []
                    }
                ]
            },
        ...
                ]
            }
        ]
    }
}

Notice that it is looking for token dark in field title.en (no match on required clause ((type:dark | title.en:dark | title.de:dark))).

I am using Elasticsearch 6.2.1

It seems that the query should work. Am I missing something?

Upvotes: 3

Views: 2852

Answers (1)

briarheart
briarheart

Reputation: 2006

Nested fields require special nested queries:

"query": {
  "nested": {
    "path": "title",
    "query": {
      "multi_match": {
        "query": "dark"
      }
    }
  }
}

But I doubt that nested fields are necessary in your case. Just use regular object type for title field to be able to find across all document fields with simple multi_match query.

Upvotes: 3

Related Questions