1.创建索引库
curl -XPUT http://localhost:9200/test/
2.删除索引库
curl -XDELETE http://localhost:9200/test/
3.插入数据
curl -H "Content-type: application/json" -XPOST localhost:9200/test/5 -d "{\"age\":\"12\",\"user_name\":\"zhangs\",\"name\":\"linda\",\"disc\":{\"a\":\"1\",\"b\":\"2\"}}"
4.修改数据
curl -H "Content-type: application/json" -XPUT localhost:9200/test/5/sr6sxW4BITjAt6wGz_Uq -d "{\"name\":\"update name\",\"disc\":{\"a\":\"1111\",\"b\":\"2\"}}"
注意:(1)sr6sxW4BITjAt6wGz_Uq 是es自动生成的id
(2)id 不能重复
也可以手动规定id
curl -H "Content-type: application/json" -XPUT localhost:9200/test/5/1 -d "{\"name\":\"testid\",\"disc\":{\"a\":\"222\",\"b\":\"2222\"}}"
5.查询
curl -H "Content-type: application/json" -XGET localhost:9200/test/5/1?pretty (没有使用倒排索引)
注意:pretty 格式化返回的数据
curl -H "Content-type: application/json" -XGET localhost:9200/test/5/_search?q=disc.a=1
注意:_search 表示 查询索引内的数据
(1)一个参数的查询:
curl -H "Content-type: application/json" -XGET localhost:9200/test/5/_search?pretty -d "{\"query\":{\"match\":{\"name\":\"testid\"}}}"
(2)对多个field进行查询:要查询的是"linda" 在 "age","gender","name"这个几个属性的范围内
multi_match 表示多重匹配
curl -H "Content-type: application/json" -XGET localhost:9200/test/5/_search?pretty -d "{\"query\":{\"multi_match\":{\"query\":\"linda\",\"fields\":[\"age\",\"gender\",\"name\"],\"operator\":\"and\"}}}"
(3)组合查询
多个term对多个field发起查询:bool(boolean)
组合查询:must,must_not,should
must+must =交集,must+must_not=差集,should+should=并集
交集:curl -H "Content-type: application/json" -XGET localhost:9200/test/5/_search?pretty -d "{\"query\":{\"bool\":{\"must\":{\"match\":{\"age\":\"12\"}},\"must\":{\"match\":{\"user_name\":\"zhaoqn\"}}}}}" age必须是12 user_name必须是zhaoqn
(上述的json格式在window10下面执行json格式转换异常,linux去掉 "\" 应该好用)
差集:curl -H "Content-type: application/json" -XGET localhost:9200/test/5/_search?pretty -d "{\"query\":{\"bool\":{\"must\":{\"match\":{\"age\":\"12\"}},\"must_not\":{\"match\":{\"user_name\":\"zhaoqn\"}}}}}" age必须是12 user_name必须不是zhaoqn
区间查询:curl -H "Content-type: application/json" -XGET localhost:9200/test/5/_search?pretty -d "{\"query\":{\"bool\":{\"must\":{\"term\":{\"user_name\":\"zhaoqn\"}},\"must_not\":{\"range\":{\"age\":{\"from\":14,\"to\":24}}}}}}" user_name是zhaoqn并且age不在14到24之间