实现基于Elasticsearch的搜索服务

07-09 1068阅读

实现基于Elasticsearch的搜索服务

大家好,我是微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!

实现基于Elasticsearch的搜索服务
(图片来源网络,侵删)
1. Elasticsearch简介

Elasticsearch是一个开源的分布式搜索引擎,提供强大的全文搜索和分析功能。本文将详细介绍如何在Java应用中集成和使用Elasticsearch来构建高效的搜索服务。

2. 环境准备与配置

在开始之前,确保已经安装并配置好Elasticsearch服务,并且可以访问其REST API。

3. Java集成Elasticsearch
3.1 添加Elasticsearch依赖

首先,在项目的pom.xml文件中添加Elasticsearch的依赖:

    org.elasticsearch
    elasticsearch
    7.16.0

3.2 编写Elasticsearch客户端配置
package cn.juwatech.elasticsearch;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}
3.3 创建Elasticsearch文档实体
package cn.juwatech.elasticsearch;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "products")
public class Product {
    @Id
    private String id;
    private String name;
    private String description;
    private double price;
    // 省略构造方法和getter/setter
}
3.4 定义Elasticsearch数据访问接口
package cn.juwatech.elasticsearch;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ProductRepository extends ElasticsearchRepository {
    List findByName(String name);
    List findByDescription(String description);
    List findByNameOrDescription(String name, String description);
}
4. 使用Elasticsearch进行搜索
4.1 添加数据到Elasticsearch索引
package cn.juwatech.elasticsearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
    @Autowired
    private ProductRepository productRepository;
    public void saveProduct(Product product) {
        productRepository.save(product);
    }
    public Iterable findAllProducts() {
        return productRepository.findAll();
    }
}
4.2 执行搜索操作
package cn.juwatech.elasticsearch;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductSearchService {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    public List searchProducts(String query) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.queryStringQuery(query));
        return elasticsearchRestTemplate.queryForList(
                sourceBuilder.build(), Product.class);
    }
}
5. 总结

本文介绍了如何利用Java语言集成Elasticsearch,实现基于Elasticsearch的高效搜索服务。从配置Elasticsearch客户端、定义文档实体、编写数据访问层到执行搜索操作,详细展示了整个实现过程。

微赚淘客系统3.0小编出品,必属精品,转载请注明出处!

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]