2017-12-01 8 views
0

マッピング:フィルタの結果、私は以下のいる

{ 
    cities: { 
    mappings: { 
     city: { 
     properties: { 
      id: { 
      type: "long" 
      }, 
      name: { 
      type: "text", 
      fields: { 
       keyword: { 
       type: "keyword", 
       ignore_above: 256 
       } 
      } 
      }, 
      population: { 
      type: "long" 
      }, 
     } 
     } 
    } 
    } 
} 

および実行する単純なクエリ:結果でそう

{ 
    query: { 
    bool: { 
     must_not: { match: { name: 'New York' } }, 
    }, 
    }, 
    size: 2, 
} 

私が取得:

{ 
    "took": 5, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 10385, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "cities", 
     "_type": "city", 
     "_id": "14", 
     "_score": 1, 
     "_source": { 
      "name": "Berlin", 
      "id": 14, 
      "population": 7293 
     } 
     }, 
     { 
     "_index": "cities", 
     "_type": "city", 
     "_id": "19", 
     "_score": 1, 
     "_source": { 
      "name": "Paris", 
      "id": 19, 
      "population": 25018 
     } 
     } 
    ] 
    } 
} 

どのようにすることができますnameが特定の値の配列になっているドキュメント、つまり['Berlin', 'Bonn', 'Munchen']に一致するようにこのクエリを制限します?

私のようなSQL文のsimmilar何かを探しています:あなたはterms queryを使用することができます

SELECT * FROM cities WHERE name != 'New York' and name IN ('Berlin', 'Bonn', 'Munchen'); 

答えて

1

ので、お問合せは、下記のようになります。

{ 
    query: { 
    bool: { 
     must: { terms: { 'name.keyword': ['Berlin', 'Bonn', 'Munchen'] } } 
     must_not: { match: { name: 'New York' } }, 
    }, 
    }, 
    size: 2, 
} 
+0

@ Lupanoideの '' name.keyword''は作業を行います。ありがとう! – hsz

0
POST /cities/_search 
{ 
    "query": { 
    "bool" : { 
     "must" : [ 
     { "match" : { "name" : "Berlin" } }, 
     { "match" : { "name" : "Munchen" } }, 
     { "match" : { "name" : "Bonn" } } 
     ], 
     "minimum_should_match" : 3 
    } 
    } 
} 

または

POST /cities/_search 
{ 
    "query": { 
    "bool" : { 
     "must" : [ 
     { "term" : { "name.keyword" : "Berlin" } }, 
     { "term" : { "name.keyword" : "Munchen" } }, 
     { "term" : { "name.keyword" : "Bonn" } } 
     ], 
     "minimum_should_match" : 3 
    } 
    } 
} 
+0

用語を分離したオブジェクトに分割することは解決策ではありません。 – hsz

+0

しかし、 '' name.keyword''トリックは私を助けました、それを知らなかった! – hsz

+0

用語クエリではキーワードデータ型が必要ですが、これは他の回答が機能しないためです。一致クエリは、テキストデータ型を必要とします。クエリ内の用語の分割は、構文が異なるだけで、クエリは同じです – Lupanoide

関連する問題