Joo*_*orp 8 python dsl boolean elasticsearch
下面的查询是我想用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"
}
}
]
}
}
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用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()
Run Code Online (Sandbox Code Playgroud)
解:
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")]
)
Run Code Online (Sandbox Code Playgroud)
哪个变成:
{
"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":{
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望将来可以将其包含在elasticsearch-dsl-py文档中.
使用Elasticsearch 2.x(和elasticsearch-dsl> 2.x),您无法再在@theslow1的注释中应用过滤器.相反,你必须通过组合Qs 构建你的过滤器:
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])
Run Code Online (Sandbox Code Playgroud)
如弹性搜索-dsl文档中search.query('bool', filter=[combinedQ])所述,将Q标准应用为过滤器.
| 归档时间: |
|
| 查看次数: |
9530 次 |
| 最近记录: |