2017-06-29 9 views
0

辞書には、キーワード、マップされたキーワードとカテゴリフィルタのようなフィールドが含まれています。1つのクエリ内で複数のサブクエリelasticsearch

ユーザーがappleを検索すると、内部的にクエリは、下のクエリとしてそれぞれのカテゴリのマッピングされたキーワードを検索します。

SELECT * FROM products where (title= "*apple*" AND title="*iphone*" and category="smartphones") OR (title= "*apple*" AND title="*ipad*" and category="tablets") OR (title= "*apple*" AND title="*watch*" and category="smart watches") 

以下は対応する弾性検索クエリです。

{ 
    "query": { 
     "bool": { 
     "should": [ 
      { 
       "bool": { 
        "must": [ 
        { 
         "match" : { 
          "title" : { 
           "query" : "apple iphone", 
           "operator" : "and" 
          } 
         } 
        }, 
        { 
         "term": { 
          "category.raw": "smartphones" 
         } 
        } 
        ] 
       } 
      }, 
      { 
       "bool": { 
        "must": [ 
        { 
         "match" : { 
          "title" : { 
           "query" : "apple watch", 
           "operator" : "and" 
          } 
         } 
        }, 
        { 
         "term": { 
          "category.raw": "smartwatch" 
         } 
        } 
        ] 
       } 
      }, 
      { 
       "bool": { 
        "must": [ 
        { 
         "match" : { 
          "title" : { 
           "query" : "apple ipad", 
           "operator" : "and" 
          } 
         } 
        }, 
        { 
         "term": { 
          "category.raw": "tablets" 
         } 
        } 
        ] 
       } 
      } 
     ], 
     "minimum_should_match": 1 
     } 
    } 
} 
  • 上記のクエリを右か?上記のクエリで必要な変更はありますか?
  • elasticsearchで各サブクエリの上位10個の結果を得る方法はありますか?

答えて

0

はい、私の知る限り、クエリは正常に見えます。 "minimum_should_match": 1は本当に必要ではありませんが、これがデフォルトの動作です。

function_scoreクエリ(多分script_score)を使用してそのようなロジックを課すことができるかもしれませんが、より良い方法は3つの異なるクエリを実行してそれぞれの結果を得ることです。 1回のリクエストで複数のクエリを実行する場合は、Multi Search APIを使用してクエリを実行できます。

関連する問題