elasticsearch - 通过复杂结构进行全文搜索Elasticsearch

标签 elasticsearch full-text-search elasticsearch-model

在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
          }
        }
      }
    }

没有任何结果。

编辑:
它与Ruby的elasticsearch-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

最佳答案

  • 在多层嵌套文档上的查询必须遵循certain schema
  • 您不能同时对嵌套字段和非嵌套字段进行多重匹配和/或查询不同路径下的嵌套字段。

  • 您可以将查询包装在 bool(boolean) 中,但请牢记上述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"
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    关于elasticsearch - 通过复杂结构进行全文搜索Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60545794/

    相关文章:

    php - Elasticsearch 5.5使用CURL获取错误 'not_x_content_exception'

    elasticsearch - 为什么在 Elasticsearch 中无法按地理位置查询?

    php - Symfony Elasticsearch准则

    mysql - 如何操纵 MySQL 全文搜索相关性以使一个字段比另一个字段更多 'valuable'?

    ruby-on-rails - Rails __elasticsearch__.create_index! "Root mapping definition has unsupported parameters(mapper_parsing_exception)"

    ruby-on-rails - 在 Rails 中使用 elasticsearch-model 保存特定字段

    elasticsearch - 找不到[@timestamp]的映射以对logstash进行排序

    javascript - 确定我的内容中是否包含搜索词数组的最快方法?

    elasticsearch - Elasticsearch 优先搜索以搜索词开头的结果