[英]Elasticsearch query in array conditions on doc fields
I have a document like this:我有这样的文件:
{
"_index": "listings",
"_type": "listing",
"_id": "234",
"_source": {
"category_id": "43608",
"categories": [
43608,
43596
]
}
}
I wanna query to array search category_id
in categories
.我想查询以数组搜索
category_id
在categories
。 some thing like that类似的东西
{
"query": {
"bool": {
"must": [
{
"terms": {
"category_id": "doc.categories"
}
}
]
}
}
}
What I supposed to do?我该怎么办?
As, category_id
is a string type, better to use SHOULD
query instead of MUST
and Simply, Itrate through the array categories
and make separate term
level query for each element in array.因为,
category_id
是一个字符串类型,最好使用SHOULD
查询而不是MUST
查询,简单地说,遍历数组categories
并对数组中的每个元素进行单独的term
级别查询。
{
"query": {
"bool": {
"should": [
{
"term": {
"category_id": "doc.categories[0]"
}
},
{
"term": {
"category_id": "doc.categories[1]"
}
},
...
]
}
}
}
It will return you all which match any of categories
array.它将返回与任何
categories
数组匹配的所有内容。
You have to user script for find a field value in another field value.您必须使用用户脚本才能在另一个字段值中查找字段值。
{
"script": {
"script": {
"source": "doc.containsKey('categories') && doc['categories'].values.contains(doc['category_id'].value)",
"lang": "painless"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.