代码之家  ›  专栏  ›  技术社区  ›  Elvira

弹性搜索副本\未填充到字段

  •  1
  • Elvira  · 技术社区  · 6 年前

    我正在尝试将弹性搜索5.6中的主标题字段复制到另一个包含:index:false的字段中,这样我就可以使用此字段来匹配准确的值。

    然而。重新建立索引后,使用_source:[精确的_hoofdtiel“]执行搜索,字段“精确的_hoofdtiel”不填充“hoofdtiel”的值。

    PUT producten_prd_5_test
    {
     "aliases": {},
     "mappings": {
       "boek": {
         "properties": {
           "hoofdtitel": {
             "type": "text",
             "copy_to": [
               "suggest-hoofdtitel", "exact_hoofdtitel"
             ]
           },
           "suggest-hoofdtitel": {
             "type": "completion",
             "analyzer": "simple",
             "preserve_separators": false,
             "preserve_position_increments": true,
             "max_input_length": 50
           },
           "exact_hoofdtitel":{
             "type":"text",
             "fields":{
               "keyword":{
                 "type":"keyword",
                 "index":false
               }
             }
           },
         }
       }
     },
     "settings": {
       "number_of_shards": "1",
       "number_of_replicas": "0"
     }
    }
    
    
    GET producten_prd_5_test/_search
    {
      "_source":["hoofdtitel","exact_hoofdtitel"]
    }
    
    
    hits": [
          {
            "_index": "producten_prd_5_test",
            "_type": "boek",
            "_id": "9781138340671",
            "_score": 1,
            "_source": {
              "hoofdtitel": "The Nature of the Firm in the Oil Industry"
            }
          },
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Nikolay Vasiliev    6 年前

    我相信你可以实现你想要的 copy_to . 我来告诉你为什么不需要它。

    如何在同一字段上进行全文和精确匹配查询?

    这可以用 fields 映射属性。基本上,使用以下映射:

    PUT producten_prd_5_test_new
    {
      "aliases": {},
      "mappings": {
        "boek": {
          "properties": {
            "hoofdtitel": {
              "type": "text", <== analysing for full text search
              "fields": {
                "keyword": {
                  "type": "keyword" <== analysing for exact match
                },
                "suggest": {  
                  "type": "completion",  <== analysing for suggest
                  "analyzer": "simple",
                  "preserve_separators": false,
                  "preserve_position_increments": true,
                  "max_input_length": 50
                }
              }
            }
          }
        }
      }
    }
    

    您将告诉ElasticSearch对同一字段进行三次索引:一次用于全文搜索,一次用于精确匹配,一次用于建议。

    准确的搜索可以通过 term 这样的查询:

    GET producten_prd_5_test_new/_search
    {
      "query": {
        "term": {
          "hoofdtitel.keyword": "The Nature of the Firm in the Oil Industry"
        }
      }
    }
    

    为什么这个领域 exact_hoofdtitel 没有出现在返回的文档中?

    因为 抄本 不更改源:

    原始源字段将不会被修改以显示复制的 价值观。

    它工作得很像 _all 字段,允许您在一个虚字段中合并多个字段的值,并以特殊的方式对其进行分析。

    这样做有意义吗 抄本 index: false 字段?

    索引:假 该字段将不会被分析,也不会被搜索(如在您的示例中,该字段 exact_hoofdtitel.keyword )

    如果要对该字段进行关键字聚合,则这样做仍然有意义:

    GET producten_prd_5_test/_search
    {
      "aggs": {
        "by copy to": {
          "terms": {
            "field": "exact_hoofdtitel.keyword"
          }
        }
      }
    }
    

    这将返回如下内容:

    {
      "aggregations": {
        "by copy to": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "The Nature of the Firm in the Oil Industry",
              "doc_count": 1
            }
          ]
        }
      }
    }
    

    希望有帮助!