gpt4 book ai didi

elasticsearch - Elastic Search中的“standard”类型

转载 作者:行者123 更新时间:2023-12-03 02:36:45 25 4
gpt4 key购买 nike

我在ElasticSearch中使用以下结构创建了索引

{
"my_index2": {
"aliases": {},
"mappings": {
"properties": {
"age": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"settings": {
"index": {
"number_of_shards": "1",
"provided_name": "my_index2",
"creation_date": "1573022346897",
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"token_chars": [
"letter",
"digit",
"whitespace",
"punctuation",
"symbol"
],
"type": "standard"
}
}
},
"number_of_replicas": "1",
"uuid": "S6fjaa12QiqTFHrqjBE3KQ",
"version": {
"created": "7040299"
}
}
}
}
}

这是我的数据:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my_index2",
"_type": "_doc",
"_id": "ou57P24BbiLEFIdnhkkg",
"_score": 1.0,
"_source": {
"name": "Hung Le",
"age": "34"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "o-57P24BbiLEFIdnx0kj",
"_score": 1.0,
"_source": {
"name": "Viet Pham",
"age": "20"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "pO58P24BbiLEFIdnAUm-",
"_score": 1.0,
"_source": {
"name": "Quy Tran",
"age": "21"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "pe58P24BbiLEFIdnJ0mn",
"_score": 1.0,
"_source": {
"name": "Khiem Pham",
"age": "22"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "pu58P24BbiLEFIdngUnX",
"_score": 1.0,
"_source": {
"name": "PhamHoangViet",
"age": "21"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "p-59P24BbiLEFIdnPUma",
"_score": 1.0,
"_source": {
"name": "Nguyễn Trần Trung Quân",
"age": "21"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "qO59P24BbiLEFIdnbkkO",
"_score": 1.0,
"_source": {
"name": "Đặng Tấn Sĩ",
"age": "21"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "qe5_P24BbiLEFIdnDknH",
"_score": 1.0,
"_source": {
"name": "Văn Trung",
"age": "34"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "qu5_P24BbiLEFIdnZkm3",
"_score": 1.0,
"_source": {
"name": "Phạm Nguyễn Minh Quân",
"age": "34"
}
}
]
}
}

我用关键字“TrungQuân”搜索:
http://localhost:9200/my_index2/_search?q=name:Trung+Quân

结果如下:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 2.19989,
"hits": [
{
"_index": "my_index2",
"_type": "_doc",
"_id": "p-59P24BbiLEFIdnPUma",
"_score": 2.19989,
"_source": {
"name": "Nguyễn Trần Trung Quân",
"age": "21"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "qe5_P24BbiLEFIdnDknH",
"_score": 1.497693,
"_source": {
"name": "Văn Trung",
"age": "34"
}
},
{
"_index": "my_index2",
"_type": "_doc",
"_id": "qu5_P24BbiLEFIdnZkm3",
"_score": 1.099945,
"_source": {
"name": "Phạm Nguyễn Minh Quân",
"age": "34"
}
}
]
}
}

它检索包含单词“Trung”或单词“Quân”的所有结果。现在,我如何配置索引,以便结果包含“Trung”和“Quân”,如下所示:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.6052008,
"hits": [
{
"_index": "my_index2",
"_type": "_doc",
"_id": "p-59P24BbiLEFIdnPUma",
"_score": 2.6052008,
"_source": {
"name": "Nguyễn Trần Trung Quân",
"age": "21"
}
}

]
}
}

最佳答案

在您的示例中,name被映射为一个多字段,这意味着它在Elasticsearch中存储了两次:

  • 曾经针对全文搜索(称为name)和
  • 优化过
  • 曾经针对分析和精确匹配进行了优化(称为name.keyword)

  • 如果您只对精确匹配搜索和 而不是全文搜索感兴趣,则可以通过“固定”映射来实现。只需将 name映射为 keyword类型的字段并重新索引您的文档即可。映射字段 name:
    "name": {
    "type": "keyword"
    }

    如果要保留全文本搜索功能(在对 name -field进行查询时),应保持映射不变,并尝试以下查询之一:

    #1使用短语搜索的示例:
    (不需要完全匹配,但两个词都需要以正确的顺序/顺序匹配。使用双引号)
    http://localhost:9200/my_index2/_search?q=name:"Trung Quân"

    #2使用完全匹配的示例:
    (姓名必须从头到尾匹配,且大小写完全相同)
    http://localhost:9200/my_index2/_search?q=name.keyword:"Trung Quân"

    关于elasticsearch - Elastic Search中的“standard”类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727546/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com