小编典典

如何在布尔查询中提升索引查询

elasticsearch

所以我想要实现的是与每个索引的自定义可搜索字段部分匹配。我生成一个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"首先具有的结果,然后搜索just "belden""cable"

此示例返回4个具有"belden cable"的结果,然后是仅具有的结果"cable",然后是个更多的结果"belden cable"

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

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

我也尝试过在match_phrase_prefixfor 内部使用boost语句,"belden cable"而不会改变结果。


阅读 322

收藏
2020-06-22

共1个答案

小编典典

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

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

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"
          }
        }
      ]
    }
  }
}
2020-06-22