一、精确查询某个字段
1、两个查询条件是and的关系
查询text类型时,需要在字段后面加上.keyword,如果字段为keyword类型,则不需要加
{
"size": 20, //查询结果显示个数
"query": {
"bool": {
"must": [
{
"term": {
"description.keyword": { //字段名称
"value": "投资咨询" //字段值
}
}
},
{
"term": {
"tags_v2.id": {
"value": "tag-ec0dcc7af3"
}
}
}
]
}
}
}
查询nested类型字段时,需要加上子字段或者使用下面的查询语句
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "tags_v2", //字段名称
"query": {
"term": {
"tags_v2.id": {
"value": "tag-ec0dcc7af3"
}
}
}
}
}
]
}
}
}
2、两个查询条件是or的关系
{
"query": {
"bool": {
"should": [
{
"term": {
"app_name": {
"value": "知泸水"
}
}
},
{
"term": {
"app_type": {
"value": "应用"
}
}
}
]
}
}
}
3、多个字段多个逻辑关系
以下三个字段的关系是:bid_type and (address3 or address4)
{
"query": {
"bool": {
"must": [
{
"term": {
"bid_type": {
"value": "中标公告"
}
}
},
{
"bool": {
"should": [
{
"term": {
"address3": {
"value": "370100"
}
}
},
{
"term": {
"address4": {
"value": "370215"
}
}
}
]
}
}
]
}
}
}
二、查询不等于这个值的数据
{
"query": {
"bool": {
"must_not": [
{
"term": {
"address1": {
"value": "150"
}
}
}
]
}
}
}
三、模糊查询某个字段
1、查询以某个词语开头(description字段以投资咨询开头)
{
"query": {
"bool": {
"must": [
{
"prefix": {
"description.keyword": {
"value": "投资咨询",
"boost": 10
}
}
}
]
}
}
}
2、查询包含某个词语(description字段包含投资咨询)
{
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"description": {
"query": "投资咨询"
}
}
}
]
}
}
}
四、查询是否存在这个字段
1、查询存在这个字段的数据
{
"query": {
"exists": {
"field": "coop_industry1"
}
}
}
2、查询不存在这个字段的数据
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "logo"
}
}
]
}
}
}
五、复合查询
下列语句查询逻辑是cid等于1631880并且attention_num在[50,60]之间并且data_status为0或者1的数据
{
"query":{
"bool":{
"must":{
"term":{
"cid":"1631880"
}
},
"must_not":{
"range":{
"attention_num":{
"gte":60,
"lte":50
}
}
},
"should":[
{
"term":{
"data_status": 0
}
},
{
"term":{
"data_status": 1
}
}
]
}
}
}
六、排序
{
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"description": {
"query": "投资咨询"
}
}
}
]
}
},
"sort": [
{
"address2": {
"order": "desc" //降序
},
"address3": {
"order": "asc" //升序
}
}
]
}
七、聚合查询
1、对keywork,long等字段进行聚合
{
"size": 1,
"aggs": {
"capital_ratio": { //随便写
"terms": {
"field": "capital_ratio", //字段名称
"size": 10, //聚合结果显示个数
"order": {
"_term": "asc" //按照升序排列
}
}
}
}
}
2、对nested字段进行聚合
{
"aggs": {
"coop_industry1_aggs": { //随便写
"nested": {
"path": "coop_industry1" //字段名称
},
"aggs": {
"coop_industry1": { //随便写
"terms": {
"field": "coop_industry1.name", //字段名称
"size": 10 //聚合后结果显示个数
}
}
}
}
}
}
3、对时间类型聚合查询
{
"query": {
"bool": {
"must": [
{
"range": {
"found_date": {
"gte": 1533556800000,
"lte": 1533806520000
}
}
}
]
}
},
"size": 0,
"aggs": {
// 自己取的聚合名字,随便写
"found_date": {
// es提供的时间处理函数
"date_histogram": {
// 需要聚合分组的字段名称, 字段类型需要为date
"field": "found_date",
// 按什么时间段聚合, 这里是30分钟, 可用的interval在上面给出
"interval": "30m",
// 设置时区, 这样就相当于东八区的时间
"time_zone": "+08:00",
// 返回值格式化,HH大写,不然不能区分上午、下午
"format": "yyyy-MM-dd HH",
// 默认为0,桶中的数量大于min_doc_count
"min_doc_count": 0
}
}
}
}
版权声明:本文为qq_39234967原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。