es常用的各种查询语句:

1.通过id且设置显示的字段查询
{ "_source" : {
    "includes" : [
      "id",
      "title"
    ],
    "excludes" : [ "content"]
  },
  "query": {
    "term": {
      "id": "282f85c59020c903e8adff91802815f0"
    }
  }
}
2.通过时间范围查询://注意日期加引号
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createtime": {
	     "format": "yyyy-MM-dd HH:mm:ss",
              "from": "2017-12-14 16:25:26",
              "to": "2017-12-14 17:25:26",
              "include_lower": true,
              "include_upper": false
            }
          }
        }
      ]
    }
  }
}
或者
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createtime": {
              "gt": "2017-12-14 16:25:26",
              "lte": "2017-12-14 17:25:26",
              "include_lower": true,
              "include_upper": false
            }
          }
        }
      ]
    }
  }
}
3.通过数字区间范围查询
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "similarValue": {
              "from": 0.1,
              "to": 1,
              "include_lower": true,
              "include_upper": true
            }
          }
        }
      ]
    }
  }
}
4.通过多条件查询,并且是“与”关系
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "similarValue": {
              "from": 0.1,
              "to": 1,
              "include_lower": true,
              "include_upper": true
            }
          }
        },
        {
          "term": {
            "dataSource": "财新网"
          }
        }
      ]
    }
  }
}
5.带分页的模糊查询
title字段:分词
{
  "from": 10,
  "size": 20,
  "query": {
    "match": {
      "title": "中国杭州"
    }
  }
}
contentType:不分词,必须全词条匹配
{
  "query": {
    "match": {
      "contentType": "企业新闻" 
    }
  }
}
6.带操作符的模糊查询,该参数的默认值是"or",变为"and"来要求所有的词条都需要被匹配,提高精确度。
{
  "query": {
    "match": {
      "title": {
        "query": "中大国际",
        "operator": "and"
      }
    }
  }
}
或通过百分比,来控制相关匹配度。
{
  "query": {
    "match": {
      "title": {
        "query": "中大国际",
        "minimum_should_match": "75%"
      }
    }
  }
}
7.match_phrase短语查询
{
  "from": "10",
  "size": "30",
  "query": {
    "match_phrase": {
      "title": {
        "query": "中国发展",
        "slop": 1
      }
    }
  }
}
另一种写法:
{
  "query": {
    "match": {
      "title": {
        "query": "中国发展",
        "type": "phrase"
      }
    }
  }
}
8.bool 联合查询
{
  "query": {
    "bool": {
      "must": {
        "match_phrase": {
          "content": "宝马"
        }
      },
      "must_not": {
        "term": {
          "contentType": "企业新闻"
        }
      }
    }
  }
}
9. 带分页带排序
{
  "from": "0",
  "size": "10",
  "sort": [
    {
      "pubdate": "asc"
    }
  ],
  "query": {
    "bool": {
      "must": {
        "match_phrase": {
          "content": "宝马"
        }
      },
      "must_not": {
        "term": {
          "contentType": "企业新闻"
        }
      }
    }
  }
}
10 range 范围查询
{
  "query": {
    "range": {
      "pubdate": {
        "gte": "2017-10-23"
      }
    }
  }
}
11.多个bool关系的查询
{
  "from": 10,
  "size": 30,
  "_source": {
    "includes": [
      "id",
      "subLabelTypes",
      "subEnterpriseName"
    ]
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "contentType": "企业风控"
          }
        },
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "subLabelTypes": "延期付款付息"
                }
              },
              {
                "match_phrase": {
                  "subLabelTypes": "变更募资用途"
                }
              },
              {
                "match_phrase": {
                  "subLabelTypes": "延迟披露财报"
                }
              },
              {
                "match_phrase": {
                  "subLabelTypes": "兑付存在不确定性"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
11.聚合操作,相当于mysql中的group by,求dataSource有哪些类型
{
  "size": 0,
  "aggs": {
    "agg_data": {
      "terms": {
        "field": "dataSource",
        "size":1000
      }
    }
  }
}
12.求的最大值similarValue
{
  "size": 0,
  "aggs": {
    "agg_sim": {
      "sum": {
        "field": "similarValue"
      }
    }
  }
}
13.求dataSource的所有数据类型中similarValue的最小值是?

{
  "size": 0,
  "aggs": {
    "agg_data": {
      "terms": {
        "field": "dataSource",
       "size":1000
      }
    },
    "agg_similarValue": {
      "sum": {
        "field": "similarValue"
        
      }
    }
  }
}
14.求datasource中各个数据类型的最小值
{
  "size": "0",
  "aggregations": {
    "agg_data": {
      "terms": {
        "field": "dataSource",
        "size": 1000
      },
      "aggregations": {
        "agg_similarValue": {
          "sum": {
            "field": "similarValue"
          }
        }
      }
    }
  },
  "ext": {}
}

15.contentType为企业风控的数据中,similarValue的平均值
{
  "query": {
    "term": {
      "contentType": "企业风控"
    }
  },
  "size": 0,
  "aggs": {
    "agg_age": {
      "avg": {
        "field": "similarValue"
      }
    }
  }
}
16 查询某个企业中企业风控数据类型,在各个时间段入库数量
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "contentType": {
              "value": "企业风控"
            }
          }
        },
        {
          "match_phrase": {
            "subEnterpriseName": {
              "query": "乐视网信息技术(北京)股份有限公司"
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "agg_date": {
      "date_range": {
        "field": "createtime",
        "format": "yyyy-MM-dd HH:mm:ss",
        "ranges": [
          {
            "to": "2017-12-31 23:59:59"
          },
          {
            "from": "2018-01-01 01:00:00",
            "to": "2018-01-09 00:00:00"
          },
          {
            "from": "2018-01-10 00:00:00"
          }
        ]
      }
    }
  }
}
17.企业风控数据类型,各个相似值的数量
{
  "query": {
    "term": {
      "contentType": "企业风控"
    }
  },
  "size": "0",
  "aggs": {
    "agg_similarValue": {
      "range": {
        "field": "similarValue",
        "ranges": [
          {
            "to": "0.6"
          },
          {
            "from": 0.6,
            "to": 0.9
          },
          {
            "from": "1"
          }
        ]
      }
    }
  }
}
18.查询各个标签的数量 ,按统计量倒序排列
{
  "size": 0,
  "aggs": {
    "agg_data": {
      "terms": {
        "field": "subLabelTypes.subLabelTypes_raw",
        "size": 1000000,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
19 查询某个企业在某个时间段的分组
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "contentType": "企业风控"
          }
        },
        {
          "match_phrase": {
            "subEnterpriseName": "乐视网信息技术(北京)股份有限公司"
          }
        },
        {
          "range": {
            "createtime": {
              "gte": "2018-01-11 00:25:26",
              "lte": "2018-01-12 17:25:26",
              "include_lower": true,
              "include_upper": true
            }
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "agg_data": {
      "terms": {
        "field": "subLabelTypes.subLabelTypes_raw",
        "size": 1000,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
20 以年月日,进行分组统计
以天:
{
  "size": 0,
  "aggs": {
    "agg_age": {
      "date_histogram": {
        "field": "createtime",
        "interval": "day",
        "format": "yyyy-MM-dd",
        "min_doc_count": 1,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
以月:
{
  "size": 0,
  "aggs": {
    "agg_age": {
      "date_histogram": {
        "field": "createtime",
        "interval": "month",
        "format": "yyyy-MM",
        "min_doc_count": 1,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
以年:
{
  "size": 0,
  "aggs": {
    "agg_age": {
      "date_histogram": {
        "field": "createtime",
        "interval": "year",
        "format": "yyyy",
        "min_doc_count": 1,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
21.查询某个企业数据类型为“企业风控”的数据以天为单位统计每种标签类型的数据量。如果按时间排序将order中_count改为_key
//"min_doc_count":1; 值>=1的数据才显示
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "contentType": "企业风控"
          }
        },
        {
          "match_phrase": {
            "subEnterpriseName": "乐视网信息技术(北京)股份有限公司"
          }
        }
	,
        {
          "range": {
            "createtime": {
              "from": "2017-12",
              "to": "2018-01",
              "format": "yyyy-MM",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "agg_date": {
      "date_histogram": {
        "field": "createtime",
        "interval": "day",
        "format": "yyyy-MM-dd",
        "min_doc_count": 1,
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "agg_lable": {
          "terms": {
            "field": "subLabelTypes.subLabelTypes_raw",
            "size": 1000,
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}

22.以年
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "contentType": "企业风控"
          }
        },
        {
          "match_phrase": {
            "subEnterpriseName": "乐视网信息技术(北京)股份有限公司"
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "agg_date": {
      "date_histogram": {
        "field": "createtime",
        "interval": "year",
        "format": "yyyy",
        "min_doc_count": 1,
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "agg_lable": {
          "terms": {
            "field": "subLabelTypes.subLabelTypes_raw",
            "size": 1000,
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}

23
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "contentType": "企业风控"
          }
        },
        {
          "match_phrase": {
            "subEnterpriseName": "乐视网信息技术(北京)股份有限公司"
          }
        },
        {
          "range": {
            "createtime": {
              "from": "2017-11",
              "to": "2018-02",
              "format": "yyyy-MM",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "agg_date": {
      "date_histogram": {
        "field": "createtime",
        "interval": "month",
        "format": "yyyy-MM",
        "min_doc_count": 1,
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "agg_lable": {
          "terms": {
            "field": "subLabelTypes.subLabelTypes_raw",
            "size": 1000,
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}





DSL 各种查询例子:
http://blog.csdn.net/wthfeng/article/details/52953317

{
  "size": 0,
  "aggs": {
    "agg_data": {
      "terms": {
        "field": "subLabelTypes.subLabelTypes_raw",
        "size": 1000000,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}

不包含某个标签


{
  "_source": {
    "includes": [
      "id",
      "subLabelTypes"
    ]
  },
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createtime": {
              "format": "yyyy-MM-dd HH:mm:ss",
              "from": "2018-05-08 00:25:26",
              "to": "2018-05-08 23:25:26",
              "include_lower": true,
              "include_upper": false
            }
          }
        },
        {
          "term": {
            "contentType": "企业风控"
          }
        }
      ],
      "must_not": {
        "match_phrase": {
          "subLabelTypes": "企业风险"
        }
      }
    }
  }
}



 

你可能感兴趣的:(elasticsearch)