1、安装elasticsearch,教程:https://www.imooc.com/article/20367
2、安装elasticsearch-head插件,教程:https://github.com/mobz/elasticsearch-head
cd elasticsearch-head
npm install
npm run start
open
http://localhost:9100/
elasticsearch的配置文件中,添加:
http.cors.enabled: true
http.cors.allow-origin: "*"
3、也可以不用安装这个插件,谷歌浏览器自带其扩展程序:直接搜索:elasticsearch-head安装即可
4、双击elasticsearch.bat,启动elasticsearch
5、创建索引:打开postman 工具,选择put请求,url输入:http://localhost:9200/customer?pretty
执行第一行返回以下内容,这里我们使用PUT谓词创建了一个名叫 customer 的索引,在后面跟上 pretty 表示如果有数据返回的话,用格式化后的JSON返回数据
{
"acknowledged": true,
"shards_acknowledged": true
}
6、创建类型:http://localhost:9200/customer/external/1?pretty
参数:
{
"name": "John Doe"
}
返回内容如下:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
从以上可以看出,一个新的客户文档成功被索引到 customer索引的 extenal 类型中,并且我们在索引的时候指定文档的内部id值为1。
值得注意的是, Elasticsearch 不需要在你索引文档到某个索引之前,明确的创建一个索引。比如上一个例子,如果 customer索引不存在, Elasticsearch将自动创建该索引。
再来看下我们刚刚索引的文档
GET /customer/external/1?pretty
返回内容如下:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "John Doe"
}
}
补充:
1、查询全部文档:GET /索引/类型/_search
2、模糊搜索姓名:http://localhost:9200/company/employee/_search?q=last_name:Smith
或者:
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
3、搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
4、精确搜索
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
4、高亮搜索
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
5、返回文档的某几个字段【_source】
GET /website/blog/123?_source=title,text
参考教程:https://www.cnblogs.com/ginb/p/6993299.html
官网文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_analytics.html