springboot 第章 springboot 与索引

Elasticsearch  

elasticserch 可以快速的储存,搜索分析海量数据,Elasticsearch 是一个分布式搜索服务,底层也是基于Lucene ,采用多shard(分片)的方式保证数据安全,提供RestFul API 并且提供自动resharding 功能, 除此之外我还用过solr也是基于lucene ,这个时候可定要对比下

http://i.zhcy.tk/blog/elasticsearchyu-solr/

solr 的确定是他牺牲了建立索引的事件,建立索引的时间长,但是搜索的速度非常的块

https://www.cnblogs.com/xiaoqi/p/solr-vs-elasticsearch.html

我们这里讲springboot 和 elasticsearch 整合 

docker 安装 elasticsearch 

docker search elasticsearch 

docker pull registry.docker-cn.com/library/elasticsearch

docker images 

run

默认初始会使用2G 的堆内存空间我们可以使用 -e 命令限制内存空间

elastic search 默认使用的端口是9200  在分布式的情况下,各个节点使用的端口是9300 我们都需要暴漏这两个接口

docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 362c5cb1669b

-e 分配栈内存, -p 映射端口 , --name image 名字  id

docker ps -a  

然后可以看到elasticsearch 已经启动

我们可以通过浏览器访问返回json 然后确定是否启动 

http://192.168.24.136:9200/  

我的内网ip 然后返回这样的数据

{
  "name" : "Xyyh80B",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "e3RZPu0pS8uUlxVA6fJQFA",
  "version" : {
    "number" : "5.6.11",
    "build_hash" : "bc3eef4",
    "build_date" : "2018-08-16T15:25:17.293Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

add

https://www.elastic.co/guide/cn/elasticsearch/guide/current/_indexing_employee_documents.html

官网说的很清楚

GET

https://www.elastic.co/guide/cn/elasticsearch/guide/current/_retrieving_a_document.html

如果未发现的话 就会返回这样的情况

{
    "_index": "megacorp",
    "_type": "employee",
    "_id": "4",
    "found": false
}

getAll

https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_lite.html

使用查询表达式

https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_with_query_dsl.html

Springboot整合Elasticsearch 

springboot 默认使用springdata 操作elasticsearch ,

elasticsearch 自动配置

springboot提供了两种方式,默认使用springdata ,还整合了最流行的jest 客户端通过http方式操作elasticsearch  

JestAutoConfiguration

默认jestAutoConfiguration 是不生效的, 如果想让生效需要导入jest 包

ElasticsearchDataAutoConfiguration

  1. Client节点信息clusterNodes clusterName 
  2. elasticsearchTemplate 操作es
  3. 编写一个ElasticsearchRepository的子接口来操作ES 

1.创建entity

package com.zzq.springboot03elasticsearch;

import io.searchbox.annotations.JestId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


public class Article {

    @JestId
    private Integer id ;
    private String title ;
    private String author ;
    private String content ;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Article(Integer id, String title, String author, String content) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.content = content;
    }
}

这里注意一定要标识主键,如果不标识主键他就会自动生成主键,

然后编写逻辑代码创建索引

	@Test
	public void contextLoads() throws IOException {
		//给es 中保存一个文档
		Article article = new Article(1, "好消息", "zhnagsan", "Hello Word");
		Index build = new Index.Builder(article).index("atguigu").type("news").build();

		jestClient.execute(build);
	}

注意一定要注入jestClient 客户端对象,然后执行execute方法

然后就ok了, 

使用全文检索查询

@Test
	public void search() throws IOException {
		String json = "{\n" +
				"    \"query\" : {\n" +
				"        \"match\" : {\n" +
				"            \"content\" : \"hello\"\n" +
				"        }\n" +
				"    }\n" +
				"}" ;
		Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();
		SearchResult res = jestClient.execute(search);
		System.out.println(res.getJsonString());
	}

更多的查询方式官网上都是有的  ,玩一个高亮搜索把

@Test
	public void search() throws IOException {
		String json = "{\n" +
				"    \"query\" : {\n" +
				"        \"match_phrase\" : {\n" +
				"            \"content\" : \"hello\"\n" +
				"        }\n" +
				"    },\n" +
				"    \"highlight\": {\n" +
				"        \"fields\" : {\n" +
				"            \"about\" : {}\n" +
				"        }\n" +
				"    }\n" +
				"}" ;
		Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();
		SearchResult res = jestClient.execute(search);
		System.out.println(res.getJsonString());
	}

SpringdataElasticSearch 操作ElasticSearch

使用springdata 操作 Elastic Search  不是使用9200 通过http通讯的,springdata 操作elastic search 使用9300 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以通过配置属性来开启和关闭目录索引。默认情况下,Spring Boot会禁用目录索引,这意味着如果访问一个没有指定文件名的目录,将返回404错误。如果你希望开启目录索引,可以在application.properties文件中添加以下配置: ``` spring.resources.add-mappings=true ``` 这样,当访问一个没有指定文件名的目录时,Spring Boot会自动列出该目录下的文件和子目录。同时,你也可以自定义返回的样式或者禁用特定目录的索引。具体的配置方式可以参考Spring Boot的官方文档。 [2<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MongoDB+集成SpringBoot+索引+并发优化 - 基于《MongoDB进阶与实战:唐卓章》](https://blog.csdn.net/weixin_48518621/article/details/120890975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot基础-索引](https://blog.csdn.net/weixin_46899412/article/details/124803743)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值