ElasticSearch使用入门-增删查改

来源:互联网 发布:2015年江苏省旅游数据 编辑:程序博客网 时间:2024/06/05 07:57

  ElasticSearch入门命令和ElasticSearch的Java API的使用可以去官网查看。ElasticSearch的安装见另外一篇笔记。

Url文档

curl: -X 后面跟 RESTful : GET, POST …
-d 后面跟数据。 (d = data to send)

1. create:

指定 ID 来建立新记录。 (貌似PUT, POST都可以)

$ curl -XPOST localhost:9200/films/md/2 -d '{ "name":"hei yi ren", "tag": "good"}' 

使用自动生成的 ID 建立新纪录:

$ curl -XPOST localhost:9200/films/md -d '{ "name":"ma da jia si jia3", "tag": "good"}' 

2. 查询:

2.1 查询所有的 index, type:
$ curl localhost:9200/_search?pretty=true 
2.2 查询某个index下所有的type:
$ curl localhost:9200/films/_search 
2.3 查询某个index 下, 某个 type下所有的记录:
$ curl localhost:9200/films/md/_search?pretty=true 
2.4 带有参数的查询:
$ curl localhost:9200/films/md/_search?q=tag:good {"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"film","_type":"md","_id":"2","_score":1.0, "_source" : { "name":"hei yi ren", "tag": "good"}},{"_index":"film","_type":"md","_id":"1","_score":0.30685282, "_source" : { "name":"ma da jia si jia", "tag": "good"}}]}} 
2.5 使用JSON参数的查询: (注意 query 和 term 关键字)
$ curl localhost:9200/film/_search -d '{"query" : { "term": { "tag":"bad"}}}' 

3. update

$ curl -XPUT localhost:9200/films/md/1 -d { ...(data)... } 

4. 删除某条记录

$ curl -XDELETE 'http://localhost:9200/everhomesv3/topic/195205'

4. 删除所有的:

$ curl -XDELETE localhost:9200/films 

Java API

下面是建立一个查询的对象依赖,具体例子见官网。

原创粉丝点击