中文文档
1、新增
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/index_test/test_type/1?pretty' -d ' # 这里的pretty参数的作用是使得返回的json显示地更加好看。1是文档的id值(唯一键)。
{
"name": "zhangsan",
"age" : "12"
}'
2、查询
1. 按ID查询单个文档
curl -u root:pwd http://127.0.0.1:9200
curl -XGET 'localhost:9200/index_test/test_type/1?pretty'
2. 按条件查询
curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy&pretty=true'
twitter是索引的名称
tweet是类型的名称
pretty是将返回的信息以可读的JSON形式返回
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 36,
"max_score": 7.3576975,
"hits": [
{
"_index": "index_t",
"_type": "t",
"_id": "cJVupGkBzU6g49Db24A7",
"_score": 7.3576975,
"_source": {
"datetime": "2019年03月19日20时-03月20日02时",
"city": "天津市区",
"time": "2019031917",
"desc": "多云,降水概率20%,南风3-4级"
}
},....
took:是查询花费的时间,毫秒单位
time_out:标识查询是否超时
_shards:描述了查询分片的信息,查询了多少个分片、成功的分片数量、失败的分片数量等
hits:搜索的结果,total是全部的满足的文档数目,hits是返回的实际数目(默认是10)
_score是文档的分数信息,与排名相关度有关。
#查询多个匹配条件,size不指定,那么默认返回10条
http://localhost:9200/index_t/t/_search?q=time:2019031917 AND city=tianjin&size=100
curl -XPOST 'http://localhost:9200/index_t/_search?pretty=true' -d '
{
"query": {
"bool": {
"must":[
{"match":{"time":"2019031917"}},
{"match":{"city":"天津市区"}}
]
}
}
}'
#from从0开始,请求了第10-30的文档
http://localhost:9200/index_t/_search?q=time:2019031917 AND city=tianjin&from=10&size=20
curl -XPOST 'localhost:9200/index_t/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 20
}'
#match_all搜索全部的文档
#指定文档返回的排序方式
curl -XPOST 'localhost:9200/index_t/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}'
查询数值范围1536854400000<=time<=1536940800000
{
"query": {
"range": {
"time": {
"gte": 1536854400000,
"lte": 1536940800000
}
}
}
}
查询1536854400000<=time<=1536940800000 and Station_Id_C=‘54464’
{
"query": {
"bool": {
"must": [
{"match": {
"Station_Id_C": "54464"
}},
{
"range": {
"time": {
"gte": 1536854400000,
"lte": 1536940800000
}
}
}
]
}
}
}
3.模糊查询
{
"query": {
"wildcard" : { "disasterName2.keyword" : "*雷电*" }
}
}
或
注意:Fuzzy Query将在6.0中正式移除,使用匹配查询替代模糊查询。
{
"query": {
"fuzzy" : {
"disasterName2" : {
"value" : "雷电",
"fuzziness" : 1
}
}
}
}
3、查看所有索引
curl -XGET 'http://localhost:9200/_cat/indices?v'
4、查看某个索引
curl -XGET 'http://localhost:9200/index'
5、删除索引
curl -XDELETE 'http://localhost:9200/index?pretty'
删除id的索引
curl -XDELETE 'http://localhost:9200/index/type/{id}'
6、删除部分记录,不删除索引
curl -X POST "localhost:9200/index/type/_delete_by_query" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {
}
}
}
{
"query": {
"range": {
"time": {
"gte": 1586398563000
}
}
}
}
7、time倒序排列
{
"query": {
"match_all": {
}
},
"sort": [
{
"time": {
"order": "desc"
}
}
]
}
8、查询location字段不存在的记录,或存在(must)
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "location"
}
}
}
}
}
type!=‘hrrr’
{
"query": {
"bool": {
"must_not": {
"match": {
"type": "hrrr"
}
}
}
}
}
9、更新所有
PUT /index/type/id
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
10、更新filename,修改为2019071007-yqgb.docx
POST /index/type/id/_update
{
"doc": {
"filename": "2019071007-yqgb.docx"
}
}
11、更新后,删除interests字段
POST /index/type/id/_update
{
"script": "ctx._source.remove(\"interests\")"
}
12、更新后,增加tel字段
POST /index/type/id/_update
{
"script": "ctx._source.tel=\"158111\""
}
13、创建索引
PUT /test
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
14、自动创建索引
PUT _cluster/settings
{
"persistent": {
"action.auto_create_index": "true"
}
}
15、查询时间字段
{
"query": {
"range": {
"date": {
"gte": "2019-01-01",
"lte": "2019-01-31"
}
}
}
}
16、增加索引字段
PUT indexName/_mapping/indexType
{
"properties": {
"I": {
"type": "float"
}
}
}
17、in查询
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"range": {
"time": {
"from": 1637107200000,
"to": 1637107200000
}
}
},
{
"terms": {
"Station_Id_C": [
"58360",
"54301"
]
}
}
]
}
}
]
}
}
}
String[] stas = staIds.split(",");
shouldQuery.must(QueryBuilders.termsQuery("Station_Id_C",stas));