本文介绍了如何整合搜索引擎elasticsearch与springboot,对外提供数据查询接口。
我的个人网站需要对mysql数据库内存储的京东商品进行模糊查询(模仿淘宝商品搜索),所以选择了将数据导入elasticsearch随后使用他来进行关键词查询。前端只需发送用户搜索的关键词和分页参数(可选),即可返回商品数据(json格式)
组件介绍:
本文测试环境:
sudo docker run -it --rm --name elasticsearch -d -p 9200:9200 -p 9300:9300 elasticsearch:2.3.5
注意到该命令:
得到如图:
此时打开网页localhost:9200即可查看状态,显示类似为:
{
"name" : "Ant-Man",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.3.5",
"build_hash" : "90f439ff60a3c0f497f91663701e64ccd01edbb4",
"build_timestamp" : "2016-07-27T10:36:52Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
注意:docker的es默认对0.0.0.0公网开放
本文中要导入的是pm_backend下的表pm_jd_item内的全部京东商品数据
详细步骤参考:
http://blog.codecp.org/2018/04/16/Elasticsearch%E4%B9%8B%E4%BD%BF%E7%94%A8Logstash%E5%AF%BC%E5%85%A5Mysql%E6%95%B0%E6%8D%AE/
最终编写的jdbc.conf为:
schedule => "* * * * *"
默认为每分钟同步一次
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/pm_backend"
jdbc_user => "root"
jdbc_password => "xxxxxxxxxx"
jdbc_driver_library => "xxxxxxxx/mysql-connector-java-5.1.6.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "5000"
statement=> "select * from pm_jd_item"
schedule => "* * * * *"
type => "pm_jd_item"
}
}
output {
elasticsearch {
hosts => "localhost:9200"
index => "pm_backend"
document_type => "%{type}"
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
在logstash目录下执行命令,完成数据的导入:
bin/logstash -f jdbc.conf
得到如图:
同步完成后,使用elasticsearch-head查看(或者用kibana,请随意):
org.elasticsearch
elasticsearch
2.4.6
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.data
spring-data-elasticsearch
# elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
#节点地址,多个节点用逗号隔开
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#spring.data.elasticsearch.local=false
spring.data.elasticsearch.repositories.enable=true
@Document(indexName = "pm_backend", type = "pm_jd_item")
public class JdItem implements Serializable {
@Id
private Integer id;
@Field(type = FieldType.Long)
private Long itemId;
@Field(type = FieldType.Long)
private Long categoryId;
@Field(type = FieldType.String)
private String name;
public interface JdItemRepository extends ElasticsearchRepository{
}
代码截取自个人项目京东价格监控,仅供参考!
/**
* 根据商品名在pm_jd_item中搜索商品
* @param itemName
* @param startRow
* @param pageSize
* @return
*/
@ApiOperation(value="查询商品", notes="查询商品")
@RequestMapping(value = "/findJdItemByName", method = {RequestMethod.GET})
public ResponseData<List<JdItem>> findJdItemByName(
@ApiParam("用户输入的商品名") @RequestParam(value = "itemName") String itemName,
@ApiParam("页码索引(默认为0)") @RequestParam(value = "startRow", required = false, defaultValue = "0") int startRow,
@ApiParam("每页的商品数量(默认为10)") @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
){
ResponseData<List<JdItem>> responseData = new ResponseData<>();
try {
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery().add(QueryBuilders.matchPhraseQuery("name", itemName), ScoreFunctionBuilders.weightFactorFunction(100)).scoreMode("sum").setMinScore(10);
Pageable pageable = new PageRequest(startRow, pageSize);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(functionScoreQueryBuilder).build();
Page<JdItem> jdItems = jdItemRepository.search(searchQuery);
// Page分页getTotalPages()返回了应有的页数,临时放在errorMsg传给前端
responseData.jsonFill(1, String.valueOf(jdItems.getTotalPages()), jdItems.getContent());
} catch (Exception e) {
e.printStackTrace();
responseData.jsonFill(2, e.getMessage(), null);
}
return responseData;
}
}
调用findJdItemByName接口,得到:
请参考:https://github.com/medcl/elasticsearch-analysis-ik
Docker安装ES & Kibana:
https://www.jianshu.com/p/fdfead5acc23
Elasticsearch之使用Logstash导入Mysql数据:
http://blog.codecp.org/2018/04/16/Elasticsearch%E4%B9%8B%E4%BD%BF%E7%94%A8Logstash%E5%AF%BC%E5%85%A5Mysql%E6%95%B0%E6%8D%AE/
我是蛮三刀把刀,后端开发。主要关注后端开发,数据安全,爬虫等方向。
来微信和我聊聊:yangzd1102
Github个人主页:
https://github.com/qqxx6661
同步更新公众号及以下全部博客:
1. Csdn
http://blog.csdn.net/qqxx6661
2. 知乎
https://www.zhihu.com/people/yang-zhen-dong-1/
3. 掘金
https://juejin.im/user/5b48015ce51d45191462ba55
4. 简书
https://www.jianshu.com/u/b5f225ca2376
如果文章对你有帮助,不妨收藏起来并转发给您的朋友们~