gpt4 book ai didi

elasticsearch - 用Elasticsearch进行全词搜索

转载 作者:行者123 更新时间:2023-12-03 01:04:04 31 4
gpt4 key购买 nike

刚开始使用Elasticsearch。并坚持第一个问题:

我的es索引中有以下字符串:

“收藏夹”,
“收藏夹\ a1”

如果我尝试仅先搜索

'query' => [
'match' => [
'name' => 'favourites'
]
],

要么
'filtered' => [
'filter' => [
'term' => [
'name' => 'favourites'
],
]
]

两者都匹配。怎么解决呢?

最佳答案

尝试在映射中使用 "index": "not_analyzed" ,在查询中使用term filter(或term query)。

例如,我可以像这样设置一个简单的索引:

PUT /test_index
{
"mappings": {
"doc": {
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

添加几个文档
POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name": "favourites"}
{"index":{"_id":2}}
{"name": "favourites\\a1"}

那么此查询将仅返回第一个文档:
POST /test_index/_search
{
"query": {
"term": {
"name": {
"value": "favourites"
}
}
}
}
...
{
"took": 45,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.30685282,
"_source": {
"name": "favourites"
}
}
]
}
}

这是我用于快速测试的一些代码:

http://sense.qbox.io/gist/accb3e9aedc43144a30bb96fd483115427c6c441

关于elasticsearch - 用Elasticsearch进行全词搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174038/

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