查询和过滤
可以使用两种结构化语句: 结构化查询(Query DSL)和结构化过滤(Filter DSL)。 查询与过滤语句非常相似,但是它们由于使用目的不同而稍有差异。

复合查询

查全部

  1. GET /ecommerce/product/_search
  2. {
  3. "query": { "match_all": {} }
  4. }

查询不存在的字段

  1. GET index/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must_not": {
  6. "exists":{"field":"filedX"}
  7. }
  8. }
  9. }
  10. }

bool 查询

该模板适用于所有情况,尤其适用于侧边栏多级多条件联合查询

  1. {
  2. "query": {
  3. "bool": {
  4. "must": [
  5. {
  6. "match": {
  7. "countryArea": "United States"
  8. }
  9. },
  10. {
  11. "bool": {
  12. "should": [
  13. {
  14. "match": {
  15. "sponsor": "National Science Foundation"
  16. }
  17. },
  18. {
  19. "match": {
  20. "sponsor": "David and Lucile Packard Foundation"
  21. }
  22. }
  23. ],
  24. "minimum_should_match": 1
  25. }
  26. },
  27. {
  28. "bool": {
  29. "should": {
  30. "nested": {
  31. "path": "researchAreas",
  32. "query": {
  33. "bool": {
  34. "should": [
  35. {
  36. "match": {
  37. "researchAreas.subjectName": "Arts and Humanities"
  38. }
  39. },
  40. {
  41. "match": {
  42. "researchAreas.subjectName": "Ecology"
  43. }
  44. }
  45. ],
  46. "minimum_should_match": 1
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. ],
  54. "must_not": {
  55. "match": {
  56. "fundingType": "Program or Curriculum Development or Provision"
  57. }
  58. }
  59. }
  60. }
  61. }

range

gt: greater than 大于
gte: greater than or equal 大于等于
lt: less than 小于
lte: less than or equal 小于等于

  1. GET /salemgr_user_bigtable/_doc/_search?
  2. {
  3. "from":0,
  4. "query":{
  5. "bool":{
  6. "filter":[
  7. {
  8. "range":{
  9. "data_officeloginon":{
  10. "lt":0
  11. }
  12. }
  13. }
  14. ]
  15. }
  16. },
  17. "size":20
  18. }

bool过滤

一条过滤语句会询问每个文档的字段值是否包含着特定值:

  • created 的日期范围是否在 2013 到 2014 ?
  • status 字段中是否包含单词 “published” ?
  • lat_lon 字段中的地理位置与目标点相距是否不超过10km ?
    1. GET / ecommerce / product / _search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": {
    6. "match": {
    7. "name": "yagao"
    8. }
    9. },
    10. "filter": {
    11. "range": {
    12. "price": {
    13. "gt": 25
    14. }
    15. }
    16. }
    17. }
    18. }
    19. }

    过滤后排序

    1. {
    2. "size":5,
    3. "query":{
    4. "bool":{
    5. "filter":[
    6. {
    7. "range":{
    8. "startTime":{
    9. "from":1517046960000,
    10. "to":1517048760000
    11. }
    12. }
    13. }
    14. ]
    15. }
    16. },
    17. "sort":[
    18. {
    19. "startTime":{
    20. "order":"desc"
    21. }
    22. }
    23. ]
    24. }

    脚本排序

    ```java String str = “(doc[‘heat’].size()==0 ? 0 : doc[‘heat’].value) + “ +
    1. "(doc['unfold'].size()==0 ? 0 : doc['unfold'].value)";
    Script script = new Script(str);

ScriptSortBuilder scriptSortBuilder = SortBuilders .scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER) .order(SortOrder.DESC); searchSourceBuilder.sort(scriptSortBuilder);

  1. <a name="MZr8P"></a>
  2. ## multi_match
  3. `multi_match`查询提供了一个简便的方法用来对多个字段执行相同的查询。
  4. <a name="NSeFp"></a>
  5. # 聚合
  6. <a name="24bOi"></a>
  7. ## 按组内字段排序
  8. AggregationBuilder aggregation =<br /> AggregationBuilders<br /> .terms("agg").field("gender")<br /> .subAggregation(<br /> AggregationBuilders.topHits("top")<br /> .explain(true)<br /> .size(1)<br /> .from(10)<br /> .sort("sortFiled", SortOrder.ASC)<br /> );
  9. <a name="bUTCn"></a>
  10. ## 按聚合后指标排序
  11. 1. 通过key排序
  12. TermsAggregationBuilder termsAggs = AggregationBuilders._terms_("聚合名称").field("分组字段")<br /> .order(BucketOrder._key_(true));
  13. 2. 文档数聚合
  14. .order(BucketOrder.count(true))
  15. <a name="2451b9c1"></a>
  16. ## 根据聚合结果排序 根据字聚合 responseTime.avg
  17. ```json
  18. {
  19. "query" : {
  20. "bool" : {
  21. "filter" : [
  22. {
  23. "term" : {
  24. "type" : {
  25. "value" : "URL",
  26. "boost" : 1.0
  27. }
  28. }
  29. }
  30. ]
  31. }
  32. },
  33. "aggregations" : {
  34. "CATEGORY" : {
  35. "terms" : {
  36. "field" : "name",
  37. "size" : 5,
  38. "order" : {"responseTime.avg" : "asc" }
  39. },
  40. "aggregations" : {
  41. "responseTime" : {
  42. "extended_stats" : {
  43. "field" : "durationInMillis",
  44. "sigma" : 2.0
  45. }
  46. },
  47. "error" : {
  48. "sum" : {
  49. "script" : {
  50. "inline" : "def errorTemp=doc['status'].value; if(errorTemp=='0'){return 0;}else{return 1;}",
  51. "lang" : "painless"
  52. }
  53. }
  54. },
  55. "apdex" : {
  56. "avg" : {
  57. "script" : {
  58. "inline" : "def responseTemp=doc['durationInMillis'].value; if(responseTemp>params.threshold){return 0.5;}else{return 1;}",
  59. "lang" : "painless",
  60. "params" : {
  61. "threshold" : 20.0
  62. }
  63. }
  64. }
  65. },
  66. "errorRate" : {
  67. "percentile_ranks" : {
  68. "script" : {
  69. "inline" : "def errorTemp=doc['status'].value; if(errorTemp=='0'){return 1;}else{return 0;}",
  70. "lang" : "painless"
  71. },
  72. "values" : [
  73. 0.0
  74. ],
  75. "keyed" : true,
  76. "tdigest" : {
  77. "compression" : 100.0
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }

按聚合名称标识对桶进行排序
AggregationBuilders
.terms(“genders”)
.field(“gender”)
.order(BucketOrder.aggregation(“avg_height”, false))
.subAggregation(
AggregationBuilders.avg(“avg_height”).field(“height”)
)

按多个聚合指标对桶进行排序

AggregationBuilders
.terms(“genders”)
.field(“gender”)
.order(BucketOrder.compound( // in order of priority:
BucketOrder.aggregation(“avg_height”, false), // sort by sub-aggregation first
BucketOrder.count(true))) // then bucket count as a tie-breaker
.subAggregation(
AggregationBuilders.avg(“avg_height”).field(“height”)
)

聚合nested

GET audio/info/_search
{
  "size": 0, 
  "query":{
    "bool": {
      "must": [
        {
          "range": {
            "info.inputTime": {
              "gte": "2019-07-01 05:51:47",
              "lte": "2019-07-09 09:59:59"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "wordgroup": {
      "nested": {
        "path": "wordFrequency"
      },
      "aggs": {
        "word": {
          "terms": {
            "field": "wordFrequency.keyword",
            "size": 50,
            "exclude": ["这个"], 
            "order": {
              "wordnum.value": "desc"
            }
          },
          "aggs": {
            "wordnum": {
              "sum": {
                "field": "wordFrequency.count"
              }
            }
          }
        }
      }
    }
  }
}

GET /community_user_bigtable/_doc/_search?{
“size”:0,
“_source”:false,
“aggs”:{
“class_aggs”:{
“terms”:{
“field”:”classid”,
“order”: [{“isfillpaper_sum”: “desc”} ]
},
“aggs”:{
“isfillpaper_sum”:{
“sum”:{
“field”:”isfillpaper”
}
},
“wechat_sum”:{

“sum”:{
“field”:”isaddwechat”
}
},
“login_filter”:{
“filter”:{
“range”: {
“data_logintime”: {
“gte”: 0
}
}
}
},
“login_sum2”:{
“filter”:{
“range”: {
“data_logintime”: {
“lte”: 0
}
}
},
“aggs”:{
“term”:{
“sum”:{
“field”:”isfillpaper”
}
}
}
}
}
}
}
}