35、springboot——springboot与检索___项目中进行整合(2)

一、整合 新建项目加入依赖(NoSql)

springboot 默认使用SpringDate ElasticSearch模块进行操作elasticSearch
 查看自动配置类:

SpringBoot支持两种技术来和ES交互“
  1、Jest

 

  2、SpringDate ElasticSearch
SpringDate ElasticSearch下的一些自动配置:
@Bean
@ConditionalOnMissingBean
public TransportClient elasticsearchClient() throws Exception {
    TransportClientFactoryBean factory = new TransportClientFactoryBean();
    factory.setClusterNodes(this.properties.getClusterNodes());
    factory.setProperties(this.createProperties());
    factory.afterPropertiesSet();
    return factory.getObject();
}

private Properties createProperties() {
    Properties properties = new Properties();
    properties.put("cluster.name", this.properties.getClusterName());
    properties.putAll(this.properties.getProperties());
    return properties;
}

public class ElasticsearchProperties {
    private String clusterName = "elasticsearch";
    private String clusterNodes;
    private Map<String, String> properties = new HashMap();

@Bean
@ConditionalOnMissingBean
@ConditionalOnBean({Client.class})
public ElasticsearchTemplate elasticsearchTemplate(Client client, ElasticsearchConverter converter) {
    try {
        return new ElasticsearchTemplate(client, converter);
    } catch (Exception var4) {
        throw new IllegalStateException(var4);
    }
}

二、测试

1、Jest方式

1.1、导入jest依赖

因为springboot默认使用SpringDate ElasticSearch模块进行操作elasticSearch;所以要想使用jest方式操作elasticsearch需要导入jest的依赖(jest的版本可根据自己安装的elasticSearch版本进行像匹配)

 我的elasticSearch的版本为5.6.12,所选的jest版本为5.3.4为例

<!--使用jest操作elasticsearch-->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.4</version>
        </dependency>

查看jest的一些自动配置:

@Bean(
    destroyMethod = "shutdownClient"
)
@ConditionalOnMissingBean
public JestClient jestClient() {
    JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(this.createHttpClientConfig());
    return factory.getObject();
}

:默认访问本机的elasticSearch端口

private List<String> uris = new ArrayList(Collections.singletonList("http://localhost:9200"));

2.2在properties配置文件中修改访问elasticSearch的路径

我在linux虚拟机中开启的elasticSeach,我的linux的ip地址为192.168.237.132,所以配置如下

spring.elasticsearch.jest.uris=http://192.168.237.132:9200

2.3创建一个pojo类

public class Article {
    @JestId         //Jest通过此注解可识别主键
    private Integer id;
    private String author;
    private String title;
    private String content;
.....

2.4单元测试类中写测试方法

    @Autowired
    JestClient jestClient;

    @Test
    public void contextLoads() {
        //1、给Es中索引(保存)一个文档
        Article article = new Article();
        article.setId(1);
        article.setAuthor("zhangsan");
        article.setTitle("好消息");
        article.setContent("Hello World");

        //构建一个索引功能
        /*因为在Article类中@JestId标识了id所以不需要指定id
        Index build = new Index.Builder(article).index("atguigu").type("news").id("1").build();*/
        Index build = new Index.Builder(article).index("atguigu").type("news").build();
        try {
            jestClient.execute(build);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2.5运行上面的测试方法可以查看结果:

2.6 测试搜索功能的实现:

    //测试搜索
    @Test
    public void search(){
        //查询表达式(这里的表达式是全文搜索content属性有hello)
        String searchJson = "{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"content\" : \"hello\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        //构建搜索功能
        Search search = new Search.Builder(searchJson).addIndex("atguigu").addType("news").build();

        try {
            //返回搜索结果
            SearchResult searchResult = jestClient.execute(search);
            System.out.println(searchResult.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

运行结果:控制台打印

{"took":155,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.25811607,"hits":[{"_index":"atguigu","_type":"news","_id":"1","_score":0.25811607,"_source":{"id":1,"author":"zhangsan","title":"好消息","content":"Hello World"}}]}}

猜你喜欢

转载自www.cnblogs.com/lyh233/p/12688833.html
今日推荐