logo头像
Snippet 博客主题

elasticsearch命令操作

1.基本命令

查询elasticsearch的健康信息

curl -XGET http://localhost:9200/_cluster/health?pretty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost]/home/lzq/service/daymem# curl -XGET http://localhost:9200/_cluster/health\?pretty 
{
"cluster_name" : "elasticsearch",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 20,
"active_shards" : 20,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 20,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 50.0
}

创建名为liaozhq的索引

curl -XPUT http://localhost:9200/liaozhq

1
2
[root@localhost]/home/lzq/service/daymem# curl -XPUT http://localhost:9200/liaozhq 
{"acknowledged":true,"shards_acknowledged":true,"index":"liaozhq"}#

删除名为liaozhq的索引

curl -XDELETE http://localhost:9200/liaozhq

1
2
[root@localhost]/home/lzq/service/daymem# curl -XDELETE http://localhost:9200/liaozhq 
{"acknowledged":true}#

2.查询命令和DSL

插入文档到liaozhq这个索引

1
2
3
4
5
6
7
8
9
10
11
PUT /liaozhq/_doc/1
{
"title": "lucene solr and elasticsearch",
"content": "elasitcsearch for search"
}

PUT /liaozhq/_doc/2
{
"title": "lucene solr and elasticsearch",
"content": "elasitcsearch is java development"
}

对应的命令如下

curl -H “Content-Type: application/json” -XPUT http://localhost:9200/liaozhq/_doc/1 -d ‘{“title”:”lucene solr and elasticsearch”, “content”:”elasitcsearch for search”}’

1
2
[root@localhost]/home/lzq/service/daymem# curl -H "Content-Type: application/json" -XPUT http://localhost:9200/liaozhq/_doc/1 -d '{"title":"lucene solr and elasticsearch", "content":"elasitcsearch for search"}'
{"_index":"liaozhq","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}#

curl -H “Content-Type: application/json” -XPUT http://localhost:9200/liaozhq/_doc/2 -d ‘{“title”:”lucene solr and elasticsearch”, “content”:”elasitcsearch is java development”}’

1
2
[root@localhost]/home/lzq/service/daymem# curl -H "Content-Type: application/json" -XPUT http://localhost:9200/liaozhq/_doc/2 -d '{"title":"lucene solr and elasticsearch", "content":"elasitcsearch is java development"}'
{"_index":"liaozhq","_type":"_doc","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}#

查询

命令如下

curl -H “Content-Type: application/json” -XGET http://localhost:9200/liaozhq/_search -d ‘{“query”:{ “match”:{“title”: “lucene java”}}}’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
╭─lzq@localhost.localdomain ~/service/daymem  
╰─➤ curl -H "Content-Type: application/json" -XGET http://localhost:9200/liaozhq/_search -d '{"query":{ "match":{"title": "lucene java"}}}'

{"took":28,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":

{"total":2,"max_score":0.2876821,"hits":

[{"_index":"liaozhq","_type":"_doc","_id":"2","_score":0.2876821,"_source":{"title":"lucene solr and elasticsearch", "content":"elasitcsearch is java development"}},

{"_index":"liaozhq","_type":"_doc","_id":"1","_score":0.2876821,"_source":{"title":"lucene solr and elasticsearch", "content":"elasitcsearch for search"}}]}}%

╭─lzq@localhost.localdomain ~/service/daymem
╰─➤ curl -H "Content-Type: application/json" -XGET http://localhost:9200/liaozhq/_search -d '{"query":{ "match":{"title": "java"}}}'
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%
微信打赏