好吧,這裏是一個示例:
{
"uid": "uid",
"oid": "oid",
"tags": ["VIP"],
"identity": {
"_id": {
"$oid": "567901d6ab265d4cf7000001"
},
"first": "John",
"gender": "male",
"last": "",
"middle": "",
"prefix": "",
"suffix": ""
},
"phones": [{
"_id": "23232323-642f-4ce1-ab1b-542bbd1468ea",
"created_at": null,
"name": "mobile",
"updated_at": null,
"value": "987654321"
}],
"emails": [{
"_id": "12121212-642f-4ce1-ab1b-542bbd1468ea",
"created_at": null,
"name": "home",
"updated_at": null,
"value": "[email protected]"
}]
}
{
"uid": "uid",
"oid": "oid",
"tags": ["VIP"],
"identity": {
"_id": {
"$oid": "567901d6ab265d4cf7000001"
},
"first": "Dom",
"gender": "male",
"last": "",
"middle": "",
"prefix": "",
"suffix": ""
},
"phones": [{
"_id": "23232323-642f-4ce1-ab1b-542bbd1468ea",
"created_at": null,
"name": "mobile",
"updated_at": null,
"value": "987654321"
}],
"emails": [{
"_id": "12121212-642f-4ce1-ab1b-542bbd1468ea",
"created_at": null,
"name": "home",
"updated_at": null,
"value": "[email protected]"
}]
}
所以,如果我使用must
條款。我做約翰/ DOM我沒有得到任何結果(請記住搜索,我搜索了所有嵌套的文件和手機不包含任何'john/dom',如果我只搜索身份/電子郵件,我會得到正確的結果,因爲電子郵件也包含名稱)。
如果我切換到should
子句,我做同樣的查詢我回來了約翰和dom。
最後的查詢看起來是這樣的:
{
"_source": ["oid", "uid"],
"sort": ["_score"],
"query": {
"bool": {
"should": [{
"nested": {
"path": "identity",
"query": {
"bool": {
"must": [{
"multi_match": {
"query": "john",
"operator": "and",
"fields": ["identity.*"],
"type": "cross_fields",
"tie_breaker": 1.0
}
}]
}
}
}
}, {
"nested": {
"path": "emails",
"query": {
"bool": {
"must": [{
"multi_match": {
"query": "john",
"operator": "and",
"fields": ["emails.*"],
"type": "cross_fields",
"tie_breaker": 1.0
}
}]
}
}
}
}, {
"nested": {
"path": "phones",
"query": {
"bool": {
"must": [{
"multi_match": {
"query": "john",
"operator": "and",
"fields": ["phones.*"],
"type": "cross_fields",
"tie_breaker": 1.0
}
}]
}
}
}
}],
"filter": [{
"term": {
"oid": "oid"
}
}]
}
}
}
你也可以去掉「排序」:「_score」],這是默認的。 – Tomer