Elasticsearch 搜索 API
目录搜索多个索引
# 未指定文档时,返回的是所有索引的文档
# 默认返回 10 个文档
GET /_search
# 返回 20 个文档
GET /_search?size=20
# 对多个索引进行搜索
GET /twitter,test,catalog/_search
# 搜索以 index 开头的索引并且排除 index3
GET /index*,-index3/_search
搜索单个索引
# 搜索单个文档
# 默认返回 10 个文档
GET twitter/_search
# 分页查询
# from 从 0 开始
GET twitter/_search?size=2&from=2
# DFS 查询(Elasticsearch 的领域特定语言),分页查询
GET twitter/_search
{
"size": 2,
"from": 2,
"query": {
"match_all": {}
}
}
只返回特定字段
# 只显示特定字段
# 只显示总数
GET twitter/_search?filter_path=hits.total
# 只返回评分和 city 字段
GET twitter/_search?filter_path=hits.hits._score,hits.hits._source.city
# 只返回 user 和 city
GET twitter/_search
{
"_source": ["user", "city"],
"query": {
"match_all": {}
}
}
# 只返回 user 和 city
# includes 和 excludes 参数支持通配符
GET twitter/_search
{
"_source": {
"includes": ["user", "city"]
},
"query": {
"match_all": {}
}
}
# 使用 fields 指定返回字段,不返回 _source
GET twitter/_search
{
"_source": false,
"fields": ["user", "city"],
"query": {
"match_all": {}
}
}
统计文档数量
# 统计文档总数
GET twitter/_count
# 根据条件统计文档总数
GET twitter/_count
{
"query": {
"match": {
"city": "北京"
}
}
}
查询索引配置
# 查询索引配置
GET twitter/_settings
# 配置索引,如果索引已经存在,需要删除后重新再索引
PUT twitter1
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
修改索引 Mapping
Elasticsearch 是 schemaless 的(无须预先创建索引和 Mapping 即可插入文档)
自动识别的字段类型可能不准确,如果知道要插入文档的字段类型,可以预先创建文档
# 查询索引 Mapping
GET twitter/_mapping
修正字段类型需要删除索引后在重新创建索引
# localtion 被自动识别成 text 类型,将其修正成 geo_point 类型
DELETE twitter
PUT twitter
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
PUT twitter/_mapping
{
"properties": {
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"country": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"location": {
"type": "geo_point"
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"province": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"uid": {
"type": "long"
},
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
GET twitter/_mapping
# 手动创建索引后,批量插入文档
POST _bulk
{"index":{"_index":"twitter","_id":1}}
{"user":"双榆树-张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{"index":{"_index":"twitter","_id":2}}
{"user":"东城区-老刘","message":"出发,下一站云南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}}
{"index":{"_index":"twitter","_id":3}}
{"user":"东城区-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}}
{"index":{"_index":"twitter","_id":4}}
{"user":"朝阳区-老贾","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{"index":{"_index":"twitter","_id":5}}
{"user":"朝阳区-老王","message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{"index":{"_index":"twitter","_id":6}}
{"user":"虹桥-老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}