gpt4 book ai didi

elasticsearch - ElasticSearch分析器,允许带和不带连字符的查询

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

您如何构造一个分析器,使您可以查询带连字符和不带连字符的字段?

以下两个查询必须返回同一个人:

{
"query": {
"term": {
"name": {
"value": "Jay-Z"
}
}
}
}

{
"query": {
"term": {
"name": {
"value": "jay z"
}
}
}
}

最佳答案

您可以做的是使用mapping character filter来用空格替换连字符。基本上是这样的:

curl -XPUT localhost:9200/tests -d '{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "keyword",
"filter": [
"lowercase"
],
"char_filter": [
"hyphens"
]
}
},
"char_filter": {
"hyphens": {
"type": "mapping",
"mappings": [
"-=>\\u0020"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"name": {
"type": "string",
"analyzer": "my_analyzer"
}
}
}
}
}'

然后,我们可以使用 _analyze端点检查分析管道将产生什么:

对于 Jay-Z:
curl -XGET 'localhost:9200/tests/_analyze?pretty&analyzer=my_analyzer' -d 'Jay-Z'
{
"tokens" : [ {
"token" : "jay z",
"start_offset" : 0,
"end_offset" : 5,
"type" : "word",
"position" : 0
} ]
}

对于 jay z:
curl -XGET 'localhost:9200/tests/_analyze?pretty&analyzer=my_analyzer' -d 'jay z'
{
"tokens" : [ {
"token" : "jay z",
"start_offset" : 0,
"end_offset" : 5,
"type" : "word",
"position" : 0
} ]
}

正如您所看到的,两种形式的索引都将被索引,因此 term查询也将适用于两种形式。

关于elasticsearch - ElasticSearch分析器,允许带和不带连字符的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847492/

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