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

时间:2016-05-13 16:26:40

标签: elasticsearch solr-boost

因此,我想要实现的是与每个索引的自定义可搜索字段进行部分匹配。 我生成一个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"的搜索结果。

这会返回(例如)包含"belden cable"的4个结果,然后只有"cable"的结果,然后是"belden cable"的更多结果。

如何提升具有完整搜索价值的结果?(" belden cable")

我尝试将单词和分隔单词的索引查询分开,但它会产生最差的相关结果。

此外,我已尝试在match_phrase_prefix "belden cable"内使用提升语句而不更改结果。

1 个答案:

答案 0 :(得分: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"
          }
        }
      ]
    }
  }
}
相关问题