elasticsearch-7.12.0学习

1 篇文章 0 订阅

elasticsearch

elasticsearch官网
elasticsearch使用:

  1. 解压elasticsearch包
  2. 熟悉目录
-bin 启动目录
-config 配置目录
-modules 功能模块
-plugins 插件
  1. 启动elasticsearch.dat,访问http://localhost:9200/
{
  "name" : "DESKTOP-FHMFD6I",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "g-TROMVhS4qCuLXY9qzHzw",
  "version" : {
    "number" : "7.12.0",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "78722783c38caa25a70982b5b042074cde5d3b3a",
    "build_date" : "2021-03-18T06:17:15.410153305Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
  1. 安装可视化插件/elasticsearch-head
    传送门
    https://github.com/mobz/elasticsearch-head.git
    • cd elasticsearch-head
    • npm install
    • npm run start
    • 访问9100端口

解决elasticsearch-head跨域问题:
在elasticsearch.yml中添加

#解决跨域问题
http.cors.enabled: true
http.cors.allow-origin: "*"

elasticsearch端口地址:
publish_address {127.0.0.1:9300}, bound_addresses {127.0.0.1:9300}, {[::1]:9300}

elasticsearch通信地址:
publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}, {[::1]:9200}

kibana

kibana安装:解压即用、需要先启动elasticsearch。

ik分词器

安装https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.12.0,注意对应版本。
在kibana中的dev tool中测试:

GET _analyze
{
  "analyzer" : "ik_smart", 
  "text" : "中国共产党"
}
GET _analyze
{
  "analyzer": "ik_max_word", 
  "text" : "爱我中国xin"
}
# 索引操作
PUT /test3/_doc/1
{
  "name": "李耀伦",
  "age" : 21,
  "brith": "1999-04-06"
}
#修改
POST /test3/_doc/1
{
  "doc":{
    "name" : "liyaolun1"
  }
}
# 删除
DELETE /test3/_doc/1
DELETE /test3

# 文档操作
# 获取数据 GET
# _source筛选结果
# sort 排序,只能对数字类型进行排序
# 分页
#   , "from": 0
#  , "size": 20
#高亮 highlight
GET /test3/_search
{
  "query": {
    "match": {
      "name": "李"
    }
  }
  , "highlight": {
    "fields": {}
  }
  , "_source": ["name"] 
}

> 整合springboot

1. 导入依赖(注意对应elasticsearch版本)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

2. 配置对象

@Configuration
public class ElasticSearchConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}

3. API测试-----索引

@Test
void contextLoads() throws IOException {
    //---------------------- 索引 ----------------------//
    //ooooooooo 创建索引:1.创建索引请求(无内容)
    CreateIndexRequest index = new CreateIndexRequest("li_index");
    //2.执行请求
    client.indices().create(index, RequestOptions.DEFAULT);
    //ooooooooo 获取索引
    GetIndexRequest getIndex = new GetIndexRequest("li_index");
    //执行请求
    boolean exists = client.indices().exists(getIndex, RequestOptions.DEFAULT);
    System.out.println(exists);
    //ooooooooo 删除索引
    DeleteIndexRequest deleteIndex = new DeleteIndexRequest("li_index");
    AcknowledgedResponse delete = client.indices().delete(deleteIndex, RequestOptions.DEFAULT);
    System.out.println(delete.isAcknowledged());
}

4. API测试-----文档

//---------------------- 文档 ----------------------//
//ooooooooo 添加文档
//创建对象
User user = new User("liya", 22);
//创建请求
IndexRequest request = new IndexRequest("li_index");
//规则 put /li_index/_doc/1
request.id("2").timeout("1s");
//转换为JSON字符串
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(user);
//数据加入请求
request.source(s, XContentType.JSON);
//客户端发送请求,获取返回结果
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
System.out.println(index.status());
//ooooooooo 获取文档信息
GetRequest li_index = new GetRequest("li_index", "1");
GetResponse documentFields = client.get(li_index, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString()); //获取文档内容
System.out.println(documentFields); //获取全部内容,和命令行一样
//ooooooooo 更新文档信息
UpdateRequest index1 = new UpdateRequest("li_index","1");
index1.timeout("1s");
User aini = new User("aini", 33);
index1.doc(objectMapper.writeValueAsString(aini),XContentType.JSON);
UpdateResponse update = client.update(index1, RequestOptions.DEFAULT);
//删除文档信息
DeleteRequest deleteRequest = new DeleteRequest("li_index","1");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);

批量插入文档信息

//批量插入文档信息
BulkRequest bulkRequest = new BulkRequest();
ArrayList<User> list = new ArrayList<>();
list.add(new User("张三",43));
list.add(new User("萨格",22));
list.add(new User("恰外",21));
list.add(new User("话跳",12));
list.add(new User("勾呗",13));
for(int i = 0;i < list.size();i++){
    bulkRequest.add(new IndexRequest("li_index")
            .id("" + i)
            .source(objectMapper.writeValueAsString(list.get(i)),XContentType.JSON));
}
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.hasFailures()); //是否失败

搜索查询

//查询
SearchRequest li_index1 = new SearchRequest("li_index");
//构造搜索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//查询条件
TermQueryBuilder queryBuilder = QueryBuilders.termQuery("name","张三");
searchSourceBuilder.query(queryBuilder);
li_index1.source(searchSourceBuilder);
client.search(li_index1,RequestOptions.DEFAULT);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值