Elasticsearch搜索引擎安装使用

一、下载,运行

https://mirrors.huaweicloud.com/elasticsearch/7.6.2/elasticsearch-7.6.2-windows-x86_64.zip

双击bat文件,运行成功后访问:http://localhost:9200/  会出现下面

二、作用

        类似数据库,可以在大量的数据中查找到想要的数据,取代数据库的模糊查询。

三、创建 http client测试

         

        插入代码

### 分割符也是注释,每个发送指令的请求之间必须使用分隔符
GET http://localhost:9200

### 测试es的分词功能,运行分词,查看结果
POST http://localhost:9200/_analyze
Content-Type: application/json

{
  "text": "my name is hanmeimei",
  "analyzer": "standard"
}

"analyzer": "standard":默认的分词方式/规则 ,按照空格拆封

四、添加中文分词器

下载包:(1条消息) 中文常见搜索引擎分词库-Java文档类资源-CSDN文库

将下载文件解压到Elasticsearch文件中plugins的ik文件夹下,然后重启

 插入示例代码

### 测试es的分词功能,运行分词,查看结果
POST http://localhost:9200/_analyze
Content-Type: application/json

{
  "text": "小刚",
  "analyzer": "ik_smart"
}

分词结果

POST http://localhost:9200/_analyze

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8

{
  "tokens": [
    {
      "token": "小刚",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 0
    }
  ]
}

http client各段代码意解释

五、ES常用命令

### 分割符也是注释,每个发送指令的请求之间必须使用分隔符
GET http://localhost:9200

### 测试es的分词功能,运行分词,查看结果
POST http://localhost:9200/_analyze
Content-Type: application/json

{
  "text": "小刚刚刚刚了刚刚",
  "analyzer": "ik_smart"
}

### 创建 index
PUT http://localhost:9200/questions

### 删除一个Index
DELETE http://localhost:9200/questions

### 设置index中的文档属性采用ik分词,properties,只有text类型能搜索
POST http://localhost:9200/questions/_mapping
Content-Type: application/json

{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_max_word"
    },
    "content": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_max_word"
    }
  }
}

### questions 中添加文档
POST http://localhost:9200/questions/_create/1
Content-Type: application/json

{
  "id":1,
  "title":"Java基本数据类型有哪些",
  "content":"面时候为啥要问基本类型这么简单问题呀,我们要如何回答呢?"
}

### questions 中添加文档
POST http://localhost:9200/questions/_create/2
Content-Type: application/json

{
  "id":2,
  "title":"int类型的范围",
  "content":"为啥要了解int类型的范围呢?"
}

### questions 中添加文档
POST http://localhost:9200/questions/_create/3
Content-Type: application/json

{
  "id":3,
  "title":"常用集合类有哪些",
  "content":"为啥企业经常问集合呀?该如何回复呢"
}

### questions 中添加文档
POST http://localhost:9200/questions/_create/4
Content-Type: application/json

{
  "id":4,
  "title":"线程的run方法和start方法有啥区别",
  "content":"run方法可以执行线程的计算过程, start也可以执行线程的计算过程,用途一样么?"
}

### 查询数据
GET http://localhost:9200/questions/_doc/4

### 更新questions索引中的文档
POST http://localhost:9200/questions/_doc/4/_update
Content-Type: application/json

{
  "doc": {
    "title": "Java线程的run方法和start方法有啥区别"
  }
}

### 搜索 ES
POST http://localhost:9200/questions/_search
Content-Type: application/json

{
  "query": { "match": {"title": "类型" } }
}

### 多字段搜索 should| must&
POST http://localhost:9200/questions/_search
Content-Type: application/json

{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "java类型" }},
        { "match": { "content": "java类型"}}
      ]
    }
  }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值