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

问题描述 投票:0回答:1

在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 full-text-search elasticsearch-model
1个回答
0
投票
  1. 查询多层嵌套文档必须遵循certain schema
  2. 您不能同时对嵌套字段和非嵌套字段进行多重匹配,并且/或者无法查询不同路径下的嵌套字段。

您可以将查询包装在布尔中,但请牢记上述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"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.