热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

全文检索及ES学习笔记

上一篇学习了lucene原理及简单的使用.这次基于lucene上学习一下ES的使用.之前在linux上部署过ES(之前的文章有部署流程),然后今天启动发现启动不起来,报错Nofa

上一篇学习了lucene原理及简单的使用.这次基于lucene上学习一下ES的使用.

之前在linux上部署过ES(之前的文章有部署流程),然后今天启动发现启动不起来,报错
No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender

于是搜了一下,ES启动报错No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender

是因为之前以root的身份进入log文件导致文件权限变成了root权限
在这里插入图片描述将文件权限修改一下即可
执行命令: chown yyy[我的用户名] xxx.log
修改完后启动正常了.


ES集群:

创建es-clutser文件夹,在内部复制三个ES服务.
然后分别修改每个ES服务中elasticsearch-cluster\node*\config\elasticsearch.yml配置文件:


node1节点:

#节点1的配置信息:
#集群名称,保证唯一
cluster.name: my-elasticsearch
#节点名称,必须不一样
node.name: node-1
#必须为本机的ip地址
network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样
http.port: 9200
#集群间通信端口号,在同一机器下必须不一样
transport.tcp.port: 9300
#设置集群自动发现机器ip集合
discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”,“127.0.0.1:9301”,“127.0.0.1:9302”]


node2节点:

#节点2的配置信息:
#集群名称,保证唯一
cluster.name: my-elasticsearch
#节点名称,必须不一样
node.name: node-2
#必须为本机的ip地址
network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样
http.port: 9201
#集群间通信端口号,在同一机器下必须不一样
transport.tcp.port: 9301
#设置集群自动发现机器ip集合
discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”,“127.0.0.1:9301”,“127.0.0.1:9302”]


node3节点:

#节点3的配置信息:
#集群名称,保证唯一
cluster.name: my-elasticsearch
#节点名称,必须不一样
node.name: node-3
#必须为本机的ip地址
network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样
http.port: 9202
#集群间通信端口号,在同一机器下必须不一样
transport.tcp.port: 9302
#设置集群自动发现机器ip集合
discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”,“127.0.0.1:9301”,“127.0.0.1:9302”]

然后依次启动各个节点服务器.


ES使用(通过SpringDataES):

环境搭建
:
xml配置:



entity:

package com.ceeemall.es.entity;import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Document(indexName = "test",type="article") //索引库名称 类型名称
public class Article {@Id //主键标识@Field(type = FieldType.Long,store = true) //type 数据类型 store 是否存储private long id;@Field(type = FieldType.text,store = true,analyzer = "ik_max_word")private String title;@Field(type = FieldType.text,store = true,analyzer = "ik_max_word") //analyzer 分词器 使用ikprivate String content;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}
}

操作:
索引库的操作:

