elasticsearch 如何在无痛模式下访问标测子字段?

1wnzp6jl  于 2023-02-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(104)

假设存在ElasticSearchMap:

"mappings": {
      "dynamic_templates": [],
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "x": {
              "type": "completion"
            },
            "y": {
              "type": "keyword"
            },
            "z": {
              "type": "text",
              "analyzer": "shingles",
              "fielddata": true
            }
        }

如何从轻松脚本访问name.x/y/z数据/令牌?
以下不起作用:

ctx._source.name.x
ctx._source['name.fields.x']
ctx._source['name.x']
eh57zj3b

eh57zj3b1#

哦,我明白了,你想把name.z中产生的任何东西都反馈到name.x中。首先,我认为你认为多字段工作的方式有一个误解。你只需要指定name字段的值,每个子字段都会根据它的类型/分析器得到一个值。但是您不能直接为这些子字段中的任何一个指定值。
您应该做的是在完成字段中直接指定shingles分析器,这样应该可以了。

    • 更新**

你想做的事情是不可能自动完成的,也不能使用多字段,你需要一个独立的completion字段,而不是name的子字段。你想做的过程可以这样做,但需要在客户端编码:

# 1. Index your document
PUT test/_doc/1
{ "name": "the big brown fox" }

# 2. For each indexed document, retrieve the term vectors for name.z
GET test/_termvectors/1?fields=name.z

=> 
  "term_vectors" : {
    "name.z" : {
      "field_statistics" : {
        "sum_doc_freq" : 7,
        "doc_count" : 1,
        "sum_ttf" : 7
      },
      "terms" : {
        "big" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 1,
              "start_offset" : 4,
              "end_offset" : 7
            }
          ]
        },
        "big brown" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 1,
              "start_offset" : 4,
              "end_offset" : 13
            }
          ]
        },

# 3. Retrieve all the keys from the terms map obtained in step 2
"big", "big brown", ...

# 4. Feed the retrieved tokens back into the document's top-level completion field
POST test/_doc/1/_update
{
   "doc": {
       "completion": {
           "input": [ "big", "big brown", ... ],
       }
   }
}

相关问题