2017-11-15 8 views
0

インデックスには、locationsのプロパティが "入れ子になっています"というマッピングがあります。このプロパティには場所の配列が含まれます。ネストされたプロパティ内に指定されたオブジェクトのANYを持つ文書をElasticsearchで選択します。

{ 
    "id": string, 
    "type": string 
} 

私のウェブサイト上で、私は複数の場所を選択して、私が選択したこれらの場所のANYを持っているすべての文書を検索する:それぞれの場所には、次のような構造を持つオブジェクトです。私は1つの場所で検索することができましたが、何とか多くの場所を検索するためにクエリを変更する必要があります。

私は、次のインデックスが設定されている:

"mappings": { 
    "users": { 
     "properties": { 
      "locations": { 
       "type": "nested", 
       "properties": { 
        "id": { 
         "type": "text", 
         "fields": { 
          "keyword": { 
           "type": "keyword", 
           "ignore_above": 256 
          } 
         } 
        }, 
        "type": { 
         "type": "text", 
         "fields": { 
          "keyword": { 
           "type": "keyword", 
           "ignore_above": 256 
          } 
         } 
        } 
       } 
      }, 
      "id": { 
       "type": "long" 
      } 
     } 
    } 
} 

例の文書:

{ 
    "locations": [ 
     { 
      "id": "UA", 
      "type": "country" 
     }, 
     { 
      "id": "RU", 
      "type": "country" 
     }, 
     { 
      "id": "OC", 
      "type": "continent" 
     } 
    ] 
} 
場所でユーザーを検索する

マイクエリ:

{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "nested": { 
         "path": "locations", 
         "query": { 
          "bool": { 
           "must": [ 
            { 
             "match": { "locations.id": "RU" } 
            }, 
            { 
             "match": { "locations.type": "country" } 
            } 
           ] 
          } 
         } 
        } 
       } 
      ] 
     } 
    } 
} 

EDIT: 私はまた、ドキュメンタリーを検索したいlocations.typecontinentに等しい。 SQLでは、複数の値検索の使用Termsために、使用するshouldhereを参照)OR条件

WHERE (id IN ('UA', 'RU') AND type = 'country') 
OR (id IN ('EU', 'OC', 'NA') AND type = 'continent') 

答えて

1

次のようになります。

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "locations", 
      "query": { 
       "bool": { 
       "should": [ 
        { 
        "bool": { 
         "must": [ 
         { 
          "terms": { 
          "locations.id": [ 
           "UA", 
           "RU" 
          ] 
          } 
         }, 
         { 
          "term": { 
          "locations.type": "country" 
          } 
         } 
         ] 
        } 
        }, 
        { 
        "bool": { 
         "must": [ 
         { 
          "terms": { 
          "locations.id": [ 
           "EU", 
           "OC" 
          ] 
          } 
         }, 
         { 
          "term": { 
          "locations.type": "continent" 
          } 
         } 
         ] 
        } 
        } 
       ] 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
+0

はおかげで、私もlocations.typeを持つ文書を盗んしたいです「大陸」に等しい。 SQLでは、 'WHERE(ID IN( 'UA'、 'RU')AND type = 'country')のように見えるでしょう。OR(id IN( 'EU'、 'OC'、 'NA')AND type = 'continent' ) '私はそれをどのように達成できますか? – starky

+0

私の回答を更新します – Eli

+0

ありがとう!それがまさに私が必要としていたものです。 – starky

関連する問題