我有這種格式在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
在分析的字段上使用術語查詢不危險嗎?如果沒有提供映射,nm將在這種情況下被分析。 – Artholl
@Artholl如果您使用'not_analyzed',如果您不希望爲上述場景分析該字段,該怎麼辦? – Kulasangar
@Kulasangar不會在同一個文檔上應用匹配/過濾條件嗎?但是,例如,在這裏,三個文檔具有相同的pid,即1,但是'nm'有三個不同的值。 – harbinger