gpt4 book ai didi

json - Elasticsearch结合查询和过滤器无法提供正确的结果

转载 作者:行者123 更新时间:2023-12-03 00:23:11 24 4
gpt4 key购买 nike

我正在尝试使用额外的过滤器项来创建搜索页面,但是我无法使查询正常工作。

这是查询示例:

{
"size": 25,
"from": 0,
"sort": {
"_score": {
"order": "asc"
}
},
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"year": "2015"
}
}
]
}
},
"query": {
"match": {
"title": "Sense"
}
}
}
}
}

我只需要2015年的结果。即使标题为“Sense8”的行,搜索标题“Sense”也没有任何结果。如果我搜索Sense8,它将返回正确的数据,但不会返回“Sense”。

我究竟做错了什么?

谢谢

最佳答案

您可能需要在映射中使用 ngram edge ngram 分析器。我写了一篇关于在Qbox blog上使用ngrams自动完成功能的博客文章,其中涉及了一些细节,但是以下一些代码可能会为您提供所需的信息:

PUT /test_index
{
"settings": {
"analysis": {
"filter": {
"ngram_filter": {
"type": "edgeNGram",
"min_gram": 2,
"max_gram": 20,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
}
},
"analyzer": {
"ngram_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"asciifolding",
"ngram_filter"
]
},
"whitespace_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"year":{
"type": "string"
},
"title":{
"type": "string",
"index_analyzer": "ngram_analyzer",
"search_analyzer": "whitespace_analyzer"
}
}
}
}
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"year": "2015","title":"Sense8"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"year": "2014","title":"Something else"}

POST /test_index/_search
{
"size": 25,
"from": 0,
"sort": {
"_score": {
"order": "asc"
}
},
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"year": "2015"
}
}
]
}
},
"query": {
"match": {
"title": "Sense"
}
}
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.30685282,
"_source": {
"year": "2015",
"title": "Sense8"
},
"sort": [
0.30685282
]
}
]
}
}

您可以在浏览器中运行以下代码:

http://sense.qbox.io/gist/4f72c182db2017ac7d32077af16cbc3528cb79f0

关于json - Elasticsearch结合查询和过滤器无法提供正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28914334/

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