SpringBoot-ElasticSearch

修改ES依赖版本与安装的ES软件版本一致

    <properties>
        <java.version>1.8</java.version>
        <!--    修改依赖的版本与安装的elasticsearch版本一致     -->
        <elasticsearch.version>7.6.2</elasticsearch.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

容器加入ES的高等级的 rest 风格的客户端工具类:RestHighLevelClient

@Configuration
public class Config {
    /*
    将elasticsearch客户端扫描进容器,将通过此客户端对elasticsearch进行操作
     */
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        return new RestHighLevelClient(
                RestClient.builder(// 参数为 HttpHost 的数组,如果是集群的话同时写多个 HttpHost
                        new HttpHost("localhost", 9200, "http")));

    }
}

数据实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class User {
    private String name;
    private int age;
    private double sal;
    private String birth;
}

对索引及文档的操作

@SpringBootTest
class ElasticApplicationTests {
    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 对索引的操作:请求:**IndexRequest,响应:**IndexResponse
     */

    // 创建索引  CreateIndexRequest
    @Test
    void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("muchen");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

    // 获取索引,判断其是否存在  GetIndexRequest
    @Test
    void getIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("muchen");
        // 获得索引
//        GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
        // 判断索引是否存在
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 删除索引  DeleteIndexRequest
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("muchen");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

    // 添加文档  IndexRequest  .index
    @Test
    void addDocument() throws IOException {
        // 创建数据对象
        User user = new User("张三", 12, 9000, "2020-02-05");
        // 创建针对指定索引的请求
        IndexRequest request = new IndexRequest("muchen");
        // 设定一些规则
        request.id("1");
        request.timeout("5s");
        // 将数据对象  (json格式) 放入请求中
        request.source(JSON.toJSONString(user), XContentType.JSON);
        // 发送数据
        IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString());
    }

    // 判断文档是否存在
    @Test
    void isExistDoc() throws IOException {
        // 创建指定索引库里的指定文档请求
        GetRequest request = new GetRequest("muchen", "1");
        // 发送请求
        boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 获取文档  GetRequest  .get
    @Test
    void getDocument() throws IOException {
        GetRequest request = new GetRequest("muchen", "1");
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        System.out.println(response.getSourceAsString());
        System.out.println(response);
    }

    // 更新文档  UpdateRequest  .update
    @Test
    void updateDocument() throws IOException {
        // 创建要更新的数据对象
        User user = new User("猪刚鬣不刚烈", 100, 1999, "1799-09-09");
        // 创建指定索引里指定文档的更新请求
        UpdateRequest request = new UpdateRequest("muchen", "1");
        request.timeout("5s");
        // 将数据放入请求中
        request.doc(JSON.toJSONString(user),XContentType.JSON);
        // 发送数据
        UpdateResponse updateResponse = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        System.out.println(updateResponse);
        System.out.println(updateResponse.status());
    }

    // 删除文档  DeleteRequest  .delete
    @Test
    void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("muchen", "1");
        request.timeout("5s");
        DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        System.out.println(deleteResponse);
        System.out.println(deleteResponse.status());
    }

    // 批量插入文档
    @Test
    void bulkDocument() throws IOException {
        // 创建批量请求
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("5s");
        // 创建批量数据
        List<User> list = new ArrayList<>();
        list.add(new User("张三",13,33333,"1909-09-09"));
        list.add(new User("张三去",12,3333,"1909-09-09"));
        list.add(new User("张三我",14,9333,"1909-09-09"));
        list.add(new User("张三饿",15,33993,"1909-09-09"));
        list.add(new User("张三如律",13,6333,"1909-09-09"));
        list.add(new User("张三他",123,53333,"1909-09-09"));
        list.add(new User("张三有",3,3633,"1909-09-09"));
        // 批量数据放入请求中;不指定id则随机产生字符串Id
        for (int i = 0; i < list.size(); i++) {
            // 批量修改 UpdateRequest;批量删除 DeleteRequest
            bulkRequest.add(new IndexRequest("muchen").id("" + (i+1)).source(JSON.toJSONString(list.get(i)),XContentType.JSON));
        }
        // 发送批量请求
        BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.status());
        System.out.println(bulkResponse);
    }

    // 查询
    @Test
    void searchDocument() throws IOException {
        // 创建搜索请求
        SearchRequest searchRequest = new SearchRequest("muchen");
        // 创建条件搜索构建器
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 创建搜索条件
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "张");
        // 搜索条件放入搜索构建器
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(10000));
        // 搜索构建器放入搜索请求中
        searchRequest.source(searchSourceBuilder);
        // 发送请求
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(JSON.toJSONString(hit.getSourceAsMap()));
        }
    }
}

