2016-03-28 87 views
2

我在RoR中的多个匹配查询时遇到问题。我有弹性搜索配置和工作,但我正在设置聚合,迄今似乎工作,但无论出于什么原因,我无法搜索我正在聚合的领域。这是我的模型提取:Elasticsearch:嵌套字段上的多个匹配查询

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宝石)

任何帮助将不胜感激。

编辑:

我得到这个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:

我米anaged做出的场合进行搜索通过@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 

UPDATE:添加多个嵌套字段 - 我想添加的休息我的聚合,但我面临类似的问题和以前一样。我的最终目标是使用聚合作为过滤器,因此我需要为我的查询添加大约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 
+0

我不熟悉rails,但有一些ES经验。您能够显示发送到ElasticSearch的JSON查询吗? – Cfreak

+0

嗨,我只是编辑我的第一篇文章 - 我已包括查询。谢谢 – martini

+0

occasion_name是一个嵌套字段。它不能直接访问匹配查询 – Rahul

回答

3

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

"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"]    } 
      } 
     ] 
    } 
} 
+0

感谢您的建议,我设法根据编辑2进行搜索工作的场合,但现在我无法搜索其他任何东西 – martini

+0

其他字段不属于嵌套范围occassions。其他领域需要在各自的范围内。在bool-should子句中分别设置子句。我会在一段时间内提供完整的查询。 – Rahul

+0

我已更新完整查询的答案 – Rahul

相关问题