使用ElasticSearch全文检索以及集群部署

ElasticSearch  即 ES

安装:版本---elasticsearch-2.4.6.tar.gz

      tar -zxvf elasticsearch-2.4.6.tar.gz

由于es不允许使用root管理员启动程序,所以我们需要新建用户:

    useradd es

修改es文件权限

    chown -R es ./elasticsearch-2.4.6/bin

然后就可以使用es用户后台启动es了

    su es

    ./elasticsearch-2.4.6/bin/elasticsearch -d

  由于es启动默认的堆内存空间是1G,所以可能请求出现问题

    错误信息:org.elasticsearch.common.breaker.CircuitBreakingException: [parent] Data too large, data for [<transport_request>] would be [986966428/941.2mb], which is larger than the limit of [986061209/940.3mb], real usage:   

    解决方案:

      ./elasticsearch-2.4.6/bin/elasticsearch  -Xms4g -Xmx4g -d   rug

    如果还没有解决就是es内存告急,默认使用70%   调整熔断内存比例大小:

      curl -H "Content-Type: application/json" -XPUT http://127.0.0.1:9200/_cluster/settings -d '{"persistent":{"indices":{"breaker":{"fielddata":{"limit" : "95%" }}}}}'

配置es服务端,---->>  如果没有配置springboot会自动创建一个本地目录供es使用

spring:
  data:
   elasticsearch:
    cluster-nodes: 127.0.0.1:9300,127.0.0.2:9300 #集群用逗号分开
    cluster-name: elasticsearch


es的使用方式和jpa极其相似,dao层也是用接口 不同的是es的dao继承ElasticSearchRepository

 

由于ElasticSearchRepository与JpaRepository都继承自PagingAndSortingRepository,所以es和jpa使用同一个实体类时往往会出现问题
(详细见:https://stackoverflow.com/questions/36252099/no-property-index-found-for-type-user)

而实体类的注解类:JPA使用的是@Entity , ES使用的是@Document(indexName="项目名",type="article") Document的indexName和type不能包含大写字母,否则会报错,indexName相当于mysql的database,type相当于table

 解决方案:

将ES的repository单独放在其他的包,如果其他的jpa都在com.test.news下,那es可以放在com,test.es中

在启动类中加上注解:

@EnableElasticsearchRepositories(basePackages = "com.test.es")
@EnableJpaRepositories(basePackages = {"com.test.news"})

在linux上集群:   修改配置文件

  cluster.name: elasticsearch

  node.name: node-1      #每一个节点都不一样

  network.host: 0.0.0.0

  http.port: 9200

  discovery.zen.ping.unicast.hosts: ["host1", "host2"]   #集群ip

  discovery.zen.minimum_master_nodes: 2    #正常是集群数一半+1

posted @ 2019-02-28 10:13  嬴安  阅读(173)  评论(0)    收藏  举报