实例

        爬取数据

        <!--  解析网页    -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.2</version>
        </dependency>
public class HtmlParseUtil {

    /**
     * 为索引库爬取数据
     * @param keyword
     * @return
     * @throws IOException
     */
    public List<Content> getData(String keyword) throws IOException {
        String url = "https://search.suning.com/"+keyword+"/";
        // 解析网页,返回浏览器的 Document 对象
        Document document = Jsoup.parse(new URL(url),15000);
        // 操作 documen对象获取页面元素即可,方法就是js中对应的方法
        Element element = document.getElementById("product-list");
        Elements lis = element.getElementsByTag("li");
        List<Content> list = new ArrayList<>();
        for (Element li : lis) {
            String img = li.getElementsByTag("img").eq(0).attr("src");
            String price = JSON.parseObject(li.getElementsByClass("sellPoint").eq(0).attr("sa-data")).getString("prdid").substring(7);
            String title = li.getElementsByClass("title-selling-point").eq(0).text();
            list.add(new Content(img,price,title));
        }
        return list;
    }
}
/**
     * 爬取数据导入索引库
     * @param keyword
     * @return
     * @throws IOException
     */
    public boolean parseData(String keyword) throws IOException {
        List<Content> contentList = new HtmlParseUtil().getData(keyword);
        BulkRequest bulkRequest = new BulkRequest();
        for (Content content : contentList) {
            bulkRequest.add(new IndexRequest("su_list").source(JSON.toJSONString(content), XContentType.JSON));
        }
        BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulkResponse.hasFailures();
    }

查询

@Service
public class ContentService {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 普通搜索
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> searchData(String keyword, int pageNo, int pageSize) throws IOException {
        SearchRequest searchRequest = new SearchRequest("su_list");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 分页
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);
        // 精准匹配 termQuery
        MatchQueryBuilder termQuery = QueryBuilders.matchQuery("title", keyword);
        searchSourceBuilder.query(termQuery);
        searchSourceBuilder.timeout(new TimeValue(30000));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        // 处理结果,数据返回
        List<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            list.add(documentFields.getSourceAsMap());
        }
        return list;
    }

    /**
     * 高亮搜索
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> searchHighLightData(String keyword, int pageNo, int pageSize) throws IOException {
        SearchRequest searchRequest = new SearchRequest("su_list");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 分页设置
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);

        // 高亮设置
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        // 设置要高亮的字段
        highlightBuilder.field("title");
        // 高亮字段的标签样式
        highlightBuilder.preTags("<span style='color:red;'>");
        highlightBuilder.postTags("</span>");
        // false:同时显示多个高亮
        highlightBuilder.requireFieldMatch(false);
        searchSourceBuilder.highlighter(highlightBuilder);

        // 精准匹配 termQuery
        MatchQueryBuilder termQuery = QueryBuilders.matchQuery("title", keyword);
        searchSourceBuilder.query(termQuery);
        searchSourceBuilder.timeout(new TimeValue(30000));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        // 处理结果,数据返回
        List<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
            // 获取高亮字段
            Map<String, HighlightField> highlightFields = documentFields.getHighlightFields();
            HighlightField title = highlightFields.get("title");
            // 将高亮字段转换成字符串,方便替换
            String highLightTitle = "";
            if(title != null){
                Text[] fragments = title.fragments();
                for (Text fragment : fragments) {
                    highLightTitle += fragment;
                }
            }
            // 替换源数据中的字段
            sourceAsMap.put("title", highLightTitle);
            list.add(sourceAsMap);
        }
        return list;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值