es查询时的自定义排序和过滤:

1)排序

核心:sort字段指定排序;

1、sort字段指定排序,

倒序:”sort”: { “tftxtime”: { “order”: “desc” }}

查询条件格式如下:

  1. {
  2. "query": {
  3. "bool": {
  4. "must": [],
  5. "must_not": [{
  6. "term": {
  7. "flow_yh_yh_location": ""
  8. }
  9. },
  10. {
  11. "term": {
  12. "routemcc": "1111"
  13. }
  14. },
  15. {
  16. "term": {
  17. "routemcc": "2222"
  18. }
  19. }
  20. ],
  21. "should": [{
  22. "match_all": {}
  23. }]
  24. }
  25. },
  26. "from": 0,
  27. "size": 10,
  28. "sort": {
  29. "tftxtime": {
  30. "order": "desc"
  31. }
  32. },
  33. "aggs": {}
  34. }

2、ES支持多级排序— sort字段:
  1. 将查询结果首先按第一个自定义条件排序,当第一个 sort 值完全相同时,再按照第二个条件进行排序,以此类推。
GET /_search
{
    "query" : {
        "bool" : {
            "must":   { "match": { "tweet": "manage text search" }},
            "filter" : { "term" : { "user_id" : 2 }}
        }
    },
    "sort": [
        { "date":   { "order": "desc" }},  // 用date来进行查询结果的排序;
        { "age": { "order": "desc" }}  // 当date相同时,再用age来进行排序;
    ]
}

2) 范围查询

range : 实现范围查询

参数:from, to, include_lower, include_upper, boost

include_lower:是否包含范围的左边界,默认是true

include_upper:是否包含范围的右边界,默认是true

GET /lib3/user/_search 

{ 

     "query": { 

          "range": { 

               "birthday": { 

                    "from": "1990-10-10", 

                    "to": "2018-05-01" 

               } 

          } 

     } 

}

GET /lib3/user/_search 

{ 

     "query": { 

          "range": { 

               "age": { 

                    "from": 20, 

                    "to": 25, 

                    "include_lower": true, 

                    "include_upper": false 

               } 

          } 

     } 

}

3)过滤:

terms —— 过滤(多个值)

            terms    跟    term    有点类似,但    terms    允许指定多个匹配条件。

        即,如果某个字段指定了多个值,那么文档需要一起去做匹配:<br />

es对查询结果的排序 - 图1

如上图所示:

    过滤掉:带有“search”,“full_text”,“nosql” 这三个tag标签的数据们;