创建索引:
post: 127.0.0.1:9200/people
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"man": {
"properties": {
"name": {
"type": "text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
},
"woman":{
}
}
}
为索引添加类型:
post:127.0.0.1:9200/book/novel/_mappings
{
"novel":{
"properties":{
"word_count":{
"type":"integer"
},
"author":{
"type":"keyword"
},
"title":{
"type":"text"
},
"publish_date":{
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"type":"date"
}
}
}
}
为类型添加文档:
post: 127.0.0.1:9200/people/man/1
{
"name": "luoluo",
"country": "China",
"age": 30,
"date": "1993-05-04"
}
修改文档:
post :127.0.0.1:9200/people/man/1/_update
{
"doc":{
"name": "luoluo3333"
}
}
删除文档:
delete: 127.0.0.1:9200/people/man/1
删除索引:
delete: 127.0.0.1:9200/people
查询数据:
get: 127.0.0.1:9200/book/novel/1
查询所有的数据:
post: 127.0.0.1:9200/book/_search
//默认返回10条数据
{
"query":{
"match_all":{}
}
}
数据大小分页查询以及从哪里返回:
post: 127.0.0.1:9200/book/_search
//默认返回10条数据
{
"query":{
"match_all":{}
},
"from": 1,
"size":1
}
对关键词查询:
post: 127.0.0.1:9200/book/_search
{
"query":{
"match":{
"title":"ElasticSearch"
}
}
}
查询的结果排序:
post: 127.0.0.1:9200/book/_search
{
"query":{
"match":{
"title":"ElasticSearch"
}
},
"sort":[
{"publish_date":{"order":"desc"}}
]
}
聚合查询:
post: 127.0.0.1:9200/book/_search
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"word_count"
}
}
}
}
会返回聚合信息:
{
"aggregations": {
"group_by_word_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1000,
"doc_count": 4
},
{
"key": 5000,
"doc_count": 3
},
{
"key": 3000,
"doc_count": 1
},
{
"key": 10000,
"doc_count": 1
}
]
}
}
}
根据多个条件进行聚合查询:
post: 127.0.0.1:9200/book/_search
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"word_count"
}
},
"group_by_publish_date":{
"terms":{
"field":"publish_date"
}
}
}
}
计算:
post: 127.0.0.1:9200/book/_search
{
"aggs":{
"grades_word_count":{
"stats":{
"field":"word_count"
}
}
}
}
返回:
{
"aggregations": {
"grades_word_count": {
"count": 9,
"min": 1000,
"max": 10000,
"avg": 3555.5555555555557,
"sum": 32000
}
}
}
子条件查询:
全文本查询:
模糊匹配:
post: 127.0.0.1:9200/book/_search
{
"query":{
"match":{
"author":"luoluo"
}
}
}
习语匹配:(匹配整个单词,不会拆分)
{
"query":{
"match_phrase":{
"title":"ElasticSearch入门"
}
}
}
多个字段匹配:
{
"query":{
"multi_match":{
"query":"luoluo",
"fields":["author","title"]
}
}
}
语法查询:
会查询出ElasticSearch 和 七十二变的数据
{
"query":{
"query_string":{
"query":"ElasticSearch OR 七十二变"
}
}
}
指定字段:
{
"query":{
"query_string":{
"query":"ElasticSearch OR luoluo",
"fields":["title","author"]
}
}
}
字段级别的查询:
{
"query":{
"term":{
"author":"luoluo"
}
}
}
还支持范围查询:
{
"query":{
"range":{
"word_count":{
"gte":1000,
"lte":2000
}
}
}
}
Filter Context
在查询过程中,只判断该文档是否满足条件,只有Yes或者No
post: 127.0.0.1:9200/book/_search
{
"query":{
"bool":{
"filter":{
"term":{
"word_count":1000
}
}
}
}
}
复合条件查询:
固定分数查询:
{
"query":{
"constant_score":{
"filter":{
"match":{
"title":"入门"
}
},
"boost":2
}
}
}