当前位置:   article > 正文

ES配置与使用_es集群master配置

es集群master配置

一、单机版安装

地址:www.elastic.co

下载tar格式,或者复制链接,wget + url下载

启动:

./bin/elasticsearch

二、插件

解决页面问题,GitHub下载:elasticsearch-head

需要node环境。node -v检查node版本,需要大于6.0.0

安装:进入目录,npm install

启动:npm run start,在9100端口启动 

head和es存在跨域问题,在es配置文件加配置

#允许跨域
http.cors.enabled: true
http.cors.allow-origin: "*"

三、分布式安装

其中一个设置为master,修改配置文件config/elasticsearch.yml

在最后加上如下配置:

集群配置,master和子节点的配置:

  1. #集群设置
  2. #集群名
  3. cluster.name: wali 
  4. #节点名
  5. node.name: master
  6. #指定为master
  7. node.master: true
  8. #绑定IP,本地127.0.0.1
  9. network.host: 127.0.0.1
  10. #子节点配置
  11. #集群名
  12. cluster.name: wali
  13. #本节点名
  14. node.name: slave1
  15. network.host: 127.0.0.1
  16. http.port: 9201
  17. #找到master
  18. discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

四 、基础概念

1、索引 含有相同属性的文档集合

2、类型 所以可以定义一个或多个类型,文档必须鼠疫一个类型

3、文档 文档是可以被索引的及基本数据单位

4、分片 每个索引都有多个分片,每个分片是一个Lucene索引

5、备份 拷贝一份分片就完成了分片的备份

RESTFul API

1、api基本格式:http://<ip>:<port>/<索引>/<类型>/<文档id>

2、常用的HTTP动词 GET/PUT/POST/DELET

索引信息中,mappings为空是非结构化

1、创建索引

创建一个people,put方式

http://192.168.152.128:9200/people

参数:

  1. {
  2. "settings": {
  3. "number_of_shards":3,
  4. "number_of_replicas":1
  5. },
  6. "mappings":{
  7. "man":{
  8. "properties":{
  9. "name":{
  10. "type":"text"
  11. },
  12. "contry":{
  13. "type":"keyword"
  14. },
  15. "age":{
  16. "type":"integer"
  17. },
  18. "date":{
  19. "type":"date",
  20. "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  21. }
  22. }
  23. },
  24. "woman":{
  25. }
  26. }
  27. }

2、 插入数据:

指定id,用put,不指定id用post,会自动生成

 3、修改

直接修改:http://192.168.152.128:9200/people/man/1/_update

{

    "doc":{

        "name":"谁是瓦力"

    }

}

脚本修改:

{

    "script":{

        "lang":"painless",

        "inline":"ctx._source.age += 1"

    }

}

或者:

{

    "script":{

        "lang":"painless",

        "inline":"ctx._source.age =params.age",

        "params":{

                 "age":100

         }

    }

}

4、删除

删除文档,delete方法:http://192.168.152.128:9200/people/man/1/

删除索引,页面删除:动作-删除;

delete方法:http://192.168.152.128:9200/people

5、查询

根据id查询,get方法:http://192.168.152.128:9200/book/novel/1

http://192.168.152.128:9200/book/_search

match_all:查询所有,from:页数,size:每页条数

  1. {
  2.     "query":{
  3.         "match_all":{}
  4.     },
  5.     "from":1,
  6.     "size":2
  7. }

条件查询,排序:

  1. {
  2. "query":{
  3. "match":{
  4. "title":"ElasticSearch"
  5. }
  6. },
  7. "sort":[
  8. {"publish_date":{"order":"desc"}}
  9. ]
  10. }

  1. {
  2. "aggs":{
  3. "group_by_word_count":{
  4. "terms":{
  5. "field":"word_count"
  6. }
  7. },
  8. "group_by_publish_date":{
  9. "terms":{
  10. "field":"publish_date"
  11. }
  12. }
  13. }
  14. }

stats:统计,min:最小,max:最大

  1. {
  2. "aggs":{
  3. "grades_word_count":{
  4. "stats":{
  5. "field":"word_count"
  6. }
  7. }
  8. }
  9. }

四、项目简单使用

1、配置

pom文件

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>transport</artifactId>
  4. <version>${elasticsearch.version}</version>
  5. </dependency>

 配置文件

  1. #ES配置
  2. elasticsearch:
  3. hosts: 192.168.152.128
  4. clusterName: wali
  5. invoice.count: 5

 配置类

  1. package com.example.esdemo.config;
  2. import org.elasticsearch.client.transport.TransportClient;
  3. import org.elasticsearch.common.settings.Settings;
  4. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  5. import org.elasticsearch.common.transport.TransportAddress;
  6. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import java.net.InetAddress;
  11. /**
  12. * ES配置
  13. *
  14. * @author zhanglei
  15. * @date 2022-01-17
  16. */
  17. @Configuration
  18. public class ElasticSearchConfig {
  19. @Value("${elasticsearch.hosts}")
  20. private String hosts;
  21. @Value("${elasticsearch.clusterName}")
  22. private String clusterName;
  23. @Bean
  24. public TransportClient getTransportClient() {
  25. System.setProperty("es.set.netty.runtime.available.processors", "false");
  26. try {
  27. Settings settings = Settings.builder().
  28. put("client.transport.sniff", true)
  29. .put("cluster.name", clusterName).
  30. build();
  31. TransportClient client = new PreBuiltTransportClient(settings);
  32. //分号
  33. String[] addressArray = hosts.split(";");
  34. if (addressArray == null) {
  35. throw new Exception("ES服務配置异常");
  36. }
  37. for (String address : addressArray) {
  38. //冒号
  39. String[] addressParts = address.split(":");
  40. String host = address;
  41. int port = 9300;
  42. if (addressParts.length == 2) {
  43. host = addressParts[0];
  44. port = Integer.parseInt(addressParts[1]);
  45. }
  46. client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
  47. }
  48. return client;
  49. } catch (Exception e) {
  50. System.out.println("ES服務配置异常");
  51. System.exit(1);
  52. return null;
  53. }
  54. }
  55. }

2、代码使用

  1. @Autowired
  2. private TransportClient client;
  3. public static final String INDEX = "people";
  4. public static final String TYPE = "man";
  5. @PostMapping("/man/query")
  6. @ApiOperation(httpMethod = "POST", value = "查询")
  7. public ResponseEntity add(@RequestParam(name = "name", required = false) String name,
  8. @RequestParam(name = "gtAge", required = false) Integer gtAge,
  9. @RequestParam(name = "ltAge", required = false) Integer ltAge,
  10. @RequestParam(name = "country", required = false) String country,
  11. @RequestParam(name = "countryQueryType", defaultValue = "1") Integer countryQueryType,
  12. @RequestParam(name = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String date,
  13. @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
  14. @RequestParam(name = "pageSize", required = false, defaultValue = "3") Integer pageSize) {
  15. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
  16. if (name != null) {
  17. boolQuery.must(QueryBuilders.matchQuery("name", name));
  18. }
  19. //范围查询
  20. if (gtAge != null || ltAge != null) {
  21. RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
  22. if (gtAge != null && gtAge > 0) {
  23. rangeQuery.from(gtAge);
  24. }
  25. if (ltAge != null && ltAge > 0) {
  26. rangeQuery.to(ltAge);
  27. }
  28. boolQuery.filter(rangeQuery);
  29. }
  30. if (country != null) {
  31. if (countryQueryType == 1) {
  32. //精确查询
  33. boolQuery.must(QueryBuilders.termQuery("country.keyword", country));
  34. } else {
  35. //模糊查询,(有一个分词能匹配也会返回,根据匹配度)
  36. boolQuery.must(QueryBuilders.matchQuery("country", country));
  37. }
  38. }
  39. SearchRequestBuilder builder = this.client.prepareSearch(INDEX)
  40. .setTypes(TYPE)
  41. .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  42. .setQuery(boolQuery)
  43. .setFrom(pageNum)
  44. .setSize(pageSize);
  45. System.out.println(builder);
  46. SearchResponse response = builder.get();
  47. ArrayList<Map<String, Object>> result = new ArrayList<>();
  48. for (SearchHit hit : response.getHits()) {
  49. result.add(hit.getSourceAsMap());
  50. }
  51. return new ResponseEntity(result, HttpStatus.OK);
  52. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号