如何根据字段值过滤 ElasticSearch 结果?
How to filter ElasticSearch results basis the field value?
下面是我的示例搜索响应,检索到 4 个结果。
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.41753215,
"hits": [
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/shared/Report_google_Shared",
"_score": 0.41753215,
"fields": {
"lastModified": [
1461738428007
],
"dir": [
"/shared"
],
"filename": [
"Report_google_Shared"
]
},
"highlight": {
"filename": [
"Report_google_Shared"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/shared/Report_Gmail_Shared",
"_score": 0.41753215,
"fields": {
"lastModified": [
1461738618676
],
"dir": [
"/shared"
],
"filename": [
"Report_Gmail_Shared"
]
},
"highlight": {
"filename": [
"Report_Gmail_Shared"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/private/hitesh/Report_Gmail_Private",
"_score": 0.1883173,
"fields": {
"lastModified": [
1461738629888
],
"dir": [
"/private/hitesh"
],
"filename": [
"Report_Gmail_Private"
]
},
"highlight": {
"filename": [
"Report_Gmail_Private"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/private/dholaria/Report_google_Private",
"_score": 0.1883173,
"fields": {
"lastModified": [
1461738451720
],
"dir": [
"/private/dholaria"
],
"filename": [
"Report_google_Private"
]
},
"highlight": {
"filename": [
"Report_google_Private"
]
}
}
]
}
}
现在,我想根据以下条件根据特定 "dir" 字段值过滤上述搜索结果。
当且仅当:
在响应中包含搜索结果
- 如果 "dir" 字段值等于:“/shared”或“/private/hitesh”
- 否则,如果 "dir" 字段值以以下任一开头:“/shared/”或
"/private/hitesh/"
如何在ElasticSearch中实现上述功能?
PS: 下面是我的示例映射。
{
"google_a804f89b-d32e-426a-a79a-ea83d65c98ea": {
"mappings": {
"viz_dashlet": {
"properties": {
"charts": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"columnLabels": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"columnNames": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"creator": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"dimensions": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"dir": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"expressions": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"filename": {
"type": "string",
"index_analyzer": "whitespace_index",
"search_analyzer": "whitespace_search",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"lastModified": {
"type": "date",
"format": "date_hour_minute_second"
},
"measures": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"promptFilters": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
试试这个查询:
{
"query": {
"bool": {
"should": [
{
"term": {
"dir.raw": {
"value": "/shared"
}
}
},
{
"term": {
"dir.raw": {
"value": "/private/hitesh"
}
}
},
{
"match_phrase_prefix": {
"dir.raw": "/shared"
}
},
{
"match_phrase_prefix": {
"dir.raw": "/private/hitesh"
}
}
]
}
}
}
下面是我的示例搜索响应,检索到 4 个结果。
{ "took": 13, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 4, "max_score": 0.41753215, "hits": [ { "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea", "_type": "viz_dashlet", "_id": "/shared/Report_google_Shared", "_score": 0.41753215, "fields": { "lastModified": [ 1461738428007 ], "dir": [ "/shared" ], "filename": [ "Report_google_Shared" ] }, "highlight": { "filename": [ "Report_google_Shared" ] } }, { "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea", "_type": "viz_dashlet", "_id": "/shared/Report_Gmail_Shared", "_score": 0.41753215, "fields": { "lastModified": [ 1461738618676 ], "dir": [ "/shared" ], "filename": [ "Report_Gmail_Shared" ] }, "highlight": { "filename": [ "Report_Gmail_Shared" ] } }, { "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea", "_type": "viz_dashlet", "_id": "/private/hitesh/Report_Gmail_Private", "_score": 0.1883173, "fields": { "lastModified": [ 1461738629888 ], "dir": [ "/private/hitesh" ], "filename": [ "Report_Gmail_Private" ] }, "highlight": { "filename": [ "Report_Gmail_Private" ] } }, { "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea", "_type": "viz_dashlet", "_id": "/private/dholaria/Report_google_Private", "_score": 0.1883173, "fields": { "lastModified": [ 1461738451720 ], "dir": [ "/private/dholaria" ], "filename": [ "Report_google_Private" ] }, "highlight": { "filename": [ "Report_google_Private" ] } } ] } }
现在,我想根据以下条件根据特定 "dir" 字段值过滤上述搜索结果。
当且仅当:
在响应中包含搜索结果- 如果 "dir" 字段值等于:“/shared”或“/private/hitesh”
- 否则,如果 "dir" 字段值以以下任一开头:“/shared/”或 "/private/hitesh/"
如何在ElasticSearch中实现上述功能?
PS: 下面是我的示例映射。
{ "google_a804f89b-d32e-426a-a79a-ea83d65c98ea": { "mappings": { "viz_dashlet": { "properties": { "charts": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "columnLabels": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "columnNames": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "creator": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "dimensions": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "dir": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "expressions": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "filename": { "type": "string", "index_analyzer": "whitespace_index", "search_analyzer": "whitespace_search", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "lastModified": { "type": "date", "format": "date_hour_minute_second" }, "measures": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "promptFilters": { "type": "string", "index_analyzer": "report_index_analyzer", "search_analyzer": "report_search_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } } } } } } }
试试这个查询:
{
"query": {
"bool": {
"should": [
{
"term": {
"dir.raw": {
"value": "/shared"
}
}
},
{
"term": {
"dir.raw": {
"value": "/private/hitesh"
}
}
},
{
"match_phrase_prefix": {
"dir.raw": "/shared"
}
},
{
"match_phrase_prefix": {
"dir.raw": "/private/hitesh"
}
}
]
}
}
}