Условная сортировка обращений в Elasticsearch - PullRequest
1 голос
/ 16 июня 2020

У меня есть указатель с названиями дорог. Мои настройки выглядят так:

  "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. Bar Road
  2. Bar Foo Road
  3. Foo Road

Итак, когда я ищу «Foo», я получаю # 2 и # 3 в качестве совпадений. Это ожидается.

Но я хотел бы контролировать порядок попаданий. В этом случае я хотел бы иметь # 3 в качестве первого совпадения, потому что строка начинается с поискового запроса.

Можно ли сортировать совпадения по своему усмотрению?

1 Ответ

0 голосов
/ 16 июня 2020

Следующее основано на этом ответе, но было адаптировано к вашему варианту использования.

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"
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...