gpt4 book ai didi

elasticsearch - 如何在 bool 查询中提升索引查询

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

所以我想要实现的是与每个索引的自定义可搜索字段部分匹配。
我生成了一个match_phrase_prefix并带有要搜索的值,如果该单词不止一个单词,则每个单词又生成一个单词(我可以使用prefix,但它有错误,或者具有未记录的设置)。

在这种情况下,我正在尝试查找"belden cable";查询如下所示:

{
"query":{
"bool":{
"should":
[
{
"indices":{
"indices":["addresss"],
"query":{
"bool":{
"should":
[
{"match_phrase_prefix":{"name":"BELDEN CABLE"}}
{"match_phrase_prefix":{"name":"BELDEN"}},
{"match_phrase_prefix":{"name":"CABLE"}}
]
}
},
"no_match_query":"none"
}
},
{
"indices":{
"indices":["customers"],
"query":{
"bool":{
"should":[
{"match_phrase_prefix":{"_all":"BELDEN CABLE"}},
{"match_phrase_prefix":{"_all":"CABLE"}},
{"match_phrase_prefix":{"_all":"BELDEN"}}
]
}
},
"no_match_query":"none"
}
}
]
}
}

我的目标搜索是首先获取具有 "belden cable"的结果,然后仅搜索 "belden""cable"

例如,这将返回4个具有 "belden cable"的结果,然后返回仅具有 "cable"的结果,然后返回更多 "belden cable"的结果。

如何增强具有完整搜索值的结果?(“屏蔽电缆”)

我尝试过分离单词和分离单词的索引查询,但是相关性最差。

我也尝试在 match_phrase_prefix内使用boost语句获取 "belden cable"而不改变结果。

最佳答案

您真正需要的是一种分析输入数据的不同方法。参见下文,这应该是最终解决方案的起点(因为您需要考虑查询和数据分析的全部要求)。使用ES进行搜索不仅涉及查询,而且还涉及的结构以及如何准备数据

这个想法是您希望对数据进行分析,以便belden cable保持原样。通过"name": {"type": "string"}的映射,正在使用standard分析器,这意味着索引中的术语列表是beldencable。您实际需要的是[belden cablebeldencable]。因此,我考虑过建议使用shingles token 过滤器。

DELETE /addresss
PUT /addresss
{
"settings": {
"analysis": {
"analyzer": {
"analyzer_shingle": {
"tokenizer": "standard",
"filter": [
"standard",
"lowercase",
"shingle"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"name": {
"type": "string",
"analyzer": "analyzer_shingle"
}
}
}
}
}
DELETE /customers
PUT /customers
{
"settings": {
"analysis": {
"analyzer": {
"analyzer_shingle": {
"tokenizer": "standard",
"filter": [
"standard",
"lowercase",
"shingle"
]
}
}
}
},
"mappings": {
"test": {
"_all": {
"analyzer": "analyzer_shingle"
}
}
}
}

POST /addresss/test/_bulk
{"index":{}}
{"name": "belden cable"}
{"index":{}}
{"name": "belden cable yyy"}
{"index":{}}
{"name": "belden cable xxx"}
{"index":{}}
{"name": "belden bla"}
{"index":{}}
{"name": "cable bla"}

POST /customers/test/_bulk
{"index":{}}
{"field1": "belden", "field2": "cable"}
{"index":{}}
{"field1": "belden cable yyy"}
{"index":{}}
{"field2": "belden cable xxx"}
{"index":{}}
{"field2": "belden bla"}
{"index":{}}
{"field2": "cable bla"}

GET /addresss,customers/test/_search
{
"query": {
"bool": {
"should": [
{
"indices": {
"indices": [
"addresss"
],
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"name": "BELDEN CABLE"
}
},
{
"match_phrase_prefix": {
"name": "BELDEN"
}
},
{
"match_phrase_prefix": {
"name": "CABLE"
}
}
]
}
},
"no_match_query": "none"
}
},
{
"indices": {
"indices": [
"customers"
],
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"_all": "BELDEN CABLE"
}
},
{
"match_phrase_prefix": {
"_all": "CABLE"
}
},
{
"match_phrase_prefix": {
"_all": "BELDEN"
}
}
]
}
},
"no_match_query": "none"
}
}
]
}
}
}

关于elasticsearch - 如何在 bool 查询中提升索引查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37215000/

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