【发布时间】:2020-08-05 19:35:36
【问题描述】:
我有一组数据结构如下:
[
{
"productId": "ProductId1",
"customerNumbers": [
"customer": {
"name": "Prod1Cust1",
"totalOrders": 23
},
"customer": {
"name": "Prod2Cust1",
"totalOrders": 5
},
"customer": {
"name": "Prod3Cust1",
"totalOrders": 5
}
]
},
{
"productId": "ProductId2",
"customerNumbers": [
"customer": {
"name": "Prod2Cust1",
"totalOrders": 23
},
"customer": {
"name": "Prod2Cust1",
"totalOrders": 5
}
]
}
]
我需要在 name 字段中获取所有 prefix 为“Prod1”的记录(在示例中避免,只应返回第一条记录,即 ProductId1 ). 另外,当数据返回时,我只需要获取前缀为 Prod1 的客户编号,即:
正确的输出:
{
"productId": "ProductId1",
"customerNumbers": [
"customer": {
"name": "Prod1Cust1",
"totalOrders": 23
}
]
}
代替:
{
"productId": "ProductId1",
"customerNumbers": [
"customer": {
"name": "Prod1Cust1",
"totalOrders": 23
},
"customer": {
"name": "Prod2Cust1",
"totalOrders": 5
},
"customer": {
"name": "Prod3Cust1",
"totalOrders": 5
}
]
}
我可以使用嵌套查询和 MatchPhrasePrefixQuery 来获取 Name 前缀为“Prod1”的记录(这会返回包含所有客户编号的结果)。如何进一步过滤数据以获取 Name 前缀为“Prod1”的客户编号。
以下是我当前的查询:
{
"from": 0,
"size": 10,
"sort": [
{
"name.keyword": {
"missing": "_first",
"order": "asc"
}
}
],
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"match": {
"customerNumbers.name": {
"query": "Prod1",
"type": "phrase_prefix"
}
}
}
]
}
},
"path": "customerNumbers"
}
}
]
}
}
]
}
}
}
P.S:我正在使用 ElasticSearch 5.x 和 Nest。
【问题讨论】:
标签: elasticsearch nest elasticsearch-query