如何根据字段值过滤ElasticSearch结果?

时间:2016-04-27 10:29:23

标签: java elasticsearch information-retrieval elasticsearch-2.0

下面是我的示例搜索响应,检索到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 /”或 “/私人/亚太区首席技术官Matt /”

如何在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"
                                }
                            }
                        }
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分: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"
          }
        }
      ]
    }
  }
}