我在具有以下结构的ElasticSearch中创建了索引
{
"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"
}
}
]
}
}
这是我的数据:
http://localhost:9200/my_index2/_search?q=name:Trung+Quân
我用关键字“ TrungQuâ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"
}
}
]
}
}
结果如下:
{
"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"
}
}
]
}
}
它将检索包含单词“ Trung”或单词“Quân”的所有结果。现在,我必须如何配置索引,以便结果包含以下内容的“ Trung”和“Quân”:
from collections import Counter
import time
#10 sec
t_end = time.time() + 10
arr = []
while time.time() < t_end:
#print(time.time())
arr.append(random.randint(0,100))
arr = Counter(arr)
print(arr.most_common(3))
答案 0 :(得分:0)
在您的示例中,name
被映射为一个多字段,这意味着它在Elasticsearch中存储了两次:
name
)进行过一次优化,并且name.keyword
)如果您只对精确匹配搜索感兴趣,而对不是全文搜索感兴趣,则可以通过“固定”映射来实现。只需将name
映射为类型keyword
的字段并重新索引您的文档即可。映射字段name
:
"name": {
"type": "keyword"
}
如果要保留全文本搜索功能(在对name
字段进行查询时),应保留映射不变,并尝试以下查询之一:
#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"