object - How to search data that not in the object in Elasticsearch using Nested?
问题描述
I try to search categories and products in Elasticsearch using Nested type.
I have data like this:
POST products/_doc
{
"id" : 1,
"category_id" : 1,
"category_name" : "Test Category",
"name": "Sapiente",
"attributes" : [
{
"color" : "blue",
"size" : 8
},
{
"color" : "black",
"size" : 16
}
]
}
and I try to search on this data like this :
GET products/_search
{
"query": {
"nested": {
"path": "attributes",
"query": {
"bool": {
"filter": [
{
"term": {
"name.keyword": "Sapiente"
}
}
]
}
}
}
}
}
But i didn't understand to how i can search for NAME or CATEGORY_NAME fields. I can only search data that in the attributes object otherwise i get empty result.
Is that possible to search both NAME, CATEGORY_NAME and data in ATTRIBUTES object ?
Thank you in advance...
解决方案
您可以使用布尔查询来组合嵌套和非嵌套查询
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "attributes",
"query": {
"bool": {
"filter": [
{
"term": {
"attributes.color": "blue"
}
}
]
}
}
}
},
{
"term": {
"name.keyword": "Sapiente"
}
}
]
}
}
}
推荐阅读
- servlets - 从另一个 servlet 调用 servlet 过滤器
- c# - 在依赖注入期间使用 NLog
- javascript - Audiocontext 没有播放任何声音
- sql - SQL注入测试软件
- python - 大熊猫计算同一列的时间差?
- ios - 当有 100% 的值时,过滤 Realm 对象返回 nil
- elasticsearch - 是否可以使用 FSCrawler 将文件内容提取到 Elasticsearch 中现有索引的特定 _id
- prometheus - 如何使用正则表达式和二元运算符编写 PromQL
- python - 如何将js模块导入python?
- ios - 使用nodejs和express的应用程序,无法登录ios Iphone