2016-12-14 92 views
1

我有這種格式在elasticsearch的數據的條件下,每一個都是單獨的文件中:ElasticSearch查詢與多個文檔

{「PID」:1「納米」:「湯姆」},{「PID」 :1,'nm':'dick''},{'pid':1,'nm':'harry'},{'pid':2,'nm':'tom'},{'pid': 2,'nm':'harry'},{'pid':3,'nm':'dick'},{'pid':3,'nm':'harry'},{'pid':4, 「納米」:「哈利」}

{ 
     "took": 137, 
     "timed_out": false, 
     "_shards": { 
      "total": 5, 
      "successful": 5, 
      "failed": 0 
     }, 
     "hits": { 
      "total": 8, 
      "max_score": null, 
      "hits": [ 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9KS86AaDUbQTYUmwY", 
       "_score": null, 
       "_source": { 
        "pid": 1, 
        "nm": "Harry" 
       } 
      }, 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9KJ9BAaDUbQTYUmwW", 
       "_score": null, 
       "_source": { 
        "pid": 1, 
        "nm": "Tom" 
       } 
      }, 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9KRlbAaDUbQTYUmwX", 
       "_score": null, 
       "_source": { 
        "pid": 1, 
        "nm": "Dick" 
       } 
      }, 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9KYnKAaDUbQTYUmwa", 
       "_score": null, 
       "_source": { 
        "pid": 2, 
        "nm": "Harry" 
       } 
      }, 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9KXL5AaDUbQTYUmwZ", 
       "_score": null, 
       "_source": { 
        "pid": 2, 
        "nm": "Tom" 
       } 
      }, 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9KbcpAaDUbQTYUmwb", 
       "_score": null, 
       "_source": { 
        "pid": 3, 
        "nm": "Dick" 
       } 
      }, 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9Kdy5AaDUbQTYUmwc", 
       "_score": null, 
       "_source": { 
        "pid": 3, 
        "nm": "Harry" 
       } 
      }, 
      { 
       "_index": "query_test", 
       "_type": "user", 
       "_id": "AVj9KetLAaDUbQTYUmwd", 
       "_score": null, 
       "_source": { 
        "pid": 4, 
        "nm": "Harry" 
       } 
      } 
      ] 
     } 
    } 

,我需要找到PID的具有「哈利」和沒有「湯姆」,這在上面的例子中是3和4這essentialy意味着尋找具有相同pid的文檔,其中沒有任何一個具有nm的值「湯姆「,但他們中至少有一個的價值爲'哈里'。

如何查詢?

編輯:使用Elasticsearch版本5

回答

1

如果你有一個POST請求主體可能類似於下面,在這裏你可以使用bool

POST _search 
{ 
    "query": { 
    "bool" : { 
     "must" : { 
     "term" : { "nm" : "harry" } 
     }, 
     "must_not" : { 
     "term" : { "nm" : "tom" } 
     } 
    } 
    } 
} 
+0

在分析的字段上使用術語查詢不危險嗎?如果沒有提供映射,nm將在這種情況下被分析。 – Artholl

+0

@Artholl如果您使用'not_analyzed',如果您不希望爲上述場景分析該字段,該怎麼辦? – Kulasangar

+0

@Kulasangar不會在同一個文檔上應用匹配/過濾條件嗎?但是,例如,在這裏,三個文檔具有相同的pid,即1,但是'nm'有三個不同的值。 – harbinger

0

我相對非常新Elasticsearch,所以我可能是錯的。但我從來沒有見過這樣的問題。簡單的過濾器不能在這裏使用,因爲這些過濾器應用於您不需要的文檔(而不是聚合)。我看到的是你想用「Having」子句(按照SQL)進行「Group by」查詢。但按查詢分組涉及的某些聚合(如任何字段的平均值,最大值,最小值)在「Having」子句中使用。基本上你使用reducer來進行聚合結果的Post處理。對於像這樣的查詢可以使用Bucket Selector Aggregation。閱讀this
但你的情況是不同的。您不想在任何度量標準聚合上應用Having子句,但您想要檢查「group by」數據的字段(或列)中是否存在某個值。就SQL而言,您希望在「group by」中執行「where」查詢。這是我從未見過的。您也可以閱讀this
但是,在應用程序級別,您可以通過中斷查詢來輕鬆完成此操作。首先找到獨特的pid,其中nm = harry使用term aggs。然後獲取附加條件nm!= tom的那些pid的文檔。

P.S.我對ES很新。如果有人與我矛盾,我會很高興在一個查詢中展示如何做到這一點。我也會學到這一點。