我的索引中有一个相对简单的构造。客户拥有自己的房产,他将自己的身份,电子邮件和电话作为嵌套属性。到目前为止,当我查询嵌套对象时,我的所有查询都可以正常工作。我正在开发类似全局搜索的东西,允许用户输入任何信息,并向他呈现相应的客户。为此,我迭代了我拥有的所有嵌套属性(因为我不知道他究竟在搜索什么)。我的查询如下:
{
"sort": ["_score"],
"query": {
"bool": {
"must": [{
"nested": {
"path": "identity",
"query": {
"bool": {
"must": [{
"multi_match": {
"query": "my search",
"fields": "identity.*"
}
}]
}
}
}
}, {
"nested": {
"path": "phones",
"query": {
"bool": {
"must": [{
"multi_match": {
"query": "my search",
"fields": "phones.*"
}
}]
}
}
}
}]
}
}
但是,当您有多个嵌套查询时," AND"运算符被应用,因此我没有得到正确的信息。如果我只搜索一个嵌套对象,我总是得到正确的结果。我试过把#34;运算符" => "或"几乎无处不在,但没有效果,文档没有说明这种情况(或至少我找不到任何东西)。我错过了什么?
答案 0 :(得分:0)
您应该将“must”替换为“should”以获取或行为,请参阅更多here。
答案 1 :(得分:0)
好的,这是一个示例:
{
"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": "john@john.com"
}]
}
{
"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": "dom@dom.com"
}]
}
因此,如果我使用must
条款并且我搜索john / dom我没有得到任何结果(记住,我搜索所有嵌套文档而电话不包含任何' john / dom',如果我只搜索身份/电子邮件,我将得到正确的结果,因为电子邮件也包含名称)。
如果我切换到should
条款并且我执行相同的查询,我会回到john和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"
}
}]
}
}
}