下面的查询是我想要使用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,这是我能得到的最接近的结果,但它并不相同。
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()发布于 2015-02-17 23:21:23
解决方案:
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文档中。
发布于 2016-03-19 01:48:35
使用Elasticsearch 2.x (和elasticsearch-dsl > 2.x),你不能再像@thlow1的评论中那样应用过滤器了。相反,你必须通过组合Q来构造你的过滤器:
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])如elasticsearch-dsl documentation中所述,search.query('bool', filter=[combinedQ])应用Q标准作为过滤器。
https://stackoverflow.com/questions/28564303
复制相似问题