[英]Elasticsearch filter by nested fields
I have a problem with creating a query to Elasticsearch with many conditions.我在创建具有多种条件的 Elasticsearch 查询时遇到问题。 My model looks like:
我的模型看起来像:
data class Product(
@Id
val id: String? = null,
val category: String,
val imagesUrls: List<String>,
@Field(type = FieldType.Double)
val price: Double?,
@Field(type = FieldType.Nested)
val parameters: List<Parameter>?
)
data class Parameter(
val key: String,
val values: List<String>
)
I would like to query products by:我想通过以下方式查询产品:
cars
)cars
)capacity
values 4L
, 5L
and second parameter gear transmission values manual
capacity
值4L
、 5L
和第二参数齿轮传动值manual
My current query looks like this:我当前的查询如下所示:
GET data/_search
{
"size": 10,
"query": {
"bool": {
"must": [
{
"term": {
"category.keyword": {
"value": "cars"
}
}
},
{
"nested": {
"path": "parameters",
"query": {
"bool": {
"must": [
{"term": {
"parameters.key.keyword": {
"value": "Capacity"
}
}},
{
"term": {
"parameters.key": {
"value": "4L, 5L"
}
}
}
]
}
}
}
}
]
}
}
Example data:示例数据:
{
"category":"cars",
"name":"Ferrari",
"price":50000,
"parameters":[
{
"key":"capacity",
"values":"4L"
},
{
"key":"gear transmission",
"values":"automcatic"
}
]
}
The search query shown below queries the data based on:
下面显示的搜索查询基于以下条件查询数据:
- category (for example cars)
类别(例如汽车)
- And parameters -> For example products with many parameters, like key capacity values 4L, 5L and second parameter gear transmission values manual
和参数 -> 例如有很多参数的产品,如关键容量值 4L、5L 和第二参数齿轮传动值手册
Adding a working example with index data, mapping, search query, and search result添加包含索引数据、映射、搜索查询和搜索结果的工作示例
Index Mapping:索引映射:
{
"mappings": {
"properties": {
"parameters": {
"type": "nested"
}
}
}
}
Index Data:指数数据:
{
"category":"cars",
"name":"Ferrari",
"price":50000,
"parameters":[
{
"key":"gear transmission",
"values":["4L","5L"]
},
{
"key":"capacity",
"values":"automcatic"
}
]
}
{
"category":"cars",
"name":"Ferrari",
"price":50000,
"parameters":[
{
"key":"capacity",
"values":["4L","5L"]
},
{
"key":"gear transmission",
"values":"automcatic"
}
]
}
{
"category":"cars",
"name":"Ferrari",
"price":50000,
"parameters":[
{
"key":"capacity",
"values":"4L"
},
{
"key":"gear transmission",
"values":"automcatic"
}
]
}
Search Query:搜索查询:
{
"query": {
"bool": {
"must": [
{
"term": {
"category.keyword": {
"value": "cars"
}
}
},
{
"nested": {
"path": "parameters",
"query": {
"bool": {
"must": [
{
"match": {
"parameters.key": "capacity"
}
},
{
"terms": {
"parameters.values": [
"4l",
"5l"
]
}
}
]
}
}
}
},
{
"nested": {
"path": "parameters",
"query": {
"bool": {
"must": [
{
"match": {
"parameters.key": "gear transmission"
}
},
{
"match": {
"parameters.values": "automcatic"
}
}
]
}
}
}
}
]
}
}
}
Search Result:搜索结果:
"hits": [
{
"_index": "bstof",
"_type": "_doc",
"_id": "1",
"_score": 3.9281754,
"_source": {
"category": "cars",
"name": "Ferrari",
"price": 50000,
"parameters": [
{
"key": "capacity",
"values": "4L"
},
{
"key": "gear transmission",
"values": "automcatic"
}
]
}
},
{
"_index": "bstof",
"_type": "_doc",
"_id": "2",
"_score": 3.9281754,
"_source": {
"category": "cars",
"name": "Ferrari",
"price": 50000,
"parameters": [
{
"key": "capacity",
"values": [
"4L",
"5L"
]
},
{
"key": "gear transmission",
"values": "automcatic"
}
]
}
}
]
When you need to match any one from a list then you can use terms
query instead of term
.当您需要匹配列表中的任何一个时,您可以使用
terms
查询而不是term
。 Update the part in query from:更新查询中的部分:
{
"term": {
"parameters.key": {
"value": "4L, 5L"
}
}
}
to below:到下面:
{
"terms": {
"parameters.values": {
"value": [
"4L",
"5L"
]
}
}
}
Note that if parameters.key is analysed field and there exist a keyword sub-field for the same, then use it instead.请注意,如果 parameters.key 是分析字段并且存在相同的关键字子字段,则改用它。 eg
parameters.values.keyword
例如
parameters.values.keyword
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.