ElasticSearch入门常用命令

基于开源项目MyAlice智能客服学习ElasticSearch

https://github.com/hpgary/MyAlice/wiki/%E7%AC%AC01%E7%AB%A0%E5%AE%89%E8%A3%85

首先和常用的关系型数据库做一个对比,明确一下概念:

EslaicSearch MySQL   
索引 数据库
type
document
filed
   

HEAD 插件安装:

./bin/plugin install mobz/elasticsearch-head

访问:http://localhost:9200/_plugin/head/

可以通过此插件创建、删除索引、查看索引状态、统计信息、开启/关闭索引、刷新、优化索引;测试分析器;

1、查看集群

http://localhost:19200/

2、查看文档数量

http://localhost:19200/_count?pretty

3、查看健康状态

http://localhost:19200/_cat/health?v

4、查看节点信息

http://localhost:19200/_cat/nodes?v

5、查看索引

http://localhost:19200/_cat/indices?v

6、查看索引下的类型

http://localhost:19200/myalice

对应的mappings字段里面的字段名称就是类型(相当于数据库中的表)

7、列出某索引 类型 下的文档

http://localhost:19200/myalice/question/_search

question是类型

8、在类型中根据title搜索

http://localhost:19200/myalice/question/_search?q=title:%E5%90%83

其中title是文档字段

9、新建type插入文档

curl -XPUT 'localhost:19200/myalice/word/1' -d'{"word":"此外"}'

10、复杂查询

curl -XGET 'localhost:19200/myalice/word/_search?pretty' -d '{"query":{"match":{"text":"此外"}}}'

11、match 和 term的区别

term 不会对搜索词分词,会将带搜索的文档分词,然后将term和分词匹配!

match 会对搜索词分词,也会对搜索的文档分词,两个有交集即可

12、删除数据

curl -XDELETE 'http://localhost:9200/twitter/tweet/1'
安装 delete by query插件:
./bin/plugin install delete-by-query

curl -H "Content-Type: application/json" -XDELETE localhost:9200/uploaddata/uploaddata/_query -d '{"query":{"range":{"createTimeStamp":{"lte":1520611200000}}}}'

13、设置最大查询数目(其中
rowkeys是索引名称
curl -XPUT http://10.38.161.138:9200/rowkeys/_settings -d '{ "index" : { "max_result_window" : 100000000}}'

14、删除索引
curl -XDELETE http://localhost:9200/traindata

15、查看内存占用等指标
http://host:9200/_cat/nodes?v&h=host,heap.current,heap.percent,heap.max,ram.max,disk.avail,node.role,m

16、post查询
curl -H "Content-Type: application/json" -XPOST localhost:9200/uploaddata/_search?pretty=true -d '{"query":{"term":{"pushId":"adm03109522050226131h"}}}'

17、关闭/打开索引:
curl -XPOST http://localhost:9200/sms0/_close
curl -XPOST http://localhost:9200/sms0/_open

 
原文地址:https://www.cnblogs.com/tengpan-cn/p/7221250.html