Full-text 通过复杂结构 Elasticsearch 进行搜索

Full-text search through complex structure Elasticsearch

如果在 Elasticsearch 中进行 full-text 搜索,我会遇到以下问题。我想搜索所有索引属性。但是,我的项目属性之一是一个非常复杂的 hashes/objects:

数组
[
  {
    "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"}
            }

          }

        }
      }
    }
  }
}

问题来了,如何正确的seek呢?对于没有嵌套的属性,它很有用,但是例如,我想在规范中按标题查找,没有返回结果。我都试过了:

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. 查询多层嵌套文档必须遵循certain schema.
  2. 不能同时对嵌套和非嵌套字段进行多重匹配and/or对不同路径下的嵌套字段进行查询。

您可以将查询包装在 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"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}