elasticSearch的单机安装部署使用Java项目操作

最近有存储大数据的需求,mysql明显无法满足海量数据

其他的hive和hbase也被pass掉,听说es效果不错,所以上手玩一下

1.安装es

首先先去官网,下载最新的包,https://www.elastic.co/downloads/elasticsearch

截止到今日2018.7.5最新版本是6.3.0,那就下载最新的吧

下载完之后解压缩,发现进入./bin/elasticsearch就可以直接执行了(需要java环境)

但是报了个can not run elasticsearch as root,原来是不能用root身份启动,那就新建一个用户es吧

然后chown改变文件夹的权限继续执行,发现又报了一个错误max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

原来是用户最大可创建文件数太小,那就修改吧

切换到root用户,vi /etc/security/limits.conf

修改成,*代表所有用户

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

继续启动

发现又报max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

虚拟内存又不够了

切换到root用户下,修改配置文件sysctl.conf
vi /etc/sysctl.conf

vm.max_map_count=655360

保存之后执行sysctl -p

发现终于启动成功,然后访问本地http://localhost:9200

出现以下信息

{    
    "name": "Sq2gJT0",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "QH6Ga480QOKF6GSBeoQ-VQ",
       "version": {
            "number": "6.3.0",
            "build_flavor": "default",
            "build_type": "tar",
            "build_hash": "424e937",
            "build_date": "2018-06-11T23:38:03.357887Z",
            "build_snapshot": false,
            "lucene_version": "7.3.1",
            "minimum_wire_compatibility_version": "5.6.0",
            "minimum_index_compatibility_version": "5.0.0"
        },
    "tagline": "You Know, for Search"
}

2.安装elasticsearch-head

至此单机版的es已经成功启动了,但是有个问题,要测试的话就必须访问restful接口,有没有图形界面呢

网上查询一下,发现有一个elasticsearch-head的插件,果断搞一下

到github主页看了一下 https://github.com/mobz/elasticsearch-head

别说了,先下载吧,

wget https://github.com/mobz/elasticsearch-head/archive/master.zip

然后发现要安装node,那就装吧

wget https://npm.taobao.org/mirrors/node/latest-v4.x/node-v4.4.7-linux-x64.tar.gz
tar -zxvf node-v4.4.7-linux-x64.tar.gz

添加node环境变量,vi /etc/profile

添加以下内容,NODE_HOME改成自己的node路径

export NODE_HOME=/opt/node-v4.5.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin/
export NODE_PATH=$NODE_HOME/lib/node_modules

保存后执行source /etc/profile生效

然后安装grunt(构建node的工具)

cd /opt/elasticsearch-head-master
npm install -g grunt-cli //执行后会生成node_modules文件夹

然后可以执行grunt -version检查是否安装成功

然后修改配置文件

vi Gruntfile.js

connect: {
            server: {
                    options: {
                                hostname: '0.0.0.0',
                                port: 9100,
                                base: '.',
                                keepalive: true
                    }
            }
}

配置完之后就可以启动了

在elasticsearch-head-master目录下

npm install
grunt server

然后访问http://localhost:9100/

发现已经有后台供访问了,但是连不上服务,原来还需要把es的跨域允许打开

vim elasticsearch.yml

http.cors.enabled: true
http.cors.allow-origin: "*"

然后就是一些后台的操作就不写了,很简单

index相当于库,type相当于表,document相当于一条记录,这么理解的话,跟传统关系型数据库还是比较类似的

3.对接java项目

那现在服务也有了,图形界面也有了,开始要对接项目了

我们新建一个springboot项目,就用最新版2.0.3的

pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>elasticSearchProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>elasticSearchProject</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>2.6</maven-jar-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.29</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这里我们使用spring-data-elasticsearch这个框架来实现对elasticsearch的操作

那我们要怎么操作呢?

第一种方式:elasticSearchTemplate

首先在配置文件里写上elasticsearch的信息

这里我使用的是properties方式,也可以用yml方式

spring.data.elasticsearch.clusterName=elasticsearch#默认名字
spring.data.elasticsearch.clusterNodes=****:9300#如果集群可以用,分隔

然后我们就可以使用elasticSearchTemplate了

在service中注入elasticSearchTemplate直接使用

@Autowired
    ElasticsearchTemplate elastticsearchTemplate;

 我们要先将我们的domian加上注解

@Document(indexName = "helloworld", type = "User", shards = 5, replicas = 1)
public class User implements Serializable {

    @Id
    private String id;
    private String name;
    private int age;//ignor getter setter}

 

比如一个简单的查询

SearchQuery searchQuery = new NativeSearchQueryBuilder().
                    withQuery(new MatchQueryBuilder("content","中国")).
                    withPageable(PageRequest.of(0, 10)).build();
List<User> list = elasticsearchTemplate.queryForList(searchQuery, User.class);

还有一些创建索引,mapping等操作都可以通过这个template来操作,这里就不写了

第二种方式是使用类似jpa

创建一个UserRepository的接口

public interface UserRepository extends ElasticsearchRepository<User, String>{}

继承的这个接口里有很多方法供使用

比如save search之类的

这里举一个查询的例子

Page<User> result = userDAO.search( new NativeSearchQueryBuilder(). 
            withQuery(new QueryStringQueryBuilder("中国")). 
            withPageable(PageRequest.of(0, 10)).build());

发现同样需要SearchQuery对象,所以都大同小异

4.安装ik分词器

最后一个问题就是搜索中文的问题,由于es的原理是对文本进行分词然后倒排索引,所以如果文本是中文的,那我们需要一个中文的分词器来达到更好的搜索效果

这里看大家推荐的都是"ik"这个分词器小插件,那我也装一下这个试试吧

还是先到github主页看看https://github.com/medcl/elasticsearch-analysis-ik

然后开始安装,我这里选择使用作者建议的第二种方式,比较简单

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip

 注:这个方法只支持5.5.1以后的版本(supported from version v5.5.1)

安装完毕之后就可以用了,注意要重启es,并且已经建立好的索引要重新建索引并且指明type使用的analyzer为ik

如果发现这个分词无法满足你的搜索需要(比如一些人名,品牌),那我们可以修改我们自定义的词典来达到更好的分词效果,具体做法github上有写,我试了一下没问题,这里就不写了

转载于:https://my.oschina.net/u/2449415/blog/1840751

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值