2016-10-07 8 views
1

は、我々は次の属性を持っていると言うElasticsearhの試合など多くの分野

{ some_data: {}, attributes: ["US", "Facebook"] } 
{ some_data: {}, attributes: ["Facebook"] } 
{ some_data: {}, attributes: ["male", "AR", "LinkedIn"] } 
{ some_data: {}, attributes: ["female", "US", "Facebook"] } 
{ some_data: {}, attributes: ["male", "US", "LinkedIn"] } 
{ some_data: {}, attributes: ["male", "US", "Facebook"] } 

私はElasticsearchがいることを、次のすべてを返すようにしたいです"属性"変数を正確に一致させます。例:この例では

1) attributes: ["male", "US", "Facebook"] # All attributes match 
2) attributes: ["male", "US"] # Two attributes combined match 
3) attributes: ["male", "Facebook"] # Two attributes combined match 
4) attributes: ["US", "Facebook"] # Two attributes combined match 
5) attributes: ["male"] # Only one matches 
6) attributes: ["US"] # Only one matches 
7) attributes: ["Facebook"] # Only one matches 

、私たちはなるだろう:

1) { some_data: {}, attributes: ["male", "US", "Facebook"] } # All match 
2) { some_data: {}, attributes: ["US", "Facebook"] } # Two matches 
3) { some_data: {}, attributes: ["Facebook"] } # One match 

2つのことを考慮する必要があります:

1)私は、性別に一致するすべてのフィールドを望んでいません=」男性'。私は最初に与えられたフィールドの組み合わせに正確に一致する結果だけを必要とします。 2)このアルゴリズムは、n個の要素に対して使用可能でなければなりません。この例では、すべてを単純化するために3を使用しましたが、照会する属性は30個あります。

そのため、データベースには1つのクエリしか持たないといいでしょう。

+0

[このドキュメント](HTTPS:/ /www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values.html#_equals_exactly)が参考になる場合があります。それはあなたが記述している質問の種類がなぜ難しいのか、とにかくそれを形成する方法を説明します。 – fylie

答えて

3

私が先にコメントした通り、tag_countフィールドを追加してそのようにクエリを実行するのが最も簡単です。目的の動作を得るには、(male AND tag_count=1) OR (male AND facebook AND tag_count=2)を指定します。これは、Elasticsearch DSLで SHOULD [(MUST male and tag_count=1) (MUST male and facebook and tag_count=2)]に変換されます。 (ORは必須で、ANDはANDである必要があります)。

明らかな理由から、これは30個のタグではうまく調整されませんが、これは正しい軌道に乗るでしょう。

{ "tags":["male"], "tag_count":1 } 
{ "tags":["male","facebook"], "tag_count":2 } 
{ "tags":["male","linkedin"], "tag_count":2 } 
{ "tags":["male","US", "facebook"], "tag_count":3 } 
{ "tags":["male","Germany", "facebook"], "tag_count":3 } 

そして、このクエリ:Elasticsearchに挿入し、次のデータで

{ 
    "query": { 
    "constant_score": { 
     "filter": { 
     "bool": { 
      "should": [ 
      { 
       "bool": { 
       "must": [ 
        { 
        "term": { 
         "tags": "male" 
        } 
        }, 
        { 
        "term": { 
         "tag_count": 1 
        } 
        } 
       ] 
       } 
      }, 
      { 
       "bool": { 
       "must": [ 
        { 
        "term": { 
         "tags": "male" 
        } 
        }, 
        { 
        "term": { 
         "tags": "facebook" 
        } 
        }, 
        { 
        "term": { 
         "tag_count": 2 
        } 
        } 
       ] 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

私は次のような結果を得る:

{ 
    "took" : 2, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 2, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "test_index", 
     "_type" : "mult_query", 
     "_id" : "AVegvUyzNutW6yNguPqZ", 
     "_score" : 1.0, 
     "_source" : { 
     "tags" : [ "male" ], 
     "tag_count" : 1 
     } 
    }, { 
     "_index" : "test_index", 
     "_type" : "mult_query", 
     "_id" : "AVegvPSFNutW6yNguPqX", 
     "_score" : 1.0, 
     "_source" : { 
     "tags" : [ "male", "facebook" ], 
     "tag_count" : 2 
     } 
    } ] 
    } 
} 
関連する問題