【问题标题】:Elasticsearch : Multi match query on nested fieldsElasticsearch:嵌套字段的多匹配查询
【发布时间】:2016-03-30 16:31:16
【问题描述】:

我在 RoR 中遇到多匹配查询问题。我已经配置并工作了 Elastic Search,但是我正在设置迄今为止似乎有效的聚合,但无论出于何种原因,我都无法在我正在聚合的字段上进行搜索。这是我的模型的摘录:

    settings :index => { :number_of_shards => 1 } do
        mapping do
          indexes :id, index: :not_analyzed
          indexes :name
          indexes :summary
          indexes :description

          indexes :occasions, type: 'nested' do
            indexes :id, type: 'integer'
            indexes :occasion_name, type: 'string', index: :not_analyzed

           ... 

          end
        end
      end




  def as_indexed_json(options = {})
    self.as_json(only: [:id, :name, :summary, :description],
                 include: {
                     occasions:           { only: [:id, :occasion_name] },
                     courses:             { only: [:id, :course_name] },
                     allergens:           { only: [:id, :allergen_name] },
                     cookingtechniques:   { only: [:id, :name] },
                     cuisine:             { only: [:id, :cuisine_name]}
                 })
  end


class << self
    def custom_search(query)
      __elasticsearch__.search(query: multi_match_query(query), aggs: aggregations)
    end


    def multi_match_query(query)
      {
          multi_match:
          {
              query: query,
              type: "best_fields",
              fields: ["name^9", "summary^8", "cuisine_name^7", "description^6", "occasion_name^6", "course_name^6", "cookingtechniques.name^5"],
              operator: "and"
          }
      }
    end

我可以搜索 multi_match_query 中指定的所有字段,“occasion_name”恰好是我正在聚合的字段。我已检查该字段是否已正确编入索引(使用弹性搜索头插件)。我还可以在我的视图中显示带有聚合的场合名称的方面。我尝试了所有我能想到的方法,包括删除聚合和搜索场合名称,但仍然没有运气。 (我正在使用 elasticsearch-rails gem)

任何帮助将不胜感激。

编辑:

我从 rails 得到了这个 ES 查询:

@search=
  #<Elasticsearch::Model::Searching::SearchRequest:0x007f91244df460
   @definition=
    {:index=>"recipes",
     :type=>"recipe",
     :body=>
      {:query=>
        {:multi_match=>
          {:query=>"Christmas",
           :type=>"best_fields",
           :fields=>["name^9", "summary^8", "cuisine_name^7", "description^6", "occasion_name^6", "course_name^6", "cookingtechniques.name^5"],
           :operator=>"and"}},
       :aggs=>
        {:occasion_aggregation=>
          {:nested=>{:path=>"occasions"}, :aggs=>{:id_and_name=>{:terms=>{:script=>"doc['occasions.id'].value + '|' + doc['occasions.occasion_name'].join(' ')", :size=>35}}}}}}},

这是我用于测试的 1 个虚拟食谱的所有索引示例(内容毫无意义 - 我仅将其用于测试):

{
"_index": "recipes",
"_type": "recipe",
"_id": "7",
"_version": 1,
"_score": 1,
"_source": {
"id": 7,
"name": "Mustard-stuffed chicken",
"summary": "This is so good we'd be surprised if this chicken fillet recipe doesn't become a firm favourite. Save it to your My Good Food collection and enjoy",
"description": "Heat oven to 200C/fan 180C/gas 6. Mix the cheeses and mustard together. Cut a slit into the side of each chicken breast, then stuff with the mustard mixture. Wrap each stuffed chicken breast with 2 bacon rashers – not too tightly, but enough to hold the chicken together. Season, place on a baking sheet and roast for 20-25 mins.",
"occasions": [
{
"id": 9,
"occasion_name": "Christmas"
}
,
{
"id": 7,
"occasion_name": "Halloween"
}
,
{
"id": 8,
"occasion_name": "Bonfire Night"
}
,
{
"id": 10,
"occasion_name": "New Year"
}
],
"courses": [
{
"id": 9,
"course_name": "Side Dish"
}
,
{
"id": 7,
"course_name": "Poultry"
}
,
{
"id": 8,
"course_name": "Salad"
}
,
{
"id": 10,
"course_name": "Soup"
}
],
"allergens": [
{
"id": 6,
"allergen_name": "Soya"
}
,
{
"id": 7,
"allergen_name": "Nut"
}
,
{
"id": 8,
"allergen_name": "Other"
}
,
{
"id": 1,
"allergen_name": "Dairy"
}
],
"cookingtechniques": [
{
"id": 15,
"name": "Browning"
}
],
"cuisine": {
"id": 1,
"cuisine_name": "African"
}
}
}

编辑2:

按照@rahulroc 的建议,我设法使搜索适用于某些场合,但现在我无法搜索其他任何东西...

    def multi_match_query(query)
  {
      nested:{
           path: 'occasions',
          query:{
      multi_match:
      {
          query: query,
          type: "best_fields",
          fields: ["name^9", "summary^8", "cuisine_name^7", "description^6", "occasion_name^6", "course_name^6", "cookingtechniques.name^5"],
          operator: "and"
      }
    }

      }
  }
end

更新:添加多个嵌套字段 - 我正在尝试添加其余的聚合,但我面临与以前类似的问题。我的最终目标是将聚合用作过滤器,因此我需要在查询中添加大约 4 个嵌套字段(我也希望这些字段可搜索)这是@rahulroc 提供的工作查询+添加另一个我无法搜索的嵌套字段。和以前一样,在索引方面一切正常,我可以显示新添加字段的聚合,但我无法搜索它。我尝试了此查询的不同变体,但无法使其工作(其余字段仍在工作且可搜索 - 问题只是新字段):

 def multi_match_query(query)
      {

        bool: {
            should: [
                {
                    nested:{
                        path: 'occasions',
                        query: {
                            multi_match:
                            {
                                      query: query,
                                      type: "best_fields",
                                      fields: ["occasion_name"]

                                  }
                        }
                    }
                },

                {
                    nested:{
                        path: 'courses',
                        query: {
                            multi_match:
                                {
                                    query: query,
                                    type: "best_fields",
                                    fields: ["course_name"]

                                }
                        }
                    }
                },

                {
                    multi_match: {
                        query: query,
                        fields:["name^9", "summary^8", "cuisine_name^7", "description^6"],

                    }
                }
            ]
        }


      }
    end

【问题讨论】:

  • 我对 Rails 不熟悉,但有一些 ES 经验。您能否显示发送到 ElasticSearch 的 JSON 查询?
  • 嗨,我刚刚编辑了我的第一篇文章 - 我已经包含了查询。谢谢
  • occasion_name 是一个嵌套字段。在匹配查询中不能直接访问

标签: ruby-on-rails elasticsearch


【解决方案1】:

您需要创建一个单独的嵌套子句来匹配嵌套字段

"query": {
    "bool": {
        "should": [
            {
                "nested": {
                    "path": "occassions",
                    "query": {
                        "multi_match": {
                            "query": "Christmas",
                            "fields": ["occassion_name^2"]
                        }
                    }
                } 
            },
            {
                "multi_match": {
                    "query": "Christmas",
                    "fields":["name^9", "summary^8", "cuisine_name^7", "description^6","course_name^6"]                }
            }
        ]
    }
}

【讨论】:

  • 感谢您的建议,我设法根据编辑 2 使搜索适用于某些场合,但现在我无法搜索其他任何内容
  • 其他字段不属于嵌套范围。其他字段需要在各自的范围内。在 bool-should 子句中制作单独的子句。我会在一段时间内提供完整的查询。
  • 我冒着烦人的风险,但我再次需要帮助......我尝试按照@rahulroc 描述的方法添加我的其他聚合 - 添加另一个 { 嵌套:{path:'courses', query: {multi_match: {query: query,type: "best_fields", fields: ["course_name"]}}}} 在 "should" 子句中,但新添加的嵌套字段不可搜索。我检查了 ElasticSearch Definitive Guide,但只有一个嵌套字段的示例。在 StackOverflow 上的另一个问题中,有人提供了一个查询,与我使用多个嵌套字段的方式完全相同,但我的不起作用......
  • 我刚刚用新的查询更新了它。感谢您的帮助!
  • 嵌套字段不需要是occassions.occassion_name^2吗? (即带有occassions. 前缀)
猜你喜欢
  • 2017-12-03
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
相关资源
最近更新 更多