[英]ElasticSearch bool filter on nested fields
As a beginner in ElasticSearch we have our document structured like this:作为 ElasticSearch 的初学者,我们的文档结构如下:
{
"Id": 1246761,
"Title": "Official statement Title",
"Categories": [
{
"Id": 3,
"Type": 1,
"Name": "Category 1-A"
},
{
"Id": 7,
"Type": 1,
"Name": "Category 1-B"
},
{
"Id": 104,
"Type": 3,
"Name": "Category 3-C"
},
{
"Id": 2001,
"Type": 7,
"Name": "Category 7-D"
}
]
}
We would like to search our document on a combinaison of keyword in the title, and a combinaison of category ids.我们想根据标题中关键字的组合和类别 ID 的组合来搜索我们的文档。 For example I want to get documents matching this:
例如,我想获取与此匹配的文档:
So far this is my query:到目前为止,这是我的查询:
"query": {
"bool": {
"must": [
{
"multi_match": {
"operator": "and",
"query": "Official"
}
}
],
"filter": [
{ "nested": {
"path": "Categories",
"query": {
"bool": {
"minimum_should_match": 2,
"should": [
{ "terms": { "Categories.Id": [3,7] }},
{ "terms": { "Categories.Id": [2001] }}
]
}
}
}}
]
}
}
But it's not giving me back any result... How should I rewrite this?但它没有给我任何结果......我应该如何重写这个?
---UPDATE--- Here's the mapping: ---更新---这是映射:
{
"properties" : {
"Id" : {
"type" : "long"
},
"Title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"Categories" : {
"type" : "nested",
"properties" : {
"Id" : {
"type" : "long"
},
"Name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"ParentId" : {
"type" : "long"
},
"Type" : {
"type" : "long"
}
}
}
}
Found out the solution was to write multiple nested filters:发现解决方案是编写多个嵌套过滤器:
"query": {
"bool": {
"must": [
{
"multi_match": {
"operator": "and",
"query": "Official"
}
}
],
"filter": [
{
"bool": {
"must": [
{ "nested": {
"path": "Categories",
"query": {
"bool": {
"must": [
{"terms": {
"Categories.Id": [
"3,7"
]
}}
]
}
}
}},
{ "nested": {
"path": "Categories",
"query": {
"bool": {
"must": [
{"terms": {
"Categories.Id": [
"2000"
]
}}
]
}
}
}}
]
}
}
]
}
}
There's a typo in the first should
query -- you're targeting .Id
instead of .Type
.第一个
should
查询中有一个错字——您的目标是.Id
而不是.Type
。 Here's a fix:这是一个修复:
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"operator": "and",
"query": "Official"
}
}
],
"filter": [
{
"nested": {
"path": "Categories",
"query": {
"bool": {
"minimum_should_match": 2,
"should": [
{
"terms": {
"Categories.Type": [ 3, 7 ] <---
}
},
{
"terms": {
"Categories.Id": [ 2001 ]
}
}
]
}
}
}
}
]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.