如何创建一个“或”过滤器使用elasticsearch-dsl-py? [英] How do I create an "OR" filter using elasticsearch-dsl-py?
本文介绍了如何创建一个“或”过滤器使用elasticsearch-dsl-py?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的查询是我想使用elasticsearch-dsl-py构造的,但是我不知道该怎么做。
The query below is what I would like to construct using elasticsearch-dsl-py, but I do not know how to do it.
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'。
Using elasticsearch-dsl-py, this is as close as I can get, but it is not the same. The '|' operator is turns into 'should' clauses, instead of '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()
推荐答案
解决方案:
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文档中。
Hopefully this can be included in the elasticsearch-dsl-py documentation in the future.
这篇关于如何创建一个“或”过滤器使用elasticsearch-dsl-py?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文