elasticsearch - ElasticSearch query_string :过滤数组成员包含 2 个字段值的元素
问题描述
我想过滤具有 Product.Name = "name1" AND Product.Version = "v1" 的命中(使用 query_string)
...
"Product" : [
{
"Name" : ....,
"Version" : ...,
...
},
{
...
},
etc ...
],
...
解决方案
您需要使用嵌套映射。
嵌套类型与对象类型不同,您将在上面的链接中有所不同。
Query_String 不适用于嵌套类型,来自文档
避免对嵌套文档使用 query_string 查询编辑 query_string 搜索不返回嵌套文档。要搜索嵌套文档,请使用嵌套查询。
映射:
PUT nestedindex
{
"mappings": {
"properties": {
"Product":{
"type":"nested",
"properties": {
"Name":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword"
}
}
},
"Version":{
"type":"integer"
}
}
}
}
}
}
询问:
GET nestedindex/_search
{
"query": {
"nested": {
"path": "Product",
"query": {
"bool": {
"must": [
{
"term": {
"Product.Name.keyword": {
"value": "Prod1"
}
}
},
{
"term": {
"Product.Version": {
"value": 1
}
}
}
]
}
},
"inner_hits": {} ---> If you need ti find matching nested document
}
}
}
结果:
"hits" : [
{
"_index" : "nestedindex",
"_type" : "_doc",
"_id" : "plQhjW0BywGFQhV7Zs6c",
"_score" : 1.6931472,
"_source" : {
"Product" : [
{
"Name" : "Prod1",
"Version" : 1
},
{
"Name" : "Prod2",
"Version" : 2
}
]
},
"inner_hits" : {
"Product" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.6931472,
"hits" : [
{
"_index" : "nestedindex",
"_type" : "_doc",
"_id" : "plQhjW0BywGFQhV7Zs6c",
"_nested" : {
"field" : "Product",
"offset" : 0
},
"_score" : 1.6931472,
"_source" : {
"Name" : "Prod1",
"Version" : 1
}
}
]
}
}
}
}
]
推荐阅读
- php - 如何通过服务器上的 cron 作业每小时“访问”一个站点?
- sql - 计算消耗返回零
- c# - 时间:2012-04-10 标签:c#winformvs2012
- android - 使用的api的易错性是什么意思
- python - 如何使用 gRPC 进行异步流式传输?
- spring - 为什么我不能在 Spring Tool Suite 和 https://start.spring.io/ 上选择 Springboot 2.1.1?
- pgadmin - pgadmin4 查询工具中奇怪的行缩进,如何解决?
- arrays - 如何正确分配不是从索引 1 开始的数组范围到 excel 范围?
- go - Arithmetic on uint8, int8
- java - Java中的重复文件通道