Выделение «лучших совпадений» с помощью Elastic Highlighting API - PullRequest
0 голосов
/ 15 февраля 2019

Можно ли выделить «лучшие совпадения» с помощью Elastic Highlighting API?Под «наилучшим соответствием» я подразумеваю точное совпадение в слове, даже если все слово соответствует запросу.Например:

  • содержимое документа - Dubai
  • , поисковый запрос - duba
  • , а желаемый результат - <b>Duba</b>i

Но проблема в моем запросе.В запросе есть несколько «нечетких» запросов.

Вот пример конфигурации индекса:

PUT /highlight_best_match
{
  "settings": {
    "number_of_shards": "1",
    "number_of_replicas": "1",
    "analysis": {
      "filter": {
        "language_stemmer": {
          "name": "german2",
          "type": "stemmer"
        },
        "language_stopwords": {
          "type": "stop",
          "stopwords": "_german_"
        }
      },
      "char_filter": {
        "ampersand_to_and": {
          "type": "mapping",
          "mappings": [
            "&=> and "
          ]
        }
      },
      "analyzer": {
        "prefix_analyzer": {
          "type": "custom",
          "tokenizer": "edge_ngram_tokenizer",
          "filter": [
            "german_normalization",
            "lowercase"
          ]
        },
        "match_analyzer": {
          "char_filter": [
            "html_strip",
            "ampersand_to_and"
          ],
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding",
            "language_stopwords",
            "language_stemmer"
          ]
        },
        "search_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "german_normalization",
            "lowercase"
          ]
        }
      },
      "tokenizer": {
        "edge_ngram_tokenizer": {
          "type": "edge_ngram",
          "min_gram": "2",
          "max_gram": "20",
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "default": {
      "dynamic": "false",
      "properties": {
        "id": {
          "type": "integer"
        },
        "title": {
          "type": "keyword",
          "fields": {
            "match": {
              "type": "text",
              "term_vector": "with_positions_offsets",
              "index_options": "offsets",
              "analyzer": "match_analyzer"
            },
            "prefix": {
              "type": "text",
              "term_vector": "with_positions_offsets",
              "index_options": "offsets",
              "analyzer": "prefix_analyzer",
              "search_analyzer": "search_analyzer"
            }
          }
        }
      }
    }
  }
}

и некоторые данные, иллюстрирующие пример:

POST /_bulk
{"create": {"_id": "1", "_index": "highlight_best_match", "_type": "default"} }
{"title": "Dubai"}
{"create": {"_id": "2", "_index": "highlight_best_match", "_type": "default"} }
{"title": "Dumai"}
{"create": {"_id": "3", "_index": "highlight_best_match", "_type": "default"} }
{"title": "Cuba"}
{"create": {"_id": "4", "_index": "highlight_best_match", "_type": "default"} }
{"title": "Kuba Südküste"}
{"create": {"_id": "5", "_index": "highlight_best_match", "_type": "default"} }
{"title": "Dubai Kreuzfahrt"}

Запрос

GET /highlight_best_match/_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "should": [
            {
              "match": {
                "title.prefix": {
                  "query": "duba",
                  "fuzziness": 1,
                  "boost": 1
                }
              }
            },
            {
              "match": {
                "title.match": {
                  "query": "duba",
                  "fuzziness": 1,
                  "boost": 1
                }
              }
            }
          ]
        }
      },
      "should": [
        {
          "match_phrase_prefix": {
            "title.match": {
              "query": "duba",
              "boost": 5
            }
          }
        },
        {
          "match": {
            "title.prefix": {
              "query": "duba",
              "fuzziness": 0,
              "boost": 3
            }
          }
        },
        {
          "match": {
            "title.match": {
              "query": "duba",
              "fuzziness": 0,
              "boost": 10
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "encoder": "plain",
    "order": "score",
    "pre_tags": [
      "<b>"
    ],
    "post_tags": [
      "</b>"
    ],
    "fields": {
      "title.prefix": {
        "type": "fvh",
        "matched_fields": [
          "title.match",
          "title.prefix"
        ]
      }
    }
  }
}

и результат

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 11.402948,
    "hits": [
      {
        "_index": "highlight_best_match",
        "_type": "default",
        "_id": "1",
        "_score": 11.402948,
        "_source": {
          "title": "Dubai"
        },
        "highlight": {
          "title.prefix": [
            "<b>Dubai</b>"
          ]
        }
      },
      {
        "_index": "highlight_best_match",
        "_type": "default",
        "_id": "5",
        "_score": 6.812179,
        "_source": {
          "title": "Dubai Kreuzfahrt"
        },
        "highlight": {
          "title.prefix": [
            "<b>Dubai</b> Kreuzfahrt"
          ]
        }
      },
      {
        "_index": "highlight_best_match",
        "_type": "default",
        "_id": "3",
        "_score": 1.5331156,
        "_source": {
          "title": "Cuba"
        },
        "highlight": {
          "title.prefix": [
            "<b>Cuba</b>"
          ]
        }
      },
      {
        "_index": "highlight_best_match",
        "_type": "default",
        "_id": "4",
        "_score": 1.0343978,
        "_source": {
          "title": "Kuba Südküste"
        },
        "highlight": {
          "title.prefix": [
            "<b>Kuba</b> Südküste"
          ]
        }
      },
      {
        "_index": "highlight_best_match",
        "_type": "default",
        "_id": "2",
        "_score": 0.7896109,
        "_source": {
          "title": "Dumai"
        },
        "highlight": {
          "title.prefix": [
            "<b>Duma</b>i"
          ]
        }
      }
    ]
  }
}

Пожалуйста, посмотрите на результаты с ID = 1 и ID = 5. Можно ли выделить только duba там как <b>Duma</b>i в результате с ID = 2?

Я знаю, что могу установить highlight_query только с точным запросом.Но я бы хотел выделить результаты в любом случае, но точные совпадения предпочтительнее, если они есть.

Заранее спасибо!

...