Elasticsearch使用不同的bool查询多个类型

时间:2016-04-11 15:40:22

标签: elasticsearch types boolean-logic

我有一个包含3种不同类型内容的索引:['媒体',' group',user']我需要在三个位置进行搜索相同类型,但在添加到结果列表之前请求其中一个必须完成的额外参数。

这是我当前的索引数据:

{
   "settings": {
      "analysis": {
         "filter": {
            "nGram_filter": {
               "type": "nGram",
               "min_gram": 2,
               "max_gram": 20,
               "token_chars": [
                  "letter",
                  "digit",
                  "punctuation",
                  "symbol"
               ]
            }
         },
         "analyzer": {
            "nGram_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding",
                  "nGram_filter"
               ]
            },
            "whitespace_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding"
               ]
            }
         }
      }
    },
    "mappings": {
        "media": {
            "_all": {
                "analyzer": "nGram_analyzer",
                "search_analyzer": "whitespace_analyzer"
            },
            "properties": {
                "UID": {
                   "type": "integer",
                   "include_in_all": false
                },
                "addtime": {
                   "type": "integer",
                   "include_in_all": false
                },
                "title": {
                   "type": "string",
                   "index": "not_analyzed"
                }
            }
        },
        "group": {
            "_all": {
                "analyzer": "nGram_analyzer",
                "search_analyzer": "whitespace_analyzer"
            },
            "properties": {
                "UID": {
                   "type": "integer",
                   "include_in_all": false
                },
                "name": {
                   "type": "string",
                   "index": "not_analyzed"
                },
                "desc": {
                   "type": "string",
                   "include_in_all": false
                }
            }
        },
        "user": {
            "_all": {
                "analyzer": "nGram_analyzer",
                "search_analyzer": "whitespace_analyzer"
            },
            "properties": {
                "addtime": {
                   "type": "integer",
                   "include_in_all": false
                },
                "username": {
                   "type": "string"
                }
            }
        }
    }
}

所以目前我可以用

搜索所有索引
{
    query: {
      match: {
        _all: {
                "query": "foo",
                "operator": "and"
            }
      }
    }
}

并使用单词" foo"获取媒体,群组或用户的结果在它上面,这很棒,但我需要删除用户不是结果所有者的所有媒体。所以我想我需要做一个bool查询,在那里设置" must"子句并添加' UID'变量到当前用户ID。

我的问题是如何执行此操作以及如何指定过滤器只能在一种类型上工作而不会保持其他类型。

我还没有找到Elastic Search文档的答案

1 个答案:

答案 0 :(得分:1)

最后,我能够通过遵循安德烈的评论来实现这一目标。我知道它并不完美,因为我必须添加一个带有类型" group"和"用户",但它完全符合我的设计,因为我需要在这些上加上更多的过滤器。建议搜索最终会变慢。

['username']