ElasticSearch操作命令大全

创建索引和映射

1、手动创建索引

curl -XPUT ‘localhost:9200/new-index’

2、获取映射

curl ‘localhost:9200/get-together/_mapping/group?pretty’

定义新映射

curl -XPUT ‘localhost:9200/get-together/_mappering/new-events’ -d ‘{
“new-event”:{
“propeties”:{
“host”:{
“type”: “string”
}}}}’

3、搜索获取数据
ES默认使用了_all字段,索引了所有字段内容

curl ‘localhost:9200/get-together/group/_search?
q=elasticsearch
&field=name,location
&size=1
&pretty’

4、在一个索引,多个类型中搜索(新版本已经废弃type)

curl “localhost:9200/get-together/group,event/_search?q=elasticsearch&pretty”

5、在多个索引中搜索

curl “localhost:9200/get-together,other-index/_search?q=elasticsearch&pretty”

6、在所有索引的某个type下搜索,可以使用_all作为索引的占位符

curl “localhost:9200/_all/type/_search”

7、指定查询的超时时间(默认永不超时)

curl “localhost:9200/get-together/group/_search?q=elasticsearch&pretty&timeout=3s”

8、运行一个类型为query_string的查询

curl ‘localhost:9200/get-together/group/_search?pretty’ -d ‘{
“query”:{
“query_string”:{
“query”: “elasticsearch san francisco”,
“default_field”: “name”,
“default_operatior”:“AND”
}}
}’

9、term查询,如果只在name字段中查找’elastic search’一个词,term查询可能会更快

curl ‘localhost:9200/get-together/group/_search?pretty’ -d ‘{
“query”:{
“term”:{
“name”: “elasticsearch”
}}
}’

10、使用过滤器,如果对搜索的评分不感兴趣,可以使用过滤器查询,过滤器只关心结果是否匹配,并且可以缓存,所以相对更快

curl ‘localhost:9200/get-together/group/_search?pretty’ -d ‘{
“query”:{
“filted”:{
“filter”:{
“term”:{
“name”:“elasticsearch”
}}
}}’

11、聚集查询

curl localhost:9200/get-together/group/_search?pretty -d ‘{
“aggregation”:{
“organizers”:{
“term”:{
“field”:“organizer”}
}
}}’

12、通过ID获取文档

curl ‘localhost:9200/get-together/group/1?pretty’

发布了55 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq422243639/article/details/100578653
今日推荐