elasticsearch基本语句

1.查询语句

范例
1)查询所有
GET   http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"match_all":{}	
	}
}
2)查询某个字段 
GET  http://192.168.56.138:9200/customs/actions/_search
{	
	“query”:{
		"match":{
			”eventCategory“:{
				"query":"href_click"
			}
		}	
	}
}
3)精确查询
GET   http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"term":{
			"eventCategory":"txt_input"
		}
	}
}
4)字段多个值精确查询
GET  http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"terms":{
			"eventCategory": [ "txt_input","href_click" ]
		}
	}
}
5)短语匹配
GET  http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"match_phrase": {
			"msg":"how you"
		}
	}
}
6)前缀查询
GET  http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"match_phrase_prefix": {
			"msg":"how a"
		}
	}
}
7)多列匹配查询
GET   http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"multi_match":{
			"query":"btn_click",
			"fields": [ "eventCategory","msg"]
		}
	}
}
8)范围查询
GET   http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"range":{
			"custid":{
				"gte":15000,
				"lte":20000
			}
		}
	}
}
9)布尔查询(带分值) must语句
GET   http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"bool":{
			"must":[
			{
				"match":{"browse":"chrome:true"}
			},
			{
				"match":{"msg":"hello"}
			}
			]
		}
	}
}
10)布尔查询(不带分值)
{
	"query":{
		"bool":{
	        "filter":[
	        {
	                 "match":{"browse":"chrome:true"}
	        },
	        {
	                 "match":{"msg":"hello"}
	        }
	        ]
		}
    }
}
11)布尔查询(带分值)  should语句
GET  http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
		"bool":{
	        "should":[
	        {
	                 "match":{"browse":"chrome:true"}
	        },
	        {
	                 "match":{"msg":"hello"}
	        }
	        ]
		}
    }
}
12)分页查询
//size为每页数量,from后跟(n-1)*size
GET   http://192.168.56.138:9200/customs/actions/_search
{
	"query":{
        "bool":{
            "filter":[
                    {
                        "match":{"browse":"mozilla:true"}
                    },
                    {
                        "match":{"msg":"hello"}
                    }
                    ]
                }
    },
        "size":3,
        "from":3
}
13)aggs聚合去重查询
GET   http://192.168.56.138:9200/customs/actions/_search
{
	"aggs":{        
        "avg_age":{
            "avg":{
                "field":"age"
        	}
        },
        "sum_age":{
            "sum":{
                "field":"age"
            }
        }
    }
}
14)aggs聚合查询(不显示其他数据)
GET  http://192.168.56.138:9200/customs/actions/_search
{
	"size": 0,
    "aggs":{        
        "avg_age":{
            "avg":{
                "field":"age"
        	}
        },
        "sum_age":{
            "sum":{
                "field":"age"
            }
        }
    }
}
15)使用terms查询name分组
GET  http://192.168.56.138:9200/mydemo2/teststore/_search
{
	"size":0,
    "aggs":{
        "mybulk":{
            "terms":{
                "field":"name"
            }
        }
    }
}
16)使用terms查询say的分组
GET   http://192.168.56.138:9200/mydemo2/teststore/_search
{
    "size":0,
    "aggs":{
        "mycount":{
            "terms":{
                "field":"say"
            }
        }
    }
}

2.添加语句

范例
1)创建索引
PUT  http://192.168.56.138:9200/mydemo2/teststore
{
	"setting":{
		"index":{
			"number_of_shards":2,
			"number_of_replicas":1
		}	
	},
	"mappings":{
		"userinfos":{
			"properties":{
				"userid":{
					"type":"string"
				},
				"username":{
					"type":"string"
				},
				"birthday":{
					"type":"date",
					"format":"yyyy-MM-dd"
				}
			}
		}
	}
}
2)添加数据
POST  http://192.168.56.138:9200/mydemo/userinfos
{
	"userid":"112",
	"username":"wang",
	"birthday":"1980-08-08",
	"say":"he is"
}

3.删除语句

1)删除索引
DELETE    http://192.168.56.150:9200/mydemo2/teststore
2)根据id删除数据(id为1)
DELETE   http://192.168.56.138:9200/mydemo/userinfos/1
3)按查询结果删除
POST  http://192.168.56.138:9200/mydemo/userinfos/_delete_by_query
{
	"query":{
		"match":{
			"userid":"112"
		}
	}
}

3.修改语句

1)修改类型
POST  http://192.168.56.138:9200/mydemo/_mapping/userinfos
{
	"properties":{
		"userid":{
			"type":"integer"
		}
	}
}
2)根据id修改数据
POST  http://192.168.56.138:9200/mydemo/userinfos/1/_update
{
	"doc":{
		"birthday":"1999-06-08"
	}
}
3)更改fielddata数据结构
PUT  http://192.168.56.138:9200/customs/actions
{
	"properties":{
		"eventCategory":{
			"type":"text",
			"fielddata":true
		}
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值