ElasticSearch将多个查询合而为一

时间:2018-10-17 16:36:25

标签: elasticsearch elasticsearch-plugin

我正在使用ElasticSearch,并且想进行查询。

我有不同的模块:class MyModelViewset(ModelViewSet): queryset = SomeModel.objects.all() serializer_class = SomeModelSerializer def get_serializer_context(self): context = super().get_serializer_context() context['my_value'] = [] # calculate something here, you have access to self.request return context class SomeModelSerializer(serializers.ModelSerializer): my_data = serializers.SerializerMethodField() class Meta: model = SomeModel def get_my_data(self, obj): # you have access to self.context['my_value'] # you have access to obj.some_property return my_data 。 其中['a', 'b', 'c', 'd']a有一个额外的值(b)我需要过滤所有模块的文本,但我只需要过滤companyId的{​​{1}} }和companyId不适用于模块ab

这看起来像这样:

c

但是我有这个:

d

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

下面的查询是您要寻找的。我已经实现了OR运算符,即should内的filter

{
  "query": {
    "bool": {
      "must": {
        "match": { "text": "mytext" }
      },
      "filter": [
       {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                      { "match": { "module": "a" }},
                      { "match": { "companyId": "1" }}
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                      { "match": { "module": "b" }},
                      { "match": { "companyId": "1" }}
                   ]
                }
              },
              {
                "bool": {
                  "must": {
                    "match": { "module": "c" }
                  }
                }
              },
              {
                "bool": {
                  "must": {
                    "match": { "module": "d" }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}