您现在的位置是:首页 > 技术杂谈 > elasticsearch > Laravel > php php
本篇主要讲在phpstorm终端和postman上请求操作elasticsearch
首先在windows开启elasticsearch,之前已经讲过,然后再执行操作命令
在phpstorm终端运行命令
curl http://127.0.0.1:9200
1)、创建索引
curl -XPUT http://127.0.0.1:9200/test //创建一个名为test的索引
在请求后面加上?pretty,可以返回JSON格式的字符串
2)、删除索引
curl -XDELETE http://127.0.0.1:9200/test //删除一个名为test的索引
3)、创建类型
curl -H'Content-Type: application/json' -XPUT http://127.0.0.1:9200/test/_mapping?pretty -d'{ "properties": { "title": { "type": "text", "analyzer": "ik_max_word" }, "description": { "type": "text", "analyzer": "ik_max_word" }, "price": { "type": "scaled_float", "scaling_factor": 100 } } }'
4)、创建文档
文档id不是自增的,需要指定id
curl -H'Content-Type: application/json' -XPUT http://127.0.0.1:9200/test/_doc/1?pretty -d'{ "title": "红米Note", "description": "新品到货,限时抢购", "price": 2300 }'
执行结果报错,提示是UTF-8的问题,这个在网上找了好多资料都没解决掉,只能先通过postman去运行了
这个需要设置header参数Content-Type: application/json
5)、读取文档数据
curl http://127.0.0.1:9200/test/_doc/1?pretty
6)、简单查询
curl -XPOST -H'Content-Type:application/json' http://127.0.0.1:9200/test/_doc/_search?pretty -d' { "query" : { "match" : { "description" : "新" }} }'
这个查询还是报错,也是utf8的问题,同样用postman请求
因为用的是ik_max_word分词,所以可以查询到数据
转载请注明:
观海听潮博客
文章评论

评论列表