Полнотекстовый поиск по сложной структуре Elasticsearch - PullRequest
0 голосов
/ 05 марта 2020

У меня возникает следующая проблема в случае полнотекстового поиска в Elasticsearch. Я хотел бы найти все проиндексированные атрибуты. Однако один из моих атрибутов Project - это очень сложный массив хэшей / объектов:

[
  {
    "title": "Group 1 title",
    "name": "Group 1 name",
    "id": "group_1_id",
    "items": [
      {
        "pos": "1",
        "title": "Position 1 title"
      },
      {
        "pos": "1.1",
        "title": "Position 1.1 title",
        "description": "<p>description</p>",
        "extra_description": {
          "rotation": "2 years",
          "amount": "1.947m²"
        },
        "inputs": {
          "unit_price": true,
          "total_net": true
        },
        "additional_inputs": [
          {
            "name": "additonal_input_name",
            "label": "Additional input label:",
            "placeholder": "Additional input placeholder",
            "description": "Additional input description",
            "type": "text"
          }
        ]
      }
    ]
  }
]

Мои отображения выглядят так:

{:title=>{:type=>"text", :analyzer=>"english"},
:description=>{:type=>"text", :analyzer=>"english"},
:location=>{:type=>"keyword"},
:company=>{:type=>"keyword"},
:created_at=>{:type=>"date"},
:due_date=>{:type=>"date"},
:specification=>
 {:type=>:nested,
  :properties=>
   {:id=>{:type=>"keyword"},
    :title=>{:type=>"text"},
    :items=>
     {:type=>:nested,
      :properties=>
       {:pos=>{:type=>"keyword"},
        :title=>{:type=>"text"},
        :description=>{:type=>"text", :analyzer=>"english"},
        :extra_description=>{:type=>:nested, :properties=>{:rotation=>{:type=>"keyword"}, :amount=>{:type=>"keyword"}}},
        :additional_inputs=>
         {:type=>:nested,
          :properties=>
           {:label=>{:type=>"keyword"},
            :placeholder=>{:type=>"text"},
            :description=>{:type=>"text"},
            :type=>{:type=>"keyword"},
            :name=>{:type=>"keyword"}
            }

          }

        }
      }
    }
  }
}

Вопрос в том, как правильно искать через Это? Для вложенных атрибутов это не работает, но, например, я хотел бы искать по названию в спецификации, результат не возвращается. Я пробовал оба:

query:
   { nested:
      { 
        multi_match: {
          query: keyword,
          fields: ['title', 'description', 'company', 'location', 'specification']
        }
      }
  }

Или

  {
      nested: {
        path: 'specification',
        query: {
          multi_match: {
            query: keyword
          }
        }
      }
    }

Без какого-либо результата.

Редактировать: Это с elasticsearch-ruby для Ruby.

Я пытаюсь сделать запрос по: MODEL_NAME.all.search(query: with_specification("Group 1 title")), где with_specification:

def with_specification(keyword)
    {
        bool: {
          should: [
            {
              nested: {
                path: 'specification',
                query: {
                  bool: {
                    should: [
                      {
                        match: {
                          'specification.title': keyword,
                        }
                      },
                      {
                        multi_match: {
                          query: keyword,
                          fields: [
                            'specification.title',
                            'specification.id'
                          ]
                        }
                      },
                      {
                        nested: {
                          path: 'specification.items',
                          query: {
                            match: {
                              'specification.items.title': keyword,
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
  end

1 Ответ

1 голос
/ 05 марта 2020
  1. Запросы к многоуровневым вложенным документам должны выполняться по определенной схеме .
  2. Нельзя выполнить одновременное совпадение для вложенных и не вложенных полей одновременно и / или запрос к вложенным полям по разным путям.

Вы можете обернуть свои запросы в bool-should, но помните о 2 вышеизложенных правилах:

GET your_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "specification",
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "specification.title": "TEXT"     <-- standalone match
                    }
                  },
                  {
                    "multi_match": {                    <-- multi-match but 1st level path
                      "query": "TEXT",
                      "fields": [
                        "specification.title",
                        "specification.id"
                      ]
                    }
                  },
                  {
                    "nested": {
                      "path": "specification.items",   <-- 2nd level path
                      "query": {
                        "match": {
                          "specification.items.title": "TEXT"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
...