【问题标题】:Conditional sorting of hits in ElasticsearchElasticsearch 中命中的条件排序
【发布时间】:2020-06-17 17:06:08
【问题描述】:

我有一个包含道路名称的索引。我的设置如下所示:

  "settings": {
    "max_ngram_diff": 20,
    "analysis": {
      "analyzer": {
        "str_search_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase"
          ]
        },
        "str_index_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "substring"
          ]
        }
      },
      "filter": {
        "substring": {
          "type": "edgeNGram",
          "min_gram": 1,
          "max_gram": 255
        }
      }
    }
  }
}

在索引中我有这样的字符串:

  1. 酒吧路
  2. 巴富路
  3. 富路

所以当我搜索“Foo”时,我会得到 #2 和 #3 作为命中。这是意料之中的。

但我想控制点击的顺序。在这种情况下,我希望 #3 作为第一个命中,因为字符串以搜索词开头。

是否可以根据需要对匹配进行排序?

【问题讨论】:

    标签: elasticsearch elasticsearch-query


    【解决方案1】:

    以下内容基于this 的回答,但已根据您的用例进行了调整。

    PUT sorting
    {
      "mappings": {
        "properties": {
          "text": {
            "type": "text",
            "fields": {
              "analyzed": {
                "type": "text",
                "analyzer": "str_index_analyzer",
                "search_analyzer": "str_search_analyzer",
                "fielddata": true
              },
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      },
      "settings": {
        "max_ngram_diff": 20,
        "analysis": {
          "analyzer": {
            "str_search_analyzer": {
              "type": "custom",
              "tokenizer": "whitespace",
              "filter": [
                "lowercase"
              ]
            },
            "str_index_analyzer": {
              "type": "custom",
              "tokenizer": "whitespace",
              "filter": [
                "lowercase",
                "substring"
              ]
            }
          },
          "filter": {
            "substring": {
              "type": "edgeNGram",
              "min_gram": 1,
              "max_gram": 255
            }
          }
        }
      }
    }
    
    GET sorting/_search
    {
      "query": {
        "function_score": {
          "query": {
            "match": {
              "text.analyzed": "Foo"
            }
          },
          "functions": [
            {
              "script_score": {
                "script": {
                  "source": """
                    def docval = doc['text.keyword'].value;
                    def length = docval.length();
                    def index = (float) docval.indexOf('Foo');
    
                    // the sooner the word appears the better so 'invert' the 'index'
                    return index > -1 ? (1 / index) : 0;
                  """
                }
              }
            }
          ],
          "boost_mode": "sum"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 2017-01-09
      • 2023-01-09
      • 2017-04-13
      • 2021-11-19
      • 2014-11-30
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多