# Синонимы и подстроки

**URL:** https://discuss.elastic.co/t/topic/104899
**Category:** Вопросы на русском языке
**Created:** [October 23, 2017, 12:56pm UTC](https://discuss.elastic.co/t/topic/104899 "2017-10-23T12:56:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![40a4b7c586dcf75bec98](https://avatars.discourse-cdn.com/v4/letter/4/a88e57/32.png) [@40a4b7c586dcf75bec98](https://discuss.elastic.co/u/40a4b7c586dcf75bec98)
#### Post date: [October 23, 2017, 12:56pm UTC](https://discuss.elastic.co/t/topic/104899/1 "2017-10-23T12:56:05Z")

</div>

Доброго времени суток!  
Настраиваю elasticSearch для поиска по базе мероприятий. Поиск нужен по полному слову и по подстрокам тоже. Настроил анализаторы и маппинг:

> ```
> { 
> "template" : "*", 
> "settings" : { 
> "analysis" : {
> "analyzer" : {
> "russian_indexer" : {
> "type" : "custom",
> "tokenizer" : "rus_nGram",
> "filter" : [
> "russian_morphology",
> "rus_snow",
> "rus_stop",
> "rus_delim",
> "lowercase"
> ]
> },
> "russian_searcher" : {
> "type" : "custom",
> "tokenizer" : "letter",
> "filter" : [
> "russian_morphology",
> "rus_snow",
> "rus_stop",
> "rus_delim",
> "lowercase"
> ]
> }
> },
> "tokenizer" : {
> "rus_nGram" : {
> "type": "nGram",
> "min_gram": 3,
> "max_gram": 8
> }
> },
> "filter" : {
> "rus_snow" : {
> "type" : "snowball",
> "language" : "Russian"
> },
> "rus_stop": {
> "type": "stop",
> "language": "_russian_"
> },
> "rus_delim": {
> "type": "word_delimiter"
> }
> }
> } 
> }, 
> "mappings" : { 
> "_default_" : { 
> "_all" : { "enabled" : false }, 
> "properties" : { 
> "content" : {
> "type" : "text",
> "analyzer" : "russian_indexer",
> "search_analyzer" : "russian_searcher"
> },
> "title" : {
> "type" : "text",
> "analyzer" : "russian_indexer",
> "search_analyzer" : "russian_searcher"
> },
> "full_info" : {
> "type" : "text",
> "analyzer" : "russian_indexer",
> "search_analyzer" : "russian_searcher"
> },
> "short_info" : {
> "type" : "text",
> "analyzer" : "russian_indexer",
> "search_analyzer" : "russian_searcher"
> },
> "role" : {
> "type" : "text",
> "analyzer" : "russian_indexer",
> "search_analyzer" : "russian_searcher"
> },
> "name" : {
> "type" : "text",
> "analyzer" : "russian_indexer",
> indent preformatted text by 4 spaces "search_analyzer" : "russian_searcher"
> },
> "created_at" : {
> "type": "date"
> }
> } 
> } 
> } 
> }
> 
> ```

Настроил морфологию и ngram.  
Из кода php обращаю к Эластику:

> ```
> $params = [
> 'index' => ['shows'],
> 'type' => ['show'],
> 'explain' => true,
> 'body' => [
> 'query' => [
> 'function_score' => [
> 'query' => [
> "dis_max" => [
> "queries" => [
> [
> 'multi_match' => (object)[
> 'query' => $searchQuery,
> 'type' => 'best_fields',
> 'fields' => ['name^10', 'short_info^5', 'full_info', 'content', 'title^10', 'role^5'],
> "boost" => 10
> ]
> ],
> [
> 'multi_match' => (object)[
> 'query' => $searchQuery,
> 'fields' => ['name^10', 'short_info^5', 'full_info', 'content', 'title^10', 'role^5'],
> "analyzer" => "russian_searcher",
> "boost" => 1
> ]
> ]
> ],
> ]
> ]
> ],
> ]
> ]
> ];
> 
> ```

Но результаты меня не удовлетворяют. Мне нужно, чтобы boost=10 применялся только тогда, когда произошло совпадение по синониму.  
Если я правильно понимаю, мне нужно создать один анализатор для ngram, а один - для морфологии. Сейчас, если я хочу найти "Красную шапочку" по запросу "Красный", он находит подстроку "Красн" и дает ей boost=10 но в explain'е пишет, что это подстрока (как nGram).

Надеюсь на помощь, вывод самого эластика могу предоставить

---

<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: [October 30, 2017, 12:47pm UTC](https://discuss.elastic.co/t/topic/104899/2 "2017-10-30T12:47:20Z")

</div>

Для того чтобы понять, почему это не работает, вам надо разобраться с тем, как работают фильтры в анализаторе. Я бы посоветовал начать с прочтения [Dealing with Human Language](https://www.elastic.co/guide/en/elasticsearch/guide/current/languages.html) в руководстве.

Вкратце, для того что бы искать и по подстроке и по корням слов, не надо сваливать все в один анализатор. Надо создать два анализатора, проиндексировать слова каждым в отдельные поля ([multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/multi-fields.html)), и искать сразу по нескольким полям с boost-ом.

---

<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: [November 27, 2017, 12:47pm UTC](https://discuss.elastic.co/t/topic/104899/3 "2017-11-27T12:47:32Z")

</div>

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