RoRのマルチマッチクエリに問題があります。私はElastic Searchを設定して動作させていますが、これまでのところ動作していると思われる集計を設定していますが、何らかの理由で集約しているフィールドを検索することができません。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
私は集計していますフィールドであることを起こる「occasion_name」の離れmulti_match_queryに指定されている、私はすべてのフィールドを検索することができています:これは私のモデルからの抽出物です。フィールドが正しく索引付けされていることを確認しました(弾性検索ヘッドプラグインを使用)。私はまた、集計されたoccasion_namesでファセットを私のビューに表示することができます。私は集計を削除し、occasion_nameで検索することを含む、私が考えることができるすべてを試みましたが、まだ運がありません。 (私はelasticsearch-rails gemを使用しています)
何か助けていただければ幸いです。
編集:
私はこれがレールからクエリを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のためにインデックス化されることを、すべての例であります - 私は)テストのためにこれを使用する:iがm
:2
{
"_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"
}
}
}
EDIT ... @rahulrocにより示唆されるような機会のための検索機能させるためにanagedが、今、私は何か他のものを検索することはできません
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
私はレールに精通していませんが、ESの経験はあります。 ElasticSearchに送信されるJSONクエリを表示できますか? – Cfreak
こんにちは、私はちょうど私の最初の投稿を編集する - 私はクエリを含めている。 Thanks – martini
occasion_nameはネストされたフィールドです。一致クエリで直接アクセスすることはできません – Rahul