2

下面是我检索到 4 个结果的示例搜索响应。

{
    “接受”:13,
    “超时”:假,
    “_shards”:{
        “总数”:5,
        “成功”:5,
        “失败”:0
    },
    “命中”:{
        “总数”:4,
        “最大分数”:0.41753215,
        “命中”:[
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/shared/Report_google_Shared",
                “_score”:0.41753215,
                “字段”:{
                    “最后修改”: [
                        1461738428007
                    ],
                    “目录”:[
                        “/共享”
                    ],
                    “文件名”: [
                        “Report_google_Shared”
                    ]
                },
                “强调”: {
                    “文件名”: [
                        “ Report_google_Shared ”
                    ]
                }
            },
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/shared/Report_Gmail_Shared",
                “_score”:0.41753215,
                “字段”:{
                    “最后修改”: [
                        1461738618676
                    ],
                    “目录”:[
                        “/共享”
                    ],
                    “文件名”: [
                        “Report_Gmail_Shared”
                    ]
                },
                “强调”: {
                    “文件名”: [
                        “ Report_Gmail_Shared ”
                    ]
                }
            },
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/private/hitesh/Report_Gmail_Private",
                “_score”:0.1883173,
                “字段”:{
                    “最后修改”: [
                        1461738629888
                    ],
                    “目录”:[
                        “/私人/hitesh”
                    ],
                    “文件名”: [
                        “Report_Gmail_Private”
                    ]
                },
                “强调”: {
                    “文件名”: [
                        “ Report_Gmail_Private ”
                    ]
                }
            },
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/private/dholaria/Report_google_Private",
                “_score”:0.1883173,
                “字段”:{
                    “最后修改”: [
                        1461738451720
                    ],
                    “目录”:[
                        “/私人/dholaria”
                    ],
                    “文件名”: [
                        “Report_google_Private”
                    ]
                },
                “强调”: {
                    “文件名”: [
                        “ Report_google_Private ”
                    ]
                }
            }
        ]
    }
}

现在,我想根据以下条件根据特定的“dir”字段值过滤上述搜索结果。

当且仅当以下情况在响应中包含搜索结果:

  • 如果“dir”字段值等于:“/shared”或“/private/hitesh”
  • 否则,如果“dir”字段值以以下任一开头:“/shared/”或“/private/hitesh/”

如何在 ElasticSearch 中实现上述功能?

PS:下面是我的示例映射。

    {
        “google_a804f89b-d32e-426a-a79a-ea83d65c98ea”:{
            “映射”:{
                “viz_dashlet”:{
                    “特性”: {
                        “图表”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “列标签”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “列名”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “创造者”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “方面”: {
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “目录”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “表达式”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “文件名”: {
                            “类型”:“字符串”,
                            “index_analyzer”:“空白索引”,
                            “search_analyzer”:“空白搜索”,
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “最后修改”: {
                            “类型”:“日期”,
                            “格式”:“日期_小时_分钟_秒”
                        },
                        “措施”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        },
                        “提示过滤器”:{
                            “类型”:“字符串”,
                            “index_analyzer”:“report_index_analyzer”,
                            "search_analyzer": "report_search_analyzer",
                            “字段”:{
                                “生的”: {
                                    “类型”:“字符串”,
                                    “索引”:“未分析”
                                }
                            }
                        }
                    }
                }
            }
        }
    }

4

1 回答 1

1

试试这个查询:

{
  "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"
          }
        }
      ]
    }
  }
}
于 2016-04-27T11:53:41.577 回答