# Расчет релевантности по триграммам

**URL:** <https://discuss.elastic.co/t/topic/183974>\
**Category:** Вопросы на русском языке\
**Created:** [June 3, 2019, 1:30pm UTC](https://discuss.elastic.co/t/topic/183974 "2019-06-03T13:30:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [June 3, 2019, 1:30pm UTC](https://discuss.elastic.co/t/topic/183974/1 "2019-06-03T13:30:11Z")

</div>

Добрый день! Я создаю справочник организаций, и пытаюсь использовать триграммы для корректировки ошибок и функционала "может быть вы имели ввиду?".

Однако, я получаю не совсем релевантные ответы, а именно, выдается не самое приближенное название. Например, есть сокращенное написание организации и полное, а при поиске короткого, наиболее релевантным считается полное. Я перерыл весь интернет и не нашел только вот [эту](http://elasticsearch-users.115913.n3.nabble.com/nGram-filter-and-relevance-score-td4071451.html) статью, но у меня почему-то не работает.

Помогите, пожалуйста, с настройкой релевантности?

Вот мой маппинг:

```
{
    "mappings": {
        "_doc": {
            "dynamic": "strict",
            "properties": {
                "role": {
                    "type": "keyword"
                },
                "id": {
                    "type": "long"
                },
                "fnsId": {
                    "type": "long"
                },
                "data": {
                    "type": "text",
                    "analyzer": "russian",
                    "fields": {
                        "edge_ngram": {
                            "type": "text",
                            "analyzer": "russian_edge_ngram"
                        },
                        "ngram": {
                            "type": "text",
                            "analyzer": "russian_ngram"
                        }
                    }
                },
                "suggest": {
                    "type": "completion",
                    "analyzer": "russian"
                },
                "okved": {
                    "type": "keyword"
                },
                "region": {
                    "type": "integer"
                },
                "address": {
                    "type": "text",
                    "index": false
                },
                "postalAddress": {
                    "type": "text",
                    "index": false
                },
                "sortField": {
                    "type": "integer"
                }
            }
        }
    },
    "settings": {
        "number_of_shards": 4,
        "number_of_replicas": 1,
        "index.refresh_interval": "0",
        "index.sort.field": "sortField",
        "index.sort.order": "desc",
        "analysis": {
            "char_filter": {
                "any_char_split": {
                    "pattern": "[^A-Za-z0-9а-яА-Я]",
                    "type": "pattern_replace",
                    "replacement": " "
                },
                "replace_e": {
                    "type": "mapping",
                    "mappings": [
                        "ё => е"
                    ]
                }
            },
            "tokenizer": {
                "ngram_tokenizer": {
                    "type": "ngram",
                    "min_gram": 3,
                    "max_gram": 3
                }
            },
            "analyzer": {
                "russian": {
                    "filter": [
                        "lowercase"
                    ],
                    "char_filter": [
                        "replace_e",
                        "any_char_split"
                    ],
                    "tokenizer": "standard"
                },
                "russian_ngram": {
                    "filter": [
                        "lowercase"
                    ],
                    "char_filter": [
                        "replace_e",
                        "any_char_split"
                    ],
                    "tokenizer": "ngram_tokenizer"
                }
            }
        }
    }
}

```

Вот запрос который я отправляю:

```
POST dictionary_organizations/_doc/_search
{
    "query": {"match" : {
            "data.ngram" : {
                "query" : "рога и копыта"
            }
        }}
}

```

И вот ответ:

```
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 4,
    "successful": 4,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1.7406245,
    "hits": [
      {
        "_index": "dictionary_organizations",
        "_type": "_doc",
        "_id": "s3",
        "_score": 1.7406245,
        "_source": {
          "data": "общество с ограниченной ответственностью рога и копыта"
        }
      },
      {
        "_index": "dictionary_organizations",
        "_type": "_doc",
        "_id": "s1",
        "_score": 1.7260926,
        "_source": {
          "data": "ооо рога и копыта"
        }
      },
      {
        "_index": "dictionary_organizations",
        "_type": "_doc",
        "_id": "s2",
        "_score": 1.4384104,
        "_source": {
          "data": "ооо только рога"
        }
      }
    ]
  }
}

```

Почему самым релевантным документом считается "общество с ограниченной ответственностью рога и копыта" а не "ооо рога и копыта"?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 7, 2019, 3:37pm UTC](https://discuss.elastic.co/t/topic/183974/2 "2019-06-07T15:37:52Z")

</div>

> [@IvanTushin](#):
>
> Почему самым релевантным документом считается "общество с ограниченной ответственностью рога и копыта" а не "ооо рога и копыта"?

Это потому, что документов мало, а шард слишком много, и аппроксимация частоты каждого термина происходит с большой погрешностью. Надо либо уменьшить количество шард до 1-й, либо искать с параметром [`?search_type=dfs_query_then_fetch`](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-request-search-type.html#dfs-query-then-fetch)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2019, 3:37pm UTC](https://discuss.elastic.co/t/topic/183974/3 "2019-07-05T15:37:52Z")

</div>

Closed on July 5, 2019, 3:37pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
