准备环境:
1、JDK1.8.211以上
2、Elasticsearch6.8.6
3、Elasticsearch-analysis-ik-6.8.6 注:用来分词
4、elasticsearch-head-master 注:用来可视查询显示
5、kibana-6.8.6 注:es可视化分析
下载es及组件地址:https://elasticsearch.cn/download/
第一步:解压elasticsearch-6.8.6.tar.gz
tar -zxvf elasticsearch-6.8.6.tar.gz
配置es下的config/elasticsearch.yml
- # ======================== Elasticsearch Configuration =========================
- #
- # NOTE: Elasticsearch comes with reasonable defaults for most settings.
- # Before you set out to tweak and tune the configuration, make sure you
- # understand what are you trying to accomplish and the consequences.
- #
- # The primary way of configuring a node is via this file. This template lists
- # the most important settings you may want to configure for a production cluster.
- #
- # Please consult the documentation for further information on configuration options:
- # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
- #
- # ---------------------------------- Cluster -----------------------------------
- #
- # Use a descriptive name for your cluster:
- #
- #cluster.name: my-application
- #
- # ------------------------------------ Node ------------------------------------
- #
- # Use a descriptive name for the node:
- #
- #node.name: node-1
- #
- # Add custom attributes to the node:
- #
- #node.attr.rack: r1
- #
- # ----------------------------------- Paths ------------------------------------
- #
- # Path to directory where to store the data (separate multiple locations by comma):
- #
- #path.data: /path/to/data
- #
- # Path to log files:
- #
- #path.logs: /path/to/logs
- #
- # ----------------------------------- Memory -----------------------------------
- #
- # Lock the memory on startup:
- #
- #bootstrap.memory_lock: true
- #
- # Make sure that the heap size is set to about half the memory available
- # on the system and that the owner of the process is allowed to use this
- # limit.
- #
- # Elasticsearch performs poorly when the system is swapping the memory.
- #
- # ---------------------------------- Network -----------------------------------
- #
- # Set the bind address to a specific IP (IPv4 or IPv6):
- #
- network.host: 0.0.0.0#不要使用本IP,localhost和127.0.0.1,否则外网不能访问
- #
- # Set a custom port for HTTP:
- #
- http.port: 9200#端口号
- #
- # For more information, consult the network module documentation.
- #
- # --------------------------------- Discovery ----------------------------------
- #
- # Pass an initial list of hosts to perform discovery when new node is started:
- # The default list of hosts is ["127.0.0.1", "[::1]"]
- #
- #discovery.zen.ping.unicast.hosts: ["host1", "host2"]
- #
- # Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
- #
- #discovery.zen.minimum_master_nodes:
- #
- # For more information, consult the zen discovery module documentation.
- #
- # ---------------------------------- Gateway -----------------------------------
- #
- # Block initial recovery after a full cluster restart until N nodes are started:
- #
- #gateway.recover_after_nodes: 3
- #
- # For more information, consult the gateway module documentation.
- #
- # ---------------------------------- Various -----------------------------------
- #
- # Require explicit names when deleting indices:
- #
- #action.destructive_requires_name: true
- http.cors.enabled: true#允许夸域访问,如head访问
- http.cors.allow-origin: "*"#允许夸域访问,如head访问
启动es:es/bin/elasticsearch
访问:http://192.168.0.104:9200出现如下即为成功
- {
- "name" : "-cpaasX",
- "cluster_name" : "elasticsearch",
- "cluster_uuid" : "4P1Bir1mSAiNC1V1r7EPjg",
- "version" : {
- "number" : "6.8.6",
- "build_flavor" : "default",
- "build_type" : "tar",
- "build_hash" : "3d9f765",
- "build_date" : "2019-12-13T17:11:52.013738Z",
- "build_snapshot" : false,
- "lucene_version" : "7.7.2",
- "minimum_wire_compatibility_version" : "5.6.0",
- "minimum_index_compatibility_version" : "5.0.0"
- },
- "tagline" : "You Know, for Search"
- }
第二步:解压elasticsearch-analysis-ik-6.8.6.zip到es/plugins目标下
tar -zxvf elasticsearch-analysis-ik-6.8.6.zip
解压后为:es/plugins/elasticsearch-analysis-ik-6.8.6
注:es/plugins/elasticsearch-analysis-ik-6.8.6/config可自定义配置分词
重启es:es/bin/elasticsearch
第三步:解压kibana-6.8.6-darwin-x86_64.tar.gz,与elasticsearch在同一级目录
tar -zxvf kibana-6.8.6-darwin-x86_64.tar.gz
配置:kibana-6.8.6/config/kibana.yml
- # Kibana is served by a back end server. This setting specifies the port to use.
- #server.port: 5601
- # Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
- # The default is 'localhost', which usually means remote machines will not be able to connect.
- # To allow connections from remote users, set this parameter to a non-loopback address.
- server.host: "0.0.0.0"#不要使用本IP,localhost和127.0.0.1,否则外网不能访问
- # Enables you to specify a path to mount Kibana at if you are running behind a proxy.
- # Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath
- # from requests it receives, and to prevent a deprecation warning at startup.
- # This setting cannot end in a slash.
- #server.basePath: ""
- # Specifies whether Kibana should rewrite requests that are prefixed with
- # `server.basePath` or require that they are rewritten by your reverse proxy.
- # This setting was effectively always `false` before Kibana 6.3 and will
- # default to `true` starting in Kibana 7.0.
- #server.rewriteBasePath: false
- # The maximum payload size in bytes for incoming server requests.
- #server.maxPayloadBytes: 1048576
- # The Kibana server's name. This is used for display purposes.
- #server.name: "your-hostname"
- # The URLs of the Elasticsearch instances to use for all your queries.
- #elasticsearch.hosts: ["http://localhost:9200"]
- elasticsearch.hosts: "http://192.168.0.104:9200"#es访问地址
- # When this setting's value is true Kibana uses the hostname specified in the server.host
- # setting. When the value of this setting is false, Kibana uses the hostname of the host
- # that connects to this Kibana instance.
- #elasticsearch.preserveHost: true
- # Kibana uses an index in Elasticsearch to store saved searches, visualizations and
- # dashboards. Kibana creates a new index if the index doesn't already exist.
- #kibana.index: ".kibana"
- # The default application to load.
- #kibana.defaultAppId: "home"
- # If your Elasticsearch is protected with basic authentication, these settings provide
- # the username and password that the Kibana server uses to perform maintenance on the Kibana
- # index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
- # is proxied through the Kibana server.
- #elasticsearch.username: "user"
- #elasticsearch.password: "pass"
- # Enables SSL and paths to the PEM-format SSL certificate and SSL key files, respectively.
- # These settings enable SSL for outgoing requests from the Kibana server to the browser.
- #server.ssl.enabled: false
- #server.ssl.certificate: /path/to/your/server.crt
- #server.ssl.key: /path/to/your/server.key
- # Optional settings that provide the paths to the PEM-format SSL certificate and key files.
- # These files validate that your Elasticsearch backend uses the same key files.
- #elasticsearch.ssl.certificate: /path/to/your/client.crt
- #elasticsearch.ssl.key: /path/to/your/client.key
- # Optional setting that enables you to specify a path to the PEM file for the certificate
- # authority for your Elasticsearch instance.
- #elasticsearch.ssl.certificateAuthorities: [ "/path/to/your/CA.pem" ]
- # To disregard the validity of SSL certificates, change this setting's value to 'none'.
- #elasticsearch.ssl.verificationMode: full
- # Time in milliseconds to wait for Elasticsearch to respond to pings. Defaults to the value of
- # the elasticsearch.requestTimeout setting.
- #elasticsearch.pingTimeout: 1500
- # Time in milliseconds to wait for responses from the back end or Elasticsearch. This value
- # must be a positive integer.
- #elasticsearch.requestTimeout: 30000
- # List of Kibana client-side headers to send to Elasticsearch. To send *no* client-side
- # headers, set this value to [] (an empty list).
- #elasticsearch.requestHeadersWhitelist: [ authorization ]
- # Header names and values that are sent to Elasticsearch. Any custom headers cannot be overwritten
- # by client-side headers, regardless of the elasticsearch.requestHeadersWhitelist configuration.
- #elasticsearch.customHeaders: {}
- # Time in milliseconds for Elasticsearch to wait for responses from shards. Set to 0 to disable.
- #elasticsearch.shardTimeout: 30000
- # Time in milliseconds to wait for Elasticsearch at Kibana startup before retrying.
- #elasticsearch.startupTimeout: 5000
- # Logs queries sent to Elasticsearch. Requires logging.verbose set to true.
- #elasticsearch.logQueries: false
- # Specifies the path where Kibana creates the process ID file.
- #pid.file: /var/run/kibana.pid
- # Enables you specify a file where Kibana stores log output.
- #logging.dest: stdout
- # Set the value of this setting to true to suppress all logging output.
- #logging.silent: false
- # Set the value of this setting to true to suppress all logging output other than error messages.
- #logging.quiet: false
- # Set the value of this setting to true to log all events, including system usage information
- # and all requests.
- #logging.verbose: false
- # Set the interval in milliseconds to sample system and process performance
- # metrics. Minimum is 100ms. Defaults to 5000.
- #ops.interval: 5000
- # Specifies locale to be used for all localizable strings, dates and number formats.
- #i18n.locale: "en"
启动kibana:kibana/bin/kibana
访问:http://192.168.0.104:5601如出现页面,即为成功
第四步:解压elasticsearch-head-master.zip,与elasticsearch在同一级目录
配置:elasticsearch-head-master/Gruntfile.js及安装node.js,grunt环境
- module.exports = function(grunt) {
- var fileSets = require("./grunt_fileSets.js");
- // Project configuration.
- grunt.initConfig({
- clean: {
- _site: {
- src: ['_site']
- }
- },
- concat: {
- vendorjs: {
- src: fileSets.vendorJs,
- dest: '_site/vendor.js'
- },
- vendorcss: {
- src: fileSets.vendorCss,
- dest: '_site/vendor.css'
- },
- appjs: {
- src: fileSets.srcJs,
- dest: '_site/app.js'
- },
- appcss: {
- src: fileSets.srcCss,
- dest: '_site/app.css'
- }
- },
- copy: {
- site_index: {
- src: 'index.html',
- dest: '_site/index.html',
- options: {
- process: function( src ) {
- return src.replace(/_site\//g, "");
- }
- }
- },
- base: {
- expand: true,
- cwd: 'src/app/base/',
- src: [ '*.gif', '*.png', '*.css' ],
- dest: '_site/base/'
- },
- iconFonts: {
- expand: true,
- cwd: 'src/vendor/font-awesome/fonts/',
- src: '**',
- dest: '_site/fonts'
- },
- i18n: {
- src: 'src/vendor/i18n/i18n.js',
- dest: '_site/i18n.js'
- },
- lang: {
- expand: true,
- cwd: 'src/app/lang/',
- src: '**',
- dest: '_site/lang/'
- },
- chrome: {
- src: 'src/chrome_ext/*.*',
- dest: '_site/'
- }
- },
- jasmine: {
- task: {
- src: [ fileSets.vendorJs, 'src/vendor/i18n/i18n.js', 'src/app/lang/en_strings.js', fileSets.srcJs ],
- options: {
- specs: 'src/app/**/*Spec.js',
- helpers: 'test/spec/*Helper.js',
- display: "short",
- summary: true
- }
- }
- },
- watch: {
- "scripts": {
- files: ['src/**/*', 'test/spec/*' ],
- tasks: ['default'],
- options: {
- spawn: false
- }
- },
- "grunt": {
- files: [ 'Gruntfile.js' ]
- }
- },
- connect: {
- server: {
- options: {
- port: 9100,
- hostname: '*',#增加通配地址,可以获取到集群es信息
- base: '.',
- keepalive: true
- }
- }
- }
- });
- grunt.loadNpmTasks('grunt-contrib-clean');
- grunt.loadNpmTasks('grunt-contrib-concat');
- grunt.loadNpmTasks('grunt-contrib-watch');
- grunt.loadNpmTasks('grunt-contrib-connect');
- grunt.loadNpmTasks('grunt-contrib-copy');
- grunt.loadNpmTasks('grunt-contrib-jasmine');
- // Default task(s).
- grunt.registerTask('default', ['clean', 'concat', 'copy', 'jasmine']);
- grunt.registerTask('server', ['connect:server']);
- grunt.registerTask('dev', [ 'default', 'watch' ]);
- };
启动head先进入head目录:cd elasticsearch-head-master
再启动head:grunt server
访问:http://localhost:9100出现页面,并能看到自带的kinaba索引即为成功
第五步:eclise新建一个springboot工程
配置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>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.5.RELEASE</version>
- <relativePath /> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.shierwin</groupId>
- <artifactId>es</artifactId>
- <packaging>war</packaging>
- <version>2.0.0-SNAPSHOT</version>
- <name>es</name>
- <description>es project</description>
- <properties>
- <java.version>1.8</java.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>#spring与es结合包
- </dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>#数据集转换工具
- <version>28.2-jre</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
配置application.properties
- #访问端口
- server.port=80
- #工程名称的访问路径,可不配
- server.servlet.context-path=/es
- spring.data.elasticsearch.repositories.enabled =true#设置repositories可用
- spring.data.elasticsearch.cluster-name =elasticsearch#设置es集群名称
- spring.data.elasticsearch.cluster-nodes =192.168.0.104:9300#设置es集群名称地址,多地址可以用逗号分开
编写对象,如问题库对象:
- package com.shierwin.es;
- import org.springframework.data.elasticsearch.annotations.Document;
- @Document(indexName = "shierwin", type = "_doc", shards = 3, replicas = 1)//连接es的索引名,类型,分片,副文本
- public class QtDo {
- private String id;
- private String qustion;
- private String anwser;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getQustion() {
- return qustion;
- }
- public void setQustion(String qustion) {
- this.qustion = qustion;
- }
- public String getAnwser() {
- return anwser;
- }
- public void setAnwser(String anwser) {
- this.anwser = anwser;
- }
- }
编写操作es增删改查接口:
- package com.shierwin.es;
- public interface QtService {
- //查询所有,id倒序排
- public void searchAll();
- //根据id查询
- public void searchOne();
- //新增
- public void addOne() ;
- //修改
- public void updateOne();
- //删除
- public void deleteOne();
- //聚合条件查询
- public void searchCondition();
- }
编写操作es增删改查接口实现类:
- package com.shierwin.es;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Optional;
- import org.elasticsearch.index.query.QueryBuilder;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.domain.Sort;
- import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
- import org.springframework.stereotype.Service;
- import com.google.common.collect.Lists;
- @Service
- public class QtServiceImp implements QtService {
- @Autowired
- private ElasticsearchRepository<QtDo, String> qtRepository;
- //查询所有,id倒序排
- @Override
- public void searchAll() {
- Iterable<QtDo> iterable = qtRepository.findAll(Sort.by("_id").ascending());
- List<QtDo> list = Lists.newArrayList(iterable);
- for (int i = 0; i < list.size(); i++) {
- QtDo qt = list.get(i);
- System.out.println(qt.getId()+"::"+qt.getQustion()+"::"+qt.getAnwser());
- }
- }
- //根据id查询
- @Override
- public void searchOne() {
- Optional<QtDo> qt = qtRepository.findById("WSUiL3ABPy5Bh_EkO65y");
- QtDo tempQt = new QtDo();
- QtDo qtObj = qt.orElse(tempQt);
- System.out.println(qtObj.getId()+":"+qtObj.getQustion()+":"+qtObj.getAnwser());
- }
- //新增
- @Override
- public void addOne() {
- QtDo qt = new QtDo();
- qt.setQustion("8");
- qt.setAnwser("15");
- qt = (QtDo) qtRepository.save(qt);
- System.out.println(qt.getId()+"::"+qt.getQustion()+"::"+qt.getAnwser());
- }
- //修改
- @Override
- public void updateOne() {
- QtDo qt = new QtDo();
- qt.setId("PKxkUXABp_H2y73Zm7i_");
- qt.setQustion("你城市的房子买在那呀?7");
- qt.setAnwser("江西省南昌市象湖新城幸福时光二期7");
- qt = (QtDo) qtRepository.save(qt);
- System.out.println(qt.getId()+"::"+qt.getQustion()+"::"+qt.getAnwser());
- }
- //删除
- @Override
- public void deleteOne() {
- qtRepository.deleteById("36xjUXABp_H2y73Z27fC");
- }
- //聚合条件查询
- @Override
- public void searchCondition() {
- int pageNum = 0;
- int pageSize = 30;
- Pageable pageable = PageRequest.of(pageNum, pageSize);
- // QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("qustion", "房");//模湖词查询
- // QueryBuilder queryBuilder = QueryBuilders.termQuery("qustion", "8");//没有分词的精确查询
- // QueryBuilder queryBuilder = QueryBuilders.matchQuery("qustion", "疫情");//分词查询
- // QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("qustion", "疫情");//精确分词短语查询
- QueryBuilder queryBuilder = QueryBuilders
- .boolQuery()
- .must(QueryBuilders.termQuery("qustion", "8"))
- .must(QueryBuilders.termQuery("anwser", "15"));
- Page<QtDo> pageList = qtRepository.search(queryBuilder, pageable);
- Iterator<QtDo> iterator= pageList.iterator();
- while(iterator.hasNext()){
- QtDo qt = (QtDo) iterator.next();
- System.out.println(qt.getId()+"::"+qt.getQustion()+"::"+qt.getAnwser());
- }
- }
- }
编写ElasticsearchRepository引用接口,不加这接口,springboot在启动时会注入异常:
- public interface QtRepository extends ElasticsearchRepository {
- }
第六步:启动spring application即可读取到es的数据
完整的工程源码下载:https://download.csdn.net/download/shiquanlong/12166806