如何创建" OR"过滤器使用elasticsearch-dsl-py?

时间:2015-02-17 14:57:21

标签: python elasticsearch boolean dsl

下面的查询是我想用elasticsearch-dsl-py构建的,但我不知道该怎么做。

GET /my_index/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "status": "published"
              }
            },
            {
              "or": {
                "filters": [
                  {
                    "range": {
                      "start_publication": {
                        "lte": "2015-02-17T03:45:00.245012+00:00"
                      }
                    }
                  },
                  {
                    "missing": {
                      "field": "start_publication"
                    }
                  }
                ]
              }
            },
            {
              "or":{
                "filters": [
                  {
                    "range": {
                      "end_publication": {
                        "gte": "2015-02-17T03:45:00.245012+00:00"
                      }
                    }
                  },
                  {
                    "missing": {
                      "field": "end_publication"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

使用elasticsearch-dsl-py,这是我能得到的尽可能接近,但它不一样。 '|'运算符变为'should'子句,而不是'OR'。

    client = Elasticsearch()
    now = timezone.now()

    s = Search(using=client,
               index="my_index"
        ).filter(
            "term", status=PUBLISHED
        ).filter(
            F("range", start_publication={"lte": now}, ) |
            F("missing", field="start_publication")
        ).filter(
            F("range", end_publication={"gte": now}, ) |
            F("missing", field="end_publication")
        )
    response = s.execute()

2 个答案:

答案 0 :(得分:7)

解决方案:

s = Search(using=client,
           index="my_index"
    ).filter(
        "term", status=PUBLISHED
    ).filter(
        "or", [F("range", start_publication={"lte": now}, ),
               F("missing", field="start_publication")]
    ).filter(
        "or", [F("range", end_publication={"gte": now}, ),
               F("missing", field="end_publication")]
    )

变成了:

{  
   "query":{  
      "filtered":{  
         "filter":{  
            "bool":{  
               "must":[  
                  {  
                     "term":{  
                        "status":"published"
                     }
                  },
                  {  
                     "or":{  
                        "filters":[  
                           {  
                              "range":{  
                                 "start_publication":{  
                                    "lte":"2015-02-17T03:45:00.245012+00:00"
                                 }
                              }
                           },
                           {  
                              "missing":{  
                                 "field":"start_publication"
                              }
                           }
                        ]
                     }
                  },
                  {  
                     "or":{  
                        "filters":[  
                           {  
                              "range":{  
                                 "end_publication":{  
                                    "gte":"2015-02-17T03:45:00.245012+00:00"
                                 }
                              }
                           },
                           {  
                              "missing":{  
                                 "field":"end_publication"
                              }
                           }
                        ]
                     }
                  }
               ]
            }
         },
         "query":{  
            "match_all":{  

            }
         }
      }
   }
}

希望将来可以将其包含在elasticsearch-dsl-py文档中。

答案 1 :(得分:7)

使用Elasticsearch 2.x(和elasticsearch-dsl> 2.x),您不能再像在@slow1的评论中那样应用过滤器了。相反,您必须通过组合Q s:

来构建过滤器
search = Search(using=esclient, index="myIndex")
firstFilter = Q("match", color='blue') & Q("match", status='published')
secondFilter = Q("match", color='yellow') & Q("match", author='John Doe')
combinedFilter = firstFilter | secondFilter
search = search.query('bool', filter=[combinedFilter])

search.query('bool', filter=[combinedQ])将Q标准应用为elasticsearch-dsl documentation中所述的过滤器。