package com.ceeemall.es;import com.ceeemall.es.entity.Article;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpEsTest {@Autowiredprivate ElasticsearchTemplate template;@Testpublic void creatIndex(){//初始化spring容器//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");//从容器中获取ElasticsearchTemplate对象// ElasticsearchTemplate template = applicationContext.getBean(ElasticsearchTemplate.class);//通过template对象创建索引库,指定entity可以基于entity创建索引库template.createIndex(Article.class);}//删除索引库@Testpublic void deleteIndex(){template.deleteIndex("test");}//设置mapping@Testpublic void putMapping(){template.putMapping(Article.class);}}

文档的操作:

定义dao:

package com.ceeemall.es.dao;import com.ceeemall.es.entity.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;public interface ArticleDao extends ElasticsearchCrudRepository {}

添加文档:

//添加文档@Testpublic void addDocument(){Article article = new Article();article.setId(1);article.setTitle("你好,我是title");article.setContent("你好,我是content");//添加文档articleDao.save(article);}

添加成功:
在这里插入图片描述

删改就不说了,就是delete和save方法.

查询:
通过id查询:

@Testpublic void findById(){//返回optional对象Optional

optional = articleDao.findById(1L);//判断optional中是否有值if (optional.isPresent()){Article article = optional.get();System.out.println(article);}}

查询所有:

@Testpublic void findAll(){Iterable

all = articleDao.findAll();for (Article article : all) {System.out.println(article);}}

传入page对象可以实现分页查询.

条件查询:
使用findBy+ 查询字段的名称,参数中就是查询的条件:

dao:

public interface ArticleDao extends ElasticsearchCrudRepository {List

findByTitle(String title); }

@Testpublic void findByTitle(){List

articles = articleDao.findByTitle("title");for (Article article : articles) {System.out.println(article);}}

也可添加pageable实现分页

queryString查询:
需要使用原生的查询条件查询,通过NativeSearchQuery创建查询条件,查询时使用estemplate查询.

@Testpublic void finfByQueryString(){//创建原生查询条件NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().//查询条件withQuery(QueryBuilders.queryStringQuery("你好")//设置默认查询字段.defaultField("content")).build();//通过estemplate对象执行查询List

articles = template.queryForList(nativeSearchQuery, Article.class);for (Article article : articles) {System.out.println(article);}}

推荐阅读
  • java日志框架详解
    Java日志框架详解1.常用日志框架1.1Java常用日志框架类别1.2Java常用日志框架历史1.3两大日志接口阵营1.3.1基于CommonsLogging接口实现的常用日志框 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 本文介绍了在RHEL 7中的系统日志管理和网络管理。系统日志管理包括rsyslog和systemd-journal两种日志服务,分别介绍了它们的特点、配置文件和日志查询方式。网络管理主要介绍了使用nmcli命令查看和配置网络接口的方法,包括查看网卡信息、添加、修改和删除配置文件等操作。 ... [详细]
  • 部署solr建立nutch索引
    2019独角兽企业重金招聘Python工程师标准接着上篇nutch1.4的部署应用,我们来部署一下solr,solr是对lucene进行了封装的企 ... [详细]
  • log4j相关
    Log4j的类图Logger-日志写出器,供程序员输出日志信息Appender-日志目的地,把格式化好的日志信息输出到指定的地方去ConsoleAppe ... [详细]
  • 一:什么是solrSolr是apache下的一个开源项目,使用Java基于lucene开发的全文搜索服务器;Lucene是一个开放源代 ... [详细]
  • springboot项目组引入JMeter的实现步骤
    本文主要介绍了springboot项目组引入JMeter的实现步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的 ... [详细]
  • 本文出自:web应用用cronolog分割tomcat的catalina.out文件#wgethttp:cronolog.orgdownloadcronolog-1. ... [详细]
  • Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用
    Springboot的日志管理springboot无需引入日志的包,springboot默认已经依赖了slf4j、logback、log4j等日志。我习惯用slf4j,下面就用sl ... [详细]
  • 将内网IP虚拟成外网能够访问的域名
    应用场景:微信开发时经常需要回调地址,下面对微信需要的回调地址啰嗦几句回调地址(回调接口):(微信通过你的发送的请求(比如授权,支付等),响应给你的信息,但是他不知道怎么返回给 ... [详细]
  • 性能测试——Jmeter基本用法概述
    Jmeter基本用法概述Jmeter:Apache组织开发的基于Java的性能测试工具Jmeter基本用法概述一、Jmeter安装与配置二、Jmeter文件目录介绍(*表 ... [详细]
  • java之学习记录 92lecene 全文检索
    搭建springBoot项目依赖:<?xmlversion=1.0 ... [详细]
  • mac php错误日志配置方法及错误级别修改
    本文介绍了在mac环境下配置php错误日志的方法,包括修改php.ini文件和httpd.conf文件的操作步骤。同时还介绍了如何修改错误级别,以及相应的错误级别参考链接。 ... [详细]
author-avatar
小猪朱一凡
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有