如果状态为真,则 Elasticsearch bool 查询按日期排序

Posted

技术标签:

【中文标题】如果状态为真,则 Elasticsearch bool 查询按日期排序【英文标题】:Elasticsearch bool query sort by date if status is true 【发布时间】:2020-11-30 09:44:47 【问题描述】:

我在 Elasticsearch 索引中有一个如下所示的 JSON 文件。如果广告未过期且状态为真,我需要对数据进行排序,然后将它们排序为 desc。我怎样才能做到这一点?

我尝试使用 end_date 排序,但它不起作用。我还需要显示 end_date 已过期的所有过期数据。

 advertisement = [
   
        "id": 1,
        "name": "test",
        "status": True,
        "start_date": "2020-08-09",
        "end_date": "2020-09-09",
    ,
   
        "id": 2,
        "name": "test2",
        "status": False,
        "start_date": "2020-08-09",
        "end_date": "2020-08-09",
    ]

这是我的弹性搜索方法。

def elastic_search(category=None):
    client = Elasticsearch(host="localhost", port=9200)
    query_all = 
        'size': 10000,
        'query': 
            "bool": 
                "filter": [
                    
                        "match": 
                            "name": "test"
                        
                    ]
               ,
        ,
        "sort": [
        
            "end_date": 
                "type": "date",
                "order": 'desc'
            
        
    ]
    
    resp = client.search(
        index="my-index",
        body=query_all
        )
    return resp

这是我的回复

http://localhost:9200/my-index/_search

   
   "took":96,
   "timed_out":false,
   "_shards":
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
   ,
   "hits":
      "total":36,
      "max_score":1.0,
      "hits":[
         
            "_index":"my-index",
            "_type":"doc",
            "_id":"52",
            "_score":1.0,
            "_source":
               "id": 1,
               "name": "test",
               "status": True,
               "start_date": "2020-08-09",
               "end_date": "2020-09-09",
            
         ,
         
            "_index":"my-index",
            "_type":"doc",
            "_id":"60",
            "_score":1.0,
            "_source":
               "id": 1,
               "name": "English test",
               "status": True,
               "start_date": "2020-08-09",
               "end_date": "2020-09-09",
            
         ,
         
            "_index":"my-index",
            "_type":"doc",
            "_id":"40",
            "_score":1.0,
            "_source":
               "id": 1,
               "name": "Designw test",
               "status": false,
               "start_date": "2020-08-09",
               "end_date": "2020-09-09",
            
         ,
         
            "_index":"my-index",
            "_type":"doc",
            "_id":"41",
            "_score":1.0,
            "_source":
               "id": 1,
               "name": "Designw New",
               "status": false,
               "start_date": "2020-08-09",
               "end_date": "2020-09-09",
            
         ,
         
            "_index":"my-index",
            "_type":"doc",
            "_id":"59",
            "_score":1.0,
            "_source":
               "id": 1,
               "name": "Designw New",
               "status": false,
               "start_date": "2020-08-09",
               "end_date": "2020-09-09",
            
         ,
         
            "_index":"my-index",
            "_type":"doc",
            "_id":"62",
            "_score":1.0,
            "_source":
               "id": 1,
               "name": "Designw New",
               "status": false,
               "start_date": "2020-08-09",
               "end_date": "2020-09-09",
            
         
      ]
   

这是我的映射 http://localhost:9200/my-index/_mapping 响应。

"my-index":
      "mappings":
         "_doc":
            "properties":
               "address":
                  "properties":
                     "name":
                        "type":"text",
                        "fields":
                           "keyword":
                              "type":"keyword",
                              "ignore_above":256
                           
                        
                     ,
                     "start_date":
                        "type":"text",
                        "fields":
                           "keyword":
                              "type":"keyword",
                              "ignore_above":256
                           
                        
                     ,
                     "end_date":
                        "type":"text",
                        "fields":
                           "keyword":
                              "type":"keyword",
                              "ignore_above":256
                           
                        
                     ,
                     "id":
                        "type":"long"
                     ,
                     "status":
                        "type":"text",
                        "fields":
                           "keyword":
                              "type":"keyword",
                              "ignore_above":256
                           
                        
                     
                  
               
            
         
      
   

【问题讨论】:

你的映射是什么? 需要按过期日期对数据进行排序并排序 再来。你的索引映射是什么? GET my-index/_mapping 再说一次,我不需要您的映射的一般描述。我需要你复制粘贴http://localhost:9200/my-index/_mapping的回复 不是。这是 _search 响应,而不是 _mapping。 【参考方案1】:

关于映射的两件事:

    有一个address 字段在您的实际文档中找不到。删除它。 应使用date 数据类型正确映射您的日期。

正确的映射如下所示:


  "properties":
    "end_date":
      "type":"date",
      "format":"yyyy-MM-dd"
    ,
    "start_date":
      "type":"date",
      "format":"yyyy-MM-dd"
    ,
    //...other properties
  

一旦您获得正确的映射,此查询会查找所有未过期且具有真实状态的广告,并按运行时间最长的广告进行排序:


  "query": 
    "bool": 
      "must": [
        
          "range": 
            "end_date": 
              "gt": "now"
            
          
        ,
        
          "term": 
            "status": 
              "value": true
            
          
        
      ]
    
  ,
  "sort": [
    
      "end_date": 
        "order": "desc"
      
    
  ]

或者,如果您要查找过期的,请将gt 更改为lt,它代表less-than。

【讨论】:

以上是关于如果状态为真,则 Elasticsearch bool 查询按日期排序的主要内容,如果未能解决你的问题,请参考以下文章

python内置函数

JAVA逻辑运算符

shell里面if条件判断

Shell与if相关参数

bash if 表达式含义

(13)文件测试