【问题标题】:Multi field text and keyword fields in elasticsearchelasticsearch中的多字段文本和关键字字段
【发布时间】:2017-09-21 09:45:52
【问题描述】:

我正在考虑从 solr 切换到 elasticsearch,并在没有提供架构/映射的情况下将一堆文档索引到其中,并且我之前在 solr 中设置为索引字符串的许多字段已设置为两者textkeyword 字段使用 multi-fields

使用multi-fieldskeyword 字段也作为text 字段有什么好处?在我的情况下,字段中的大多数值都是单个单词,所以我想它们是否被发送到分析器并不重要,但 es 文档似乎暗示 keyword 字段在搜索时不被考虑或至少被不同地对待?

如果我搜索“ipad”这个词,如果文档在关键字字段和其他一些文本字段中包含“ipad”,与没有关键字字段的同一文档相比,文档的得分会更高一点。 ?如果说“ipad”仅在关键字字段中,文档是否仍然匹配?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    为了回答我自己的问题,我创建了一个快速测试,搜索时几乎所有关键字和文本字段都是等效的,并且多字段似乎与它们的主要类型获得相同的分数,所以我猜第二个字段对搜索评分没有影响

    奇怪的是,关键字和文本字段中的多字值得到了相同的分数,我本来希望关键字字段得分较低或根本没有,但出于我的目的,这很好,所以我不打算进一步调查.

    索引创建

    PUT test_index
    {
        "settings" : {
            "number_of_shards" : 1
        },
        "mappings" : {
            "test_type" : {
                "properties" : {
                    "multifield": {
                      "type": "text",
                      "fields": {
                         "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                         }
                      }
                    },
    
                    "keywordfield": {
                      "type": "keyword"
                    },
    
                    "textfield": {
                      "type": "text"
                    }
    
                }
            }
        }
    }
    

    数据插入

    POST /_bulk
    { "update": { "_index": "test_index", "_type": "test_type", "_id": 1 }
    { "doc" : { "multifield" : "ipad"  }, "doc_as_upsert" : true }
    { "update": { "_index": "test_index", "_type": "test_type", "_id": 2 }
    { "doc" : { "keywordfield" : "ipad"  }, "doc_as_upsert" : true }
    { "update": { "_index": "test_index", "_type": "test_type", "_id": 3 }
    { "doc" : { "keywordfield" : "a green ipad"  }, "doc_as_upsert" : true }
    { "update": { "_index": "test_index", "_type": "test_type", "_id": 4 }
    { "doc" : { "textfield" : "a yellow ipad"  }, "doc_as_upsert" : true }
    { "update": { "_index": "test_index", "_type": "test_type", "_id": 5 }
    { "doc" : { "keywordfield" : "ipad", "textfield" : "ipad"  }, "doc_as_upsert" : true }
    { "update": { "_index": "test_index", "_type": "test_type", "_id": 6 }
    { "doc" : { "keywordfield" : "unrelated", "textfield" : "hopefully this wont show up"  }, "doc_as_upsert" : true }
    { "update": { "_index": "test_index", "_type": "test_type", "_id": 7 }
    { "doc" : { "textfield" : "ipad"  }, "doc_as_upsert" : true }
    

    结果

    GET /test_index/_search?q=ipad
    {
       "took": 1,
       "timed_out": false,
       "_shards": {
          "total": 1,
          "successful": 1,
          "failed": 0
       },
       "hits": {
          "total": 6,
          "max_score": 0.28122374,
          "hits": [
             {
                "_index": "test_index",
                "_type": "test_type",
                "_id": "5",
                "_score": 0.28122374,
                "_source": {
                   "keywordfield": "ipad",
                   "textfield": "ipad"
                }
             },
             {
                "_index": "test_index",
                "_type": "test_type",
                "_id": "1",
                "_score": 0.2734406,
                "_source": {
                   "multifield": "ipad"
                }
             },
             {
                "_index": "test_index",
                "_type": "test_type",
                "_id": "2",
                "_score": 0.2734406,
                "_source": {
                   "keywordfield": "ipad"
                }
             },
             {
                "_index": "test_index",
                "_type": "test_type",
                "_id": "7",
                "_score": 0.2734406,
                "_source": {
                   "textfield": "ipad"
                }
             },
             {
                "_index": "test_index",
                "_type": "test_type",
                "_id": "3",
                "_score": 0.16417998,
                "_source": {
                   "keywordfield": "a green ipad"
                }
             },
             {
                "_index": "test_index",
                "_type": "test_type",
                "_id": "4",
                "_score": 0.16417998,
                "_source": {
                   "textfield": "a yellow ipad"
                }
             }
          ]
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 2021-10-15
      • 2023-03-30
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多