0

如果在 Elasticsearch 中进行全文搜索,我会遇到以下问题。我想搜索所有索引属性。但是,我的项目属性之一是一个非常复杂的哈希/对象数组:

[
  {
    "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
4

1 回答 1

1
  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"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
于 2020-03-05T14:07:07.080 回答