gpt4 book ai didi

match - Elasticsearch :有什么方法可以将逗号分隔列表中以空格分隔的单词视为一个术语?

转载 作者:行者123 更新时间:2023-12-03 00:36:20 26 4
gpt4 key购买 nike

我不知道是否可行,但是我尝试使用“精确搜索”选项按位置进行搜索。有几个要搜索的字段,最重要的是“location_raw”字段:

"match": {
"location.location_raw": {
"type": "boolean",
"operator": "AND",
"query": "[location query]",
"analyzer": "standard"
}
}

location_raw字段是一个位置字符串,每个位置之间都有一个逗号,例如“Sudbury,Middlesex,Massachusetts”或“Leamington,Warwickshire,England”。如果有人搜索“Middlesex萨德伯里”,则将其作为
"query": "Sudbury Middlesex" 

并且这两个术语都必须存在于location_raw字段中。这部分有效。

问题在于,当location_raw字段包含多字位置时,例如New York或Saint George,当有人搜索“York”或“George”时,这些位置将返回。如果我精确搜索“乔治”,则不想获得“圣乔治”的结果。有什么方法可以使Elastic在字符串“Saint George,Stamford,Lincoln,England”中将“Saint George”视为一个术语?

最佳答案

这是一种方法,但是您也必须在csv中查询,或使用terms filter

我使用了一个简单的pattern analyzer模式:", "。我用一个文档设置了一个简单的索引:

PUT /test_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"analyzer": {
"csv": {
"type": "pattern",
"pattern": ", ",
"lowercase": false
}
}
}
},
"mappings": {
"doc": {
"properties": {
"location": {
"type": "string",
"index_analyzer": "csv",
"search_analyzer": "standard",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"location":"Saint George, Stamford, Lincoln, England"}

我可以看到使用简单的 terms aggregation生成的术语:
POST /test_index/_search?search_type=count
{
"aggs": {
"location_terms": {
"terms": {
"field": "location"
}
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": []
},
"aggregations": {
"location_terms": {
"buckets": [
{
"key": "England",
"doc_count": 1
},
{
"key": "Lincoln",
"doc_count": 1
},
{
"key": "Saint George",
"doc_count": 1
},
{
"key": "Stamford",
"doc_count": 1
}
]
}
}
}

然后,如果我使用相同的csv语法进行查询,则不会返回“英国乔治市”的文档:
POST /test_index/_search
{
"query": {
"match": {
"location": {
"type": "boolean",
"operator": "AND",
"query": "George, England",
"analyzer": "csv"
}
}
}
}
...
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

但适用于“英格兰圣乔治”:
POST /test_index/_search
{
"query": {
"match": {
"location": {
"type": "boolean",
"operator": "AND",
"query": "Saint George, England",
"analyzer": "csv"
}
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2169777,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.2169777,
"_source": {
"location": "Saint George, Stamford, Lincoln, England"
}
}
]
}
}

该查询是等效的,并且性能可能更高:
POST /test_index/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"terms": {
"location": [
"Saint George",
"England"
],
"execution": "and"
}
}
}
}
}

这是我用来测试的代码:

http://sense.qbox.io/gist/234ea93accb7b20ad8fd33e62fe92f1d450a51ab

关于match - Elasticsearch :有什么方法可以将逗号分隔列表中以空格分隔的单词视为一个术语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260967/

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