我们如何在以下条件下编写查询:
数据结构:
“name”:“Ross”,
“status”:“ACTIVE”,
“adsPurchased”:是的,
“searchString”:“some_tring”,
“productCost”:[
{
“产品”:“产品1”,
“费用”:“50.0”
},
{
“产品”:“产品2”,
“费用”:“80.0”
}
]
}
{
“name”:“Chandler”,
“status”:“INACTIVE”,
“adsPurchased”:是的,
“searchString”:“some_String”,
“productCost”:[
{
“产品”:“产品1”,
“费用”:“60.0”
},
{
“产品”:“产品4”,
“费用”:“800.0”
}
]
}
{
“name”:“joey”,
“status”:“ACTIVE”,
“adsPurchased”:是的,
“searchString”:“some_tring”,
“productCost”:[
{
“产品”:“产品1”,
“费用”:“30.0”
},
{
“产品”:“产品5”,
“费用”:“90.0”
}
]
}
所以,我应该找罗斯和乔伊
最佳答案
添加带有映射,示例文档和搜索查询的工作示例。
映射:
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"status": {
"type": "text"
},
"adsPurchased": {
"type": "boolean"
},
"searchString": {
"type": "text"
},
"productCost": {
"type": "nested",
"properties": {
"product": {
"type": "text"
},
"cost": {
"type": "integer"
}
}
}
}
}
}
搜索查询:{
"query": {
"bool": {
"must": [
{
"match": {
"adsPurchased": true -->adsPurchased MUST to be true
}
},
{
"match": {
"searchString": "some_tring" -->searchString MUST be "some_tring"
}
},
{
"nested": {
"path": "productCost",
"query": {
"bool": {
"must": [ -->must have "Product 1" under productCost
{
"match": {
"productCost.product": "Product 1"
}
}
]
}
}
}
}
],
"must_not": [
{
"match": {
"status": "INACTIVE" -->status MUST NOT be "INACTIVE"
}
}
]
}
},
"sort": [ -->Sort them based on cost
{
"productCost.cost": {
"order": "asc",
"nested_path": "productCost"
}
}
]
}
搜索结果:"hits": [
{
"_index": "foo3",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"name": "joey",
"status": "ACTIVE",
"adsPurchased": true,
"searchString": "some_tring",
"productCost": [
{
"product": "Product 1",
"cost": "30.0"
},
{
"product": "Product 5",
"cost": "90.0"
}
]
},
"sort": [
30
]
},
{
"_index": "foo3",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "Ross",
"status": "ACTIVE",
"adsPurchased": true,
"searchString": "some_tring",
"productCost": [
{
"product": "Product 1",
"cost": "50.0"
},
{
"product": "Product 2",
"cost": "80.0"
}
]
},
"sort": [
50
]
}
]
在搜索结果中,您将得到所需的结果,即Ross和joey 要了解有关嵌套排序的更多信息,可以引用此官方的documentation;有关嵌套查询的信息,请引用this
关于elasticsearch - 如何为此编写 Elasticsearch 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62495447/