Elasticsearch - multi_match不适用于嵌套字段

时间:2018-02-16 18:12:37

标签: elasticsearch elasticsearch-6

我的记录可以为单个文本字段提供多个翻译,例如:

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

为了表示这些记录,我创建了以下索引:

{
  "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字段搜索):

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

但是这个查询没有(通过嵌套的title.en字段搜索):

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

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

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

似乎查询使用了正确的字段,它应该匹配其中一个标记:

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

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

我正在使用Elasticsearch 6.2.1

似乎查询应该有效。我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

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

npm ERR! react-registration@0.1.0 lint: `esw webpack.config.* src tools --color`
npm ERR! Failed at the react-registration@0.1.0 lint script.
npm ERR! react-registration@0.1.0 prebuild: `npm run clean-dist && npm run lint && npm run test`
npm ERR! Failed at the react-registration@0.1.0 prebuild script.

但我怀疑在您的情况下嵌套字段是必要的。只需使用0 info it worked if it ends with ok 1 verbose cli [ '/home/serj/.nvm/versions/node/v8.9.4/bin/node', 1 verbose cli '/home/serj/.nvm/versions/node/v8.9.4/bin/npm', 1 verbose cli 'run', 1 verbose cli 'lint' ] 2 info using npm@5.6.0 3 info using node@v8.9.4 4 verbose run-script [ 'prelint', 'lint', 'postlint' ] 5 info lifecycle react-registration@0.1.0~prelint: react-registration@0.1.0 6 info lifecycle react-registration@0.1.0~lint: react-registration@0.1.0 7 verbose lifecycle react-registration@0.1.0~lint: unsafe-perm in lifecycle true 8 verbose lifecycle react-registration@0.1.0~lint: PATH: /home/serj/.nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/ node-gyp-bin:/home/serj/Desktop/Symfony/DEPLOY/react/node_modules/.bin:/home/serj/.nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/ npm-lifecycle/node-gyp-bin:/home/serj/Desktop/Symfony/DEPLOY/react/node_modules/.bin:/home/serj/.nvm/versions/node/v8.9.4/bin:/home/serj/bin:/ home/serj/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/serj/.composer/vendor/bin 9 verbose lifecycle react-registration@0.1.0~lint: CWD: /home/serj/Desktop/Symfony/DEPLOY/react 10 silly lifecycle react-registration@0.1.0~lint: Args: [ '-c', 'esw webpack.config.* src tools --color' ] 11 silly lifecycle react-registration@0.1.0~lint: Returned: code: 1 signal: null 12 info lifecycle react-registration@0.1.0~lint: Failed to exec lint script 13 verbose stack Error: react-registration@0.1.0 lint: `esw webpack.config.* src tools --color` 13 verbose stack Exit status 1 13 verbose stack at EventEmitter.<anonymous> (/home/serj/.nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/index.j s:285:16) 13 verbose stack at emitTwo (events.js:126:13) 13 verbose stack at EventEmitter.emit (events.js:214:7) 13 verbose stack at ChildProcess.<anonymous> (/home/serj/.nvm/versions/node/v8.9.4/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spa wn.js:55:14) 13 verbose stack at emitTwo (events.js:126:13) 13 verbose stack at ChildProcess.emit (events.js:214:7) 13 verbose stack at maybeClose (internal/child_process.js:925:16) 13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5) 14 verbose pkgid react-registration@0.1.0 15 verbose cwd /home/serj/Desktop/Symfony/DEPLOY/react 16 verbose Linux 4.10.0-38-generic 17 verbose argv "/home/serj/.nvm/versions/node/v8.9.4/bin/node" "/home/serj/.nvm/versions/node/v8.9.4/bin/npm" "run" "lint" 18 verbose node v8.9.4 19 verbose npm v5.6.0 20 error code ELIFECYCLE 21 error errno 1 22 error react-registration@0.1.0 lint: `esw webpack.config.* src tools --color` 22 error Exit status 1 23 error Failed at the react-registration@0.1.0 lint script. 23 error This is probably not a problem with npm. There is likely additional logging output above. 24 verbose exit [ 1, true ] 字段的常规对象类型,即可在所有文档字段中找到简单的"query": { "nested": { "path": "title", "query": { "multi_match": { "query": "dark" } } } } 查询。