windows下elasticsearch配置及spring boot 简单demod的 整合

时间:2024-08-01 14:05:02

学习过程:

  1. elasticsearch 下载安装
  2. elasticsearch-head 安装
  3. spring boot 下elasticsearch的配置
  4. 使用ElasticsearchRepository实现增删改查(ElasticsearchRepository,elasticsearchTemplate)
  5. 如何优雅的使用FunctionScoreQueryBuilder
  6. 测试

一、elasticsearch 下载安装:ElasticSearch官网:http://www.elasticsearch.org

在安装Elasticsearch之前我们需要先安装jdk的环境,这些都是老生常谈,我们不去多加叙述,具体的安装步骤我们可以参考https://www.cnblogs.com/ljhdo/p/4887557.html ,这里有详细的Elasticsearch及jdk安装步骤。安装好之后我们可以找到安装目录bin下的批处理文件来启动项目.

windows下elasticsearch配置及spring boot 简单demod的 整合

windows下elasticsearch配置及spring boot 简单demod的 整合

看到这样的界面后我们可以在浏览器里输入http://localhost:9200/可以看到返回了一段json,其中对外服务的http端口,默认为9200,9300是客户端的端口。在这里elasticsearch我们就安装完了。

{
"name": "node-1",
"cluster_name": "my-application",
"cluster_uuid": "YWYqGhDnSE-z3pbVDEs8rQ",
"version": {
"number": "6.3.0",
"build_flavor": "default",
"build_type": "zip",
"build_hash": "424e937",
"build_date": "2018-06-11T23:38:03.357887Z",
"build_snapshot": false,
"lucene_version": "7.3.1",
"minimum_wire_compatibility_version": "5.6.0",
"minimum_index_compatibility_version": "5.0.0"
},
"tagline": "You Know, for Search"
}

二、elasticsearch-head 安装

elasticsearch安装完后我们需要安装head插件管理我们的elasticsearch,上面链接教程中elasticsearch使用的是2.4.4的版本,而我用的是6.3.0的版本,在cmd中使用es命令的方式已经不可用了。我们需要自己区去官网下载安装包,在这之前还需要先安装node.js和grunt,参考https://www.cnblogs.com/Onlywjy/p/Elasticsearch.html我们能很快的完成elasticsearch及head的安装和配置。安装完成后我们可以通过cmd进入到head的安装目录通过“npm run start ”来启动head插件,在浏览器中输入”http://localhost:9100“来访问。

windows下elasticsearch配置及spring boot 简单demod的 整合

windows下elasticsearch配置及spring boot 简单demod的 整合

三、spring boot 下配置

pom依赖:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

application.yml

spring:
data:
elasticsearch:
cluster-name: my-application #elasticsearch/config文件下elasticsearch.yml中设置的cluster.name
        cluster-nodes: 127.0.0.1:9300 #客户端端口,启动elasticsearch时默认为9300

四、使用ElasticsearchRepository实现增删改查

参考https://blog.****.net/larger5/article/details/79777319,偷懒的同学,可以直接看下面,我们完成了pojo,dao,controller的编写,由于只是做了个demo就没有使用service层去规范。在clone链接中代码时候我们会遇到一些错误,下面我们着手解决这些错误。

pojo

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document; /**
* @Author: gaofeng_peng
* @Date: 2018/6/24 10:44
*/
@Document(indexName = "product", type = "book")
public class Book {
@Id
String id;
String name;
String message;
String type; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
}

在上述代码中@Document注解中 indexName指的是索引,可以理解成mysql中数据库 ,type既对应的是数据表。

dao

public interface BookDao extends ElasticsearchRepository<Book, String> {

    Book findBooksById(String id);

    void deleteById(String id);
}

参考的文档中,dao层没有写接口,在后面的实现中会报错,忖度作者的用意controller下getBookById中 bookDao.findOne() 方法 对应了 findBooksById,insertBook中bookDao.delete() 对应deleteById,相信这么简单大家都能看出来。

controller

package com.bookstore.controller.backend;

import com.bookstore.dao.BookDao;
import com.bookstore.pojo.Book;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
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.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.*; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; /**
* @Author: gaofeng_peng
* @Date: 2018/6/24 11:02
*/
@RestController
@RequestMapping("book")
public class BookController {
@Autowired
private BookDao bookDao; /**
* 1、查 id
*
* @param id
* @return
*/
@GetMapping("/get/{id}")
public Book getBookById(@PathVariable String id) {
return bookDao.findBooksById(id);
} /**
* 2、查 ++:全文检索(根据整个实体的所有属性,可能结果为0个)
*
* @param q
* @return
*/
@GetMapping("/select/{q}")
public List<Book> testSearch(@PathVariable String q) {
QueryStringQueryBuilder builder = new QueryStringQueryBuilder(q);
Iterable<Book> searchResult = bookDao.search(builder);
Iterator<Book> iterator = searchResult.iterator();
List<Book> list = new ArrayList<Book>();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
} /**
* 3、查 +++:分页、分数、分域(结果一个也不少)
*
* @param page
* @param size
* @param q
* @return
*/
@GetMapping("/{page}/{size}/{q}")
public List<Book> searchCity(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String q) { // 分页参数
Pageable pageable = new PageRequest(page, size); FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
QueryBuilders.matchQuery("name", q),
ScoreFunctionBuilders.weightFactorFunction(1000)),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
QueryBuilders.matchQuery("message", q),
ScoreFunctionBuilders.weightFactorFunction(1000))
}; FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions);
// 分数、分页
SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build(); Page<Book> searchPageResults = bookDao.search(searchQuery);
return searchPageResults.getContent(); } /**
* 4、增
*
* @param book
* @return
*/
@PostMapping("/insert")
public Book insertBook(Book book) {
bookDao.save(book);
return book;
} /**
* 5、删 id
*
* @param id
* @return
*/
@DeleteMapping("/delete/{id}")
public Book insertBook(@PathVariable String id) {
Book book = bookDao.findBooksById(id);
bookDao.deleteById(id);
return book;
} /**
* 6、改
*
* @param book
* @return
*/
@PutMapping("/update")
public Book updateBook(Book book) {
bookDao.save(book);
return book;
} }

在这里我们要着重讲一下参考文档中的searchCity 方法,下面是作者的写法:

 /**
* 3、查 +++:分页、分数、分域(结果一个也不少)
* @param page
* @param size
* @param q
* @return
* @return
*/
@GetMapping("/{page}/{size}/{q}")
public List<Book> searchCity(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String q) { // 分页参数
Pageable pageable = new PageRequest(page, size); // 分数,并自动按分排序
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("name", q)),
ScoreFunctionBuilders.weightFactorFunction(1000)) // 权重:name 1000分
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("message", q)),
ScoreFunctionBuilders.weightFactorFunction(100)); // 权重:message 100分 // 分数、分页
SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build(); Page<Book> searchPageResults = bookDao.search(searchQuery);
return searchPageResults.getContent(); }

大家注意下红色的部分,由于我们在配置依赖时候没有指定elasticsearch的版本,现在如果还是直接clone上面的依赖的话会发现已经没有add的方法了,一种方式去指定版本,都走到这一步了,我们采取另一种方式使用

FunctionScoreQueryBuilder functionScoreQuery(ScoreFunctionBuilder function)方法,具体看下面
 public List<Book> searchCity(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String q) {

        // 分页参数
Pageable pageable = new PageRequest(page, size); FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
QueryBuilders.matchQuery("name", q),
ScoreFunctionBuilders.weightFactorFunction(1000)),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
QueryBuilders.matchQuery("message", q),
ScoreFunctionBuilders.weightFactorFunction(1000))
}; FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions);
// 分数、分页
SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build(); Page<Book> searchPageResults = bookDao.search(searchQuery);
return searchPageResults.getContent(); }

上面我们使用的是:SpringData 封装,直接在 dao 接口继承 ElasticsearchRepository的方式,作者很全面还提供了elasticsearchTemplate的方式,

package com.bookstore.controller.backend;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList;
import java.util.List;
import java.util.Map; /**
* @Author: gaofeng_peng
* @Date: 2018/6/24 10:58
*/
@RestController
@RequestMapping("/template")
public class BookControllerTemplate { @Autowired
ElasticsearchTemplate elasticsearchTemplate; /**
* 查询所有
* @throws Exception
*/
@GetMapping("/all")
public List<Map<String, Object>> searchAll() throws Exception {
//这一步是最关键的
Client client = elasticsearchTemplate.getClient();
// @Document(indexName = "product", type = "book")
SearchRequestBuilder srb = client.prepareSearch("product").setTypes("book");
SearchResponse sr = srb.setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(); // 查询所有
SearchHits hits = sr.getHits();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SearchHit hit : hits) {
Map<String, Object> source = hit.getSource();
list.add(source);
System.out.println(hit.getSourceAsString());
}
return list;
}
}

到此位置简单的增删改查就完成了,还需一点注意的是作者@RestController什么的没加,记得加上。。。。。

五、如何优雅的使用FunctionScoreQueryBuilder

福利链接:https://www.programcreek.com/java-api-examples/index.php?api=org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder,找了老半天,必须给我个