2015-01-08 13 views
5

私はElasticsearchを自分のRailsアプリケーションに追加しようとします。 は、私は(彼らは1つだけのフィールドであるかのように)2つのフィールドにmulti_matchクエリを実行すると、エラーがある1ElasticSearch with multi_matchとbool

response = Wine.search({ 
      query: { 
       multi_match: { 
      query: "test", 
      fields: ["winery", "name"] 
      }, 
     bool: { 
      must: { 
      term: { status: 1 } 
      }, 
      should: [], 
      minimum_should_match: 1 
     } 
     }  
     }) 

に等しくなければなりません別のフィールド(ステータス)にフィルタを持っています:

"fields\":[\"winery\",\"name\"]},\"bool\":{\"must\":{\"term\":{\"status\":1}},\"should\":[],\"minimum_should_match\":1}}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"bool\"]; }]","status":400} 

リクエストに間違いがありますか?どのようにmulti_matchとBOOLを一緒に実行するのですか?

答えて

13

使用filtered query:Elasticsearch 5用

{ 
    "query": { 
     "filtered": { 
      "query": { 
       "multi_match": { 
        "query": "test", 
        "fields": [ 
         "winery", 
         "name" 
        ] 
       } 
      }, 
      "filter": { 
       "term": { 
        "status": "1" 
       } 
      } 
     } 
    } 
} 

同じクエリ:

{ 
    "query": { 
     "bool": { 
      "must": { 
       "multi_match": { 
        "query": "test", 
        "fields": [ 
         "winery", 
         "name" 
        ] 
       } 
      }, 
      "filter": { 
       "term": { 
        "status": "1" 
       } 
      } 
     } 
    } 
} 
+0

完璧な答え!ありがとう! –

+0

残念ながら、答えはもはやElasticSearch 5.xでは有効ではなく、更新されたドキュメントページもあまり役に立ちません。 multi_matchクエリをフィルタを使った新しい "bool"クエリに変換する方法を知っていますか? – Overbryd

+1

私はElasticserarch 5.xで動作するクエリを使って答えを更新しました。詳細については、boolクエリのdocs:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-queryを参照してください。 –