ElasticSearch 教程

本文介绍如何安装和配置Elasticsearch,包括安装elasticsearch-head插件、启动服务、创建索引和文档,以及如何进行基本的查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、安装elasticsearch,教程:https://www.imooc.com/article/20367

2、安装elasticsearch-head插件,教程:https://github.com/mobz/elasticsearch-head

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值