我有以下文档结构:
{
product_name: "Product1",
product_id: 1,
...,
articles: [
{
article_name: 'Article 101',
id: 101,
some_param: 10,
clients: []
},
{
article_name: 'Article 102',
id: 102,
some_param: 11,
clients: [
{
client_id: 10001,
client_name: "some client 1001"
}
...
]
}
]
},
{
product_name: "Product2",
product_id: 2,
...,
articles: [
{
article_name: 'Article 101',
id: 101,
some_param: 10,
clients: []
},
{
article_name: 'Article 102',
id: 102,
some_param: 10,
clients: [
{
client_id: 10001,
client_name: "some client 1001"
}
...
]
}
]
}
仅当某些文章符合 2 个条件(单篇文章应符合两个条件)时,我才需要获取文档(产品):articles.some_param = 10 AND articles.clients.client_id = 10001
所以我只需要获取 ID 为 2 的产品。
我现在正在使用这个查询,这是不正确的(我知道为什么),因为它获取两个文档:
{
"query": {
"bool": {
"filter": [
{
"term": {
"articles.clients.id": 10001
}
},
{
"terms": {
"articles.some_param": 10
}
}
]
}
}
}
我如何编写查询,只获取至少有 1 篇符合这两个条件的文章的产品:articles.some_param = 10 AND articles.clients.client_id = 10001
例如,仅获取 ID 为 2 的产品?
像这样的东西:
{
"query": {
"nested": {
"path": "articles",
"query": {
"bool": {
"must": [
{
"term": {
"articles.some_param": {
"value": 10
}
}
},
{
"nested": {
"path": "articles.clients",
"query": {
"term": {
"articles.clients.id":{
"value": 10001
}
}
}
}
}
]
}
}
}
}
}
更新:尝试将第二个查询包装为 bool。
{
"query": {
"nested": {
"path": "articles",
"query": {
"bool": {
"must": [
{
"term": {
"articles.some_param": {
"value": 10
}
}
},
{
"bool":{
"must" : [
{
"nested": {
"path": "articles.clients",
"query": {
"term": {
"articles.clients.id":{
"value": 10001
}
}
}
}
}
]
}
}
]
}
}
}
}
}
ps 我可能会误认为第二个嵌套查询的路径。就是查不出来。因此,您可以在第二个查询中使用路径。
pps 过滤器不是您需要的查询。它不计算分数
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句