elasticsearch入门,linux上简单操作

来源:互联网 发布:程序员项目经验 编辑:程序博客网 时间:2024/05/20 07:51

福利:

入门视频点击此处,注册账号看前几章,入门还是没问题的


入门操作:

说明:我这里新建的名称—index为test,type为kind

1、创建索引

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

补充,更详尽教程详见:java创建索引、linux创建

相应的删除索引:

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


2、创建restful风格插入

curl -XPOST http://localhost:9200/索引名称/索引类型/id -d JSON格式的参数

curl -XPUT http://localhost:9200/test/kind/1?pretty -d '{"title":"test_1","label":["sofa","beauty","white","American"],"create_date":"2017-11-1 15:41"}'

注意:这种方式插入,中文会报错(我就因为这问题,耽误半天)


入门实验操作:

首先——插入几条测试数据:

curl -XPUT http://localhost:9200/test/kind/1?pretty -d '{"title":"sofa_1","label":["sofa","beauty","white","American"],"create_date":"2017-11-1 15:41"}'  curl -XPUT http://localhost:9200/test/kind/2?pretty -d '{"title":"sofa_2","label":["sofa","beauty","red","American"],"create_date":"2017-11-1 14:41"}'curl -XPUT http://localhost:9200/test/kind/3?pretty -d '{"title":"sofa_3","label":["sofa","cute","red","China"],"create_date":"2017-11-1 13:41"}'curl -XPUT http://localhost:9200/test/kind/4?pretty -d '{"title":"chair_4","label":["chair","cute","red","China"],"create_date":"2017-11-1 12:41"}'curl -XPUT http://localhost:9200/test/kind/5?pretty -d '{"title":"chair_5","label":["chair","beauty","white","American"],"create_date":"2017-11-1 11:41"}'curl -XPUT http://localhost:9200/test/kind/6?pretty -d '{"title":"chair_6","label":["chair","beauty","black","China"],"create_date":"2017-11-1 10:41"}'

、搜索lable中含有China的

curl http://localhost:9200/test/kind/_search?q=label:China

、搜索label中含有“beauty American”相关内容

(测试结果:这里返回内容就类似搜索引擎,匹配度越高优先返回,但也有些不是这样:比如搜"white American")

curl -X GET http://localhost:9200/test/kind/_search?pretty -d '{"query" : {"match" : {"label" : "beauty American"}}}'

便于阅读,格式化如下:

curl http://localhost:9200/test/kind/_search?pretty -d '{    "query" : {        "match" : {            "label" : "beauty American"        }    }}'

在满足2的基础上,再按照标签出现的次数推荐标签

curl -X GET http://localhost:9200/test/kind/_search?pretty -d '{"query" : {"match" : {"label" : "beauty American"}},"aggs": {"all_label": {"terms": { "field": "label" }}}}'


注意:这里使用elasticsearch的aggregations聚合查询有个问题,中文会被拆分成单个字统计,要将字段设置属性:

感谢文章:http://jingyan.baidu.com/article/15622f2400c160fdfcbea5e9.html

curl -X PUT http://localhost:9200/test/_mapping/kind/ -d '{"properties": {"label": {"type":"text","index": "not_analyzed"}}}'


警告这里会报一个错误,点击:Fielddata is disabled on text fields by default
错误解决办法:执行如下操作,注意添加"_mapping",感觉这句相当于给label字段定义为text类型,设置fielddata为true:
curl -X PUT http://localhost:9200/test/_mapping/kind/ -d '{"properties": {"label": {"type":"text","index": "not_analyzed","fielddata": true}}}'




原创粉丝点击