gpt4 book ai didi

search - Elasticsearch | copy_to 部分搜索

转载 作者:行者123 更新时间:2023-11-29 02:51:54 24 4
gpt4 key购买 nike

我的 copy_to 可以正常工作以进行精确匹配,但我无法通过部分匹配正确设置它。以下是我的映射/设置和查询以及预期和实际结果。

设置:

{
"test": {
"settings": {
"index": {
"analysis": {
"filter": {
"ngram_filter": {
"type": "edge_ngram",
"min_gram": "1",
"max_gram": "15"
}
},
"analyzer": {
"ngram_analyzer": {
"filter": [
"lowercase",
"ngram_filter"
],
"type": "custom",
"tokenizer": "standard"
}
}
},
"number_of_shards": "1",
"number_of_replicas": "1",
}
}
}
}

映射:

POST /test/_mapping/name
{
"name": {
"properties": {
"vital": {
"properties": {
"first": {
"type": "string",
"copy_to": "full_name",
"term_vector": "yes",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
},
"last": {
"type": "string",
"copy_to": "full_name",
"term_vector": "yes",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
},
"full_name": {
"type": "string",
"term_vector": "yes",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
}
}
}
}
}
}

发布:

POST /test/name 
{
"vital": {
"first": "Tom",
"last": "Doe"
}
}

现在当我进行搜索时...

GET /test/name/_search
{
"query": {
"match": {
"full_name": {
"query": "Tom Doe",
"operator": "and"
}
}
}
}

...我得到了结果!! Hurrraaaay,但如果我进行搜索......

GET /test/name/_search
{
"query": {
"match": {
"full_name": {
"query": "Tom Do",
"operator": "and"
}
}
}
}

... 我没有得到任何结果 :( 我希望部分匹配也适用于 full_name。作为另一个原因,我成功地能够对名字和姓氏进行部分匹配。只是 full_name 不起作用. 我该怎么做?

最佳答案

你在映射中有一个小错误,你需要将名字和姓氏复制到 vital.full_name 字段中,而不仅仅是 full_name,否则会创建一个名为 full_name 的新字符串字段,在映射的顶层带有标准分析器(如果运行 GET test,您将在映射中看到该新字段):

POST /test/_mapping/name
{
"name": {
"properties": {
"vital": {
"properties": {
"first": {
"type": "string",
"copy_to": "vital.full_name", <--- fix this
"term_vector": "yes",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
},
"last": {
"type": "string",
"copy_to": "vital.full_name", <--- fix this
"term_vector": "yes",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
},
"full_name": {
"type": "string",
"term_vector": "yes",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
}
}
}
}
}
}

然后像这样修正你的查询:

POST /test/name/_search
{
"query": {
"match": {
"vital.full_name": { <-- fix this
"query": "Tom Doe",
"operator": "and"
}
}
}
}

POST /test/name/_search
{
"query": {
"match": {
"vital.full_name": { <-- fix this
"query": "Tom Do",
"operator": "and"
}
}
}
}

两者都会如您所愿地工作。

关于search - Elasticsearch | copy_to 部分搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41817487/

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