elasticsearch的基本用法

开始学习使用 elasticsearch, 把步骤记录在这里:

最大的特点:
1. 数据库的 database, 就是 index
2. 数据库的 table, 就是 tag
3. 不要使用browser, 使用curl来进行客户端操作. 否则会出现 java heap ooxx...

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 localhost:9200/films
### Elasticsearch 入门教程:基本操作与使用指南 #### 创建索引 为了使数据能够在Elasticsearch中被查询,首先需要创建一个索引。索引可以视为数据库中的表,在这里定义了文档结构及其映射关系。 ```json PUT /my-index-000001 { "settings": { "number_of_shards": 1, "number_of_replicas": 1 } } ``` 此命令会创建一个新的名为`my-index-000001`的索引,并设置了分片数量和副本数[^2]。 #### 单条写入文档 向已存在的索引内添加新记录的过程称为写入文档。每一条记录都是JSON格式的数据对象,可以通过HTTP POST请求来实现这一过程。 ```json POST /my-index-000001/_doc/1 { "title": "Elasticsearch Basics", "content": "An introduction to using Elasticsearch." } ``` 这条语句表示往指定索引下的 `_doc` 类型里插入了一篇ID为 `1` 的文章[^4]。 #### 批量写入文档 当有大量文档待导入时,采用批量方式能显著提高效率。Bulk API允许一次性提交多个动作(如新增、修改或移除),从而减少网络往返次数。 ```json POST /_bulk { "index" : { "_index" : "my-index-000001", "_id" : "2" } } { "title" : "Advanced Search Techniques", "content":"Exploring advanced search techniques." } { "create" : { "_index" : "my-index-000001", "_id" : "3" } } { "title" : "Data Analysis with ES", "content":"Using Elasticsearch for data analysis tasks." } ``` 以上例子展示了如何利用 bulk 请求连续执行两个不同的索引操作。 #### 更新现有文档 对于已经存在于索引内的文档,如果仅需更改部分内容,则可通过部分更新的方式完成而不必重新上传整个文件。 ```json POST /my-index-000001/_update/1 { "doc": { "tags":["tutorial"] } } ``` 这段代码片段说明了怎样给 ID 为 `1` 的那篇文章增加标签字段。 #### 删除文档 最后,若要彻底清除某份资料,只需发送 DELETE 方法至对应路径即可。 ```json DELETE /my-index-000001/_doc/1 ``` 该指令用于永久性地从系统中去除编号为 `1` 的那份材料。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值