[springboot学习笔记]Spring Boot与检索(ElasticSearch)

94 篇文章 2 订阅
8 篇文章 0 订阅

Spring Boot与检索(ElasticSearch)

1.docker运行elasticsearch


docker pull elasticsearch

#拉取仓库中的软件

docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 镜像ID

docker安装6.x版本elasticearch容器:(这有坑)

1.创建启动容器

docker run -di --name=自定义名字 -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" -p 9200:9200 -p 9300:9300 elasticsearch:版本号
2.docker ps

查看容器,容器启动了,但是过一会发现容器自动关闭了

3.docker logs -f  容器id 

查看容器日志信息,看最后面发现一个error:

ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least
显示max_map_count的值太小了,需要设大到262144

查看max_map_count :
cat /proc/sys/vm/max_map_count
65530

设置max_map_count:
sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
4.重启容器:docker start  容器id或名字

浏览器就能访问了
————————————————
原文链接:https://blog.csdn.net/dsx1134500455/article/details/89531146

进入域名:9200

显示这样的数据说明成功了
{
  "name" : "P90Xynk",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "xfegyxxeQACxISbZc8Cdng",
  "version" : {
    "number" : "5.6.12",
    "build_hash" : "cfe3d9f",
    "build_date" : "2018-09-10T20:12:43.732Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

2.简单实用elasticsearch

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

3.springboot整合elasticsearch

1 整合jest

@Autowired
JestClient jestClient;

@Test
public void create() {
    Article article = new Article();
    article.setId(1);
    article.setAuthor("zhangsan");
    article.setTitle("haoxiaoxi");
    article.setContent("haoxiaoxijiujiushimeiyousmxiaoxihaha");
    //index("xiaoxiao").type("news").id(article.getId().toString())表示哪个索引,哪个类型,指定的key
    Index index = new Index.Builder(article).index("xiaoxiao").type("news").id(article.getId().toString()).build();
    try {
        jestClient.execute(index);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@Test
public void search() {
    String json = "{\n" +
            "    \"query\": {\n" +
            "        \"match\": {\n" +
            "            \"title\": \"haoxiaoxi\"\n" +
            "        }\n" +
            "    }\n" +
            "}";
    try {
        //addIndex("xiaoxiao"),addType("news")表示在哪个索引哪个类型进行搜索
        Search search = new Search.Builder(json).addIndex("xiaoxiao").addType("news").build();
        SearchResult result = jestClient.execute(search);
        System.out.println(result.getJsonString());//调用查询结果的json字符串
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2 整合elasticsearch

1.配置application.properties

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.1.50:9300

2.继承 ElasticsearchRepository<Book, Integer>,泛型1是类型,2是主键

public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
    //模糊查询
    public List<Book> findByBookNameLike(String bookName);
}

3.在bean类上注解@Document

@Document(indexName = "xiaoxiao",type = "book")//参数一是索引名,参数2是类型

4.测试

出现错误

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}

问题是没有配置集群

需要在properties文件中配置集群名字

spring.data.elasticsearch.cluster-name=docker-cluster

这个name在elasticsearch的端口映射网站上可以看到

{
  "name" : "fqQHaH1",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "tvUGMDW1RfSZrKDxStXwkA",
  "version" : {
    "number" : "6.8.10",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "537cb22",
    "build_date" : "2020-05-28T14:47:19.882936Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.3",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
@Test
public void testEScreate(){
    Book book = new Book(1,"吴承恩","西游记");
    bookRepository.index(book);//保存索引
    List<Book> books = bookRepository.findByBookNameLike("西");
    for (Book book1 : books) {
        System.out.println(book1);
    }
}

出现错误:org.springframework.data.elasticsearch.ElasticsearchException: failed to map

原因:没有在bean类中添加无参构造方法

这次坑比较多,搞了好久,加油加油

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值