全文检索 Elasticearch 多条件检索 过滤检索 分词检索

时间:2021-08-28 01:12:20

1.ElasticaSearch安装

1.下载

下载最新版本: https://www.elastic.co/cn/elasticsearch/
全文检索 Elasticearch 多条件检索 过滤检索 分词检索

2.根据不同的系统平台下载相应的压缩包

全文检索 Elasticearch 多条件检索 过滤检索 分词检索

2. elasticsearch.yml

配置格式是YAML,可以采用如下两种方式:
方式1:层次方式
path: data: /var/lib/elasticsearch logs: /var/log/elasticsearch
方式2:属性方式
path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch
本项目采用方式2,例子如下:

cluster.name: wf
node.name: xc_node_1
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["0.0.0.0:9300", "0.0.0.0:9301"]
discovery.zen.minimum_master_nodes: 1
node.ingest: true
bootstrap.memory_lock: false
node.max_local_storage_nodes: 2
# 数据路径
path.data: E:\elasticsearch\elasticsearch\data
# 日志路径
path.logs: E:\elasticsearch\elasticsearch\logs

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

注意path.data和path.logs路径配置正确。
常用的配置项如下:
cluster.name:配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称。
node.name:节点名,通常一台物理服务器就是一个节点,es会默认随机指定一个名字,建议指定一个有意义的名称,方便管理一个或多个节点组成一个cluster集群,集群是一个逻辑的概念,节点是物理概念。

3. 启动elasticsearch

1. 进入bin录下,运行elasticsearch程序即可

全文检索 Elasticearch 多条件检索 过滤检索 分词检索
bin:脚本目录,包括:启动、停止等可执行脚本
config:配置文件目录
data:索引目录,存放索引文件的地方
logs:日志目录
modules:模块目录,包括了es的功能模块
plugins :插件目录,es支持插件机制

2.运行elasticsearch.bat

全文检索 Elasticearch 多条件检索 过滤检索 分词检索

3.或在cmd下运行:elasticsearch.bat

全文检索 Elasticearch 多条件检索 过滤检索 分词检索

4.安装 Elasticsearch 服务

elasticsearch-service.bat install

在系统服务里面就有了
全文检索 Elasticearch 多条件检索 过滤检索 分词检索
更多命令

启动Elasticsearch服务:
elasticsearch-service.bat start

停止Elasticsearch服务:
elasticsearch-service.bat stop

安装Elasticsearch服务:
elasticsearch-service.bat install

卸载Elasticsearch服务:
elasticsearch-service.bat remove

浏览器输入:http://localhost:9200
显示结果如下(配置不同内容则不同)说明ES启动成功:
全文检索 Elasticearch 多条件检索 过滤检索 分词检索

4. 使用Postman 创建索引库

1. 使用postman或curl这样的工具创建:

put 方式 http://localhost:9200/索引库名称
全文检索 Elasticearch 多条件检索 过滤检索 分词检索

5. 创建 IDEA 项目

1. 创建myes 项目

全文检索 Elasticearch 多条件检索 过滤检索 分词检索

2. 添加依赖

   <!-- 父依赖总版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <dependencies>
        <!-- web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 高级 es 依赖 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.5.4</version>
        </dependency>
        <!-- es依赖 -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.5.4</version>
        </dependency>
    </dependencies>

3. 创建 application.yml 配置文件

my:
  elasticsearch:
    hostlist: 127.0.0.1:9200 #配置 IP地址和端口

4. 在 config 包下创建MyElasticsearchConfig

package com.ddz.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyElasticsearchConfig {
    @Value("${my.elasticsearch.hostlist}")
    private String hostlist;

    @Bean
    public RestHighLevelClient createClient() {
        String[] split = hostlist.split(",");
        HttpHost[] httpHosts = new HttpHost[split.length];
        for (int i = 0; i < split.length; i++) {
            String hostStr = split[i];
            httpHosts[i] = new HttpHost(hostStr.split(":")[0],
                    Integer.parseInt(hostStr.split(":")[1]), "http");
        }
        return new RestHighLevelClient(RestClient.builder(httpHosts));
    }
}

6. 创建测试类MyEsTest

package com.ddz.demo;

import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyEsTest {
    @Resource
    private RestHighLevelClient client;

    /* 创建映射*/
    public void createIndex() throws Exception {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_course");
        createIndexRequest.settings(Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0));
        createIndexRequest.mapping("doc", "{\n" +
                "\t\"properties\": {\n" +
                "\t\t\"description\": {\n" +
                "\t\t\t\"type\": \"text\",\n" +
                "\t\t\t\"analyzer\": \"ik_max_word\",\n" +
                "\t\t\t\"search_analyzer\": \"ik_smart\"\n" +
                "\t\t},\n" +
                "\t\t\"name\": {\n" +
                "\t\t\t\"type\": \"text\",\n" +
                "\t\t\t\"analyzer\": \"ik_max_word\",\n" +
                "\t\t\t\"search_analyzer\": \"ik_smart\"\n" +
                "\t\t},\n" +
                "\t\t\"pic\": {\n" +
                "\t\t\t\"type\": \"text\",\n" +
                "\t\t\t\"index\": false\n" +
                "\t\t},\n" +
                "\t\t\"price\": {\n" +
                "\t\t\t\"type\": \"float\"\n" +
                "\t\t},\n" +
                "\t\t\"studymodel\": {\n" +
                "\t\t\t\"type\": \"keyword\"\n" +
                "\t\t},\n" +
                "\t\t\"timestamp\": {\n" +
                "\t\t\t\"type\": \"date\",\n" +
                "\t\t\t\"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd\"\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}", XContentType.JSON);
        IndicesClient indices = client.indices();
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println(acknowledged);
    }


    /*添加文档*/
    public void addDoc() throws Exception {
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("name", "spring cloud实战");
        jsonMap.put("description", "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud基础入门 3.实战Spring Boot 4.注册中心eureka。");
        jsonMap.put("studymodel", "201001");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jsonMap.put("timestamp", dateFormat.format(new Date()));
        jsonMap.put("price", 5.6f);
        IndexRequest indexRequest = new IndexRequest("my_course", "doc");
        indexRequest.source(jsonMap);
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        DocWriteResponse.Result result = indexResponse.getResult();
        System.out.println(result.name());
    }

    /*查询单个*/
    public void getDoc() throws Exception {
        GetRequest getRequest = new GetRequest("my_course", "doc", "DN2jK3UB9F4_AyODZgTx");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        boolean exists = getResponse.isExists();
        System.out.println(exists);
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        System.out.println(sourceAsMap);
    }

    /*查询全部*/
    public void queryAll() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        //
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置全部查询
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }

    }

    /*分页查询*/
    public void queryPage() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置全部查询
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //设置分页 开始 和 大小
        searchSourceBuilder.from(2);
        searchSourceBuilder.size(2);
        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /* 精准不分词查询 keywor */
    public void queryTerm() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置全部查询
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //设置分页 开始 和 大小
        searchSourceBuilder.from(2);
        searchSourceBuilder.size(2);
        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /* 多条精准数据查询*/
    public void queryTerms() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String[] sm = {"201001", "201002"};
        //设置全部查询
        searchSourceBuilder.query(QueryBuilders.termsQuery("studymodel", sm));
        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /*单个字段的分词查询*/
    public void queryMatch() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String keywords = "spring开发框架";
        //设置全部查询并设置分词匹配率
        searchSourceBuilder.query(QueryBuilders.matchQuery("description", keywords).minimumShouldMatch("80%"));
        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /*分词陪陪多个字段查询*/
    public void queryMultiMatch() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String keywords = "spring框架";
        //设置全部查询并                                                                                      提升优先级
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery(keywords, "name", "description").field("name", 10));
        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /*多条件的分词查询*/
    public void queryBool() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String keywords = "spring框架";
        //分词的查询
        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keywords, "name", "description").field("name", 10);// 提升优先级
        //精准的查询

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", 201002);
        // 组合 以上查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(multiMatchQueryBuilder);
        boolQueryBuilder.must(termQueryBuilder);
        //执行查询
        searchSourceBuilder.query(boolQueryBuilder);

        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }

    /*多条件过滤,多个字段的分词查询*/
    public void queryFilter() throws Exception {
        //设置索引库
        SearchRequest searchRequest = new SearchRequest("my_course");
        //设置查询方式
        searchRequest.types("doc");
        //创建查询方法
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String keywords = "spring框架";
        //分词的查询
        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keywords, "name", "description").field("name", 10);// 提升优先级
        //精准的查询
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", 201002);
        //范围查询
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price").gte(60).lte(100);
        //组合查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(multiMatchQueryBuilder);
        boolQueryBuilder.filter(termQueryBuilder);
        boolQueryBuilder.filter(rangeQueryBuilder);
        //执行查询
        searchSourceBuilder.query(boolQueryBuilder);

        searchRequest.source(searchSourceBuilder);
        //获取响应对象
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //获取 hist
        SearchHits searchHits = searchResponse.getHits();
        //获取 hist 下的hist 数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String id = hit.getId();
            String index = hit.getIndex();
            System.out.println(id + "---------" + index);
            //获取每个对象
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }
}

7. 创建启动类 MyEsApp

package com.ddz;

import com.ddz.demo.MyEsTest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class MyEsApp {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(MyEsApp.class, args);
        MyEsTest bean = applicationContext.getBean(MyEsTest.class);
        bean.queryFilter();
    }
}

全文检索 Elasticearch 多条件检索 过滤检索 分词检索

8. 启动服务在控制台得到测试数据

全文检索 Elasticearch 多条件检索 过滤检索 分词检索