elasticsearch - 对同一个嵌套对象的两个查询..在不同的值上,然后期望 inner_hits 中有 2 个匹配的对象,但在 Elasticsearch 中只有 1 个
问题描述
我有一个 penDocuments 嵌套对象,如下所示。执行 must 子句中的两个不同查询。我期待 innner_hits 包含两个平底锅,护照对象。这意味着总点击数应该是 2,但有 1 个对象是 pan 。为什么护照对象没有显示在 innerHits 中?我的嵌套字段是
"penDocuments": {
"type": "nested",
"properties": {
"type": {
"type": "text",
"index_options": "docs"
},
"values": {
"type": "text",
"index_options": "docs",
"analyzer": "lowercase_analyzer"
}
}
}
我的数据:
"penDocuments":
[
{
"type": "PAN",
"values": [
"735fHgBT1",
"Zwgt3ES21"
]
},
{
"type": "Passport",
"values": [
"zzzzz",
"wwwww"
]
}
]
我的查询是:
{
"query": {
"bool": {
"must": [
{
"constant_score": {
"filter": {
"nested": {
"inner_hits": {
"_source": true
},
"path": "penDocuments",
"query": {
"bool": {
"must": [
{
"match": {
"penDocuments.type": "Passport"
}
},
{
"match": {
"penDocuments.values": "zzzzz"
}
}
]
}
}
}
},
"boost": 100
}
},
{
"constant_score": {
"filter": {
"nested": {
"inner_hits": {
"_source": true
},
"path": "penDocuments",
"query": {
"bool": {
"must": [
{
"match": {
"penDocuments.type": "PAN"
}
},
{
"match": {
"penDocuments.values": "735fHgBT1"
}
}
]
}
}
}
},
"boost": 100
}
}
]
}
} }
在inner_hits
{
"inner_hits": {
"penDocuments": {
"hits": {
"total": 1,
"max_score": 1.3862944,
"hits": [
{
"_index": "neglistindividual_junit",
"_type": "details",
"_id": "869",
"_nested": {
"field": "penDocuments",
"offset": 0
},
"_score": 1.3862944,
"_source": {
"type": "PAN",
"values": [
"735fHgBT1",
"Zwgt3ES21"
]
}
}
]
}
}
} }
解决方案
我试着和你一起映射和查询。由于 lowercase_analyzer 的定义不存在,我创建了自己的。由于在查询中使用了多个 inner_hits,您的查询给出了错误,我为此做了一些小改动。结果我得到了两个文件
映射
PUT testindex12
{
"mappings": {
"properties": {
"penDocuments": {
"type": "nested",
"properties": {
"type": {
"type": "text",
"index_options": "docs"
},
"values": {
"type": "text",
"index_options": "docs",
"analyzer":"lowercase_analyzer"
}
}
}
}
},
"settings": {
"analysis": {
"analyzer": {
"lowercase_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"lowercase"
]
}
}
}
}
}
数据:
POST testindex12/_doc
{
"penDocuments":
[
{
"type": "PAN",
"values": [
"735fHgBT1",
"Zwgt3ES21"
]
},
{
"type": "Passport",
"values": [
"zzzzz",
"wwwww"
]
}
]
询问:
GET testindex12/_search
{
"query": {
"bool": {
"must": [
{
"constant_score": {
"filter": {
"nested": {
"inner_hits": {
"name": "Passport" ---> multiple innerhits need different name since they take name of path , which is same for both query
},
"path": "penDocuments",
"query": {
"bool": {
"must": [
{
"match": {
"penDocuments.type": "Passport"
}
},
{
"match": {
"penDocuments.values": "zzzzz"
}
}
]
}
}
}
},
"boost": 100
}
},
{
"constant_score": {
"filter": {
"nested": {
"inner_hits": {
"name": "PAN"
},
"path": "penDocuments",
"query": {
"bool": {
"must": [
{
"match": {
"penDocuments.type": "PAN"
}
},
{
"match": {
"penDocuments.values": "735fHgBT1"
}
}
]
}
}
}
},
"boost": 100
}
}
]
}
}
}
结果:
"inner_hits" : {
"Passport" : { --->passport doc
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testindex12",
"_type" : "_doc",
"_id" : "Lz47SG0BbzgYofLx6DbY",
"_nested" : {
"field" : "penDocuments",
"offset" : 1
},
"_score" : 1.3862944,
"_source" : {
"type" : "Passport",
"values" : [
"zzzzz",
"wwwww"
]
}
}
]
}
},
"PAN" : { --->pan doc
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testindex12",
"_type" : "_doc",
"_id" : "Lz47SG0BbzgYofLx6DbY",
"_nested" : {
"field" : "penDocuments",
"offset" : 0
},
"_score" : 1.3862944,
"_source" : {
"type" : "PAN",
"values" : [
"735fHgBT1",
"Zwgt3ES21"
]
}
}
]
}
}
}
推荐阅读
- c - 用于 LED 调光异常行为的 PWM
- python - 在不匹配(“N”)的参考上查找字符串(DNA序列)的位置
- arrays - 如何在 c 编程中将一个数组绑定到另一个数组?
- r - 使用字符向量分量作为 R 函数的参数
- python - 如何为新项目使用新目录位置及其在 buildozer 中的 main.py 文件为 android 部署?
- facebook-graph-api - 更改最低 ROAS Facebook Graph API
- python - Python 中没有重新定义变量(discord.py)
- racket - 尽管使用了“对接”和“斜接”,为什么我的球拍笔会在两条线连接处绘制额外的像素?
- sql-server - 使用 PowerShell 在 SQL Server 表中加载 URL 结果
- c# - Ubuntu 20.04:编译并运行使用 Npgsql 访问 PostgreSQL 的 C# 程序