Elasticsearch-multi_match在嵌套字段上不起作用 [英] Elasticsearch - multi_match does not work on nested fields

查看:853
本文介绍了Elasticsearch-multi_match在嵌套字段上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个记录,可以对一个文本字段进行多种翻译,例如:

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"
            }
          }
        }
      }
    }
  }
}

但是当我尝试使用multi_map查询时,它不会返回预期的结果.此查询查找记录(通过顶级type字段搜索):

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"
        }
    }
}

但是此查询不会(通过嵌套的title.en字段搜索):

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

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

这是令人惊讶的,因为如果我得到title.en字段的术语向量,则似乎记录已正确索引:

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": []
                    }
                ]
            },
        ...
                ]
            }
        ]
    }
}

请注意,它正在字段title.en(no match on required clause ((type:dark | title.en:dark | title.de:dark)))中寻找令牌dark.

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))).

我正在使用Elasticsearch 6.2.1

I am using Elasticsearch 6.2.1

该查询似乎可以正常工作.我想念什么吗?

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

推荐答案

嵌套字段需要特殊的嵌套查询:

Nested fields require special nested queries:

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

但是我怀疑在您的情况下嵌套字段是否必要.只需对title字段使用常规对象类型,就可以通过简单的multi_match查询在所有文档字段中查找.

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.

这篇关于Elasticsearch-multi_match在嵌套字段上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