Commit f6e2f9eb by 刘栋

UDI-增加es

parent 5cc4692b
......@@ -8,12 +8,8 @@ import com.infynova.common.core.api.ResponseData;
import com.infynova.udi.dto.UdiListQry;
import com.infynova.udi.helper.UdiEasyExcelHelper;
import com.infynova.udi.service.UdiService;
import com.infynova.udi.service.helper.EsSearchHelper;
import com.infynova.udi.service.helper.RedisSearchHelper;
import com.infynova.udi.service.helper.RedisSearchIndexer;
import com.infynova.udi.vo.UdiExportVo;
import com.infynova.udi.vo.UdiVo;
import io.redisearch.SearchResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
......@@ -63,46 +59,4 @@ public class UdiController {
UdiEasyExcelHelper.export("udi查询导出列表", udiExportVos, UdiExportVo.class);
}
@Resource
private RedisSearchIndexer redisSearchIndexer;
@Resource
private RedisSearchHelper redisSearchHelper;
@Resource
private EsSearchHelper esSearchHelper;
@ApiOperationSupport(order = 200)
@ApiOperation(value = "createIndex", notes = "createIndex",hidden = true)
@PostMapping("createIndex")
public void createIndex(){
//redisSearchIndexer.createIndex();
esSearchHelper.createIndex();
}
@ApiOperationSupport(order = 200)
@ApiOperation(value = "deleteIndex", notes = "deleteIndex",hidden = true)
@PostMapping("deleteIndex")
public void deleteIndex(){
//redisSearchIndexer.deleteIndex();
esSearchHelper.deleteIndex();
}
@ApiOperationSupport(order = 200)
@ApiOperation(value = "initData", notes = "initData",hidden = true)
@PostMapping("initData")
public void initData(){
//redisSearchIndexer.initData();
esSearchHelper.initData();
}
@ApiOperationSupport(order = 200)
@ApiOperation(value = "searchThree", notes = "searchThree")
@PostMapping("searchThree")
public Object searchThree(@RequestParam(value = "companyName", required = false) String companyName,
@RequestParam(value = "productFactoryCode", required = false) String productFactoryCode){
//return redisSearchHelper.searchThree(companyName,productFactoryCode);
return esSearchHelper.searchThree(companyName,productFactoryCode);
}
}
package com.infynova.udi.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.infynova.udi.service.UdiService;
import com.infynova.udi.service.helper.EsSearchHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@Slf4j
@Api(tags = "udi-es")
@ApiSupport(order = 500)
@RestController
@RequestMapping("/es")
public class UdiEsController {
@Resource
private EsSearchHelper esSearchHelper;
@ApiOperationSupport(order = 100)
@ApiOperation(value = "createIndex", notes = "createIndex",hidden = true)
@PostMapping("createIndex")
public CreateIndexResponse createIndex(){
return esSearchHelper.createIndex();
}
@ApiOperationSupport(order = 200)
@ApiOperation(value = "deleteIndex", notes = "deleteIndex",hidden = true)
@PostMapping("deleteIndex")
public AcknowledgedResponse deleteIndex(){
return esSearchHelper.deleteIndex();
}
@ApiOperationSupport(order = 300)
@ApiOperation(value = "initData", notes = "initData",hidden = true)
@PostMapping("initData")
public void initData(){
esSearchHelper.initData();
}
@ApiOperationSupport(order = 400)
@ApiOperation(value = "searchThree", notes = "searchThree")
@PostMapping("searchThree")
public Object searchThree(@RequestParam(value = "companyName", required = false) String companyName,
@RequestParam(value = "productFactoryCode", required = false) String productFactoryCode){
return esSearchHelper.searchThree(companyName,productFactoryCode);
}
@ApiOperationSupport(order = 500)
@ApiOperation(value = "searchFour", notes = "searchFour")
@PostMapping("searchFour")
public Object searchFour(@RequestParam(value = "brandName", required = false) String brandName,
@RequestParam(value = "productName", required = false) String productName,
@RequestParam(value = "specification", required = false) String specification,
@RequestParam(value = "model", required = false) String model){
return esSearchHelper.searchFour(brandName,productName,specification,model);
}
}
......@@ -23,6 +23,8 @@ import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
......@@ -54,24 +56,62 @@ public class EsSearchHelper {
);
}
public void closeClient() throws IOException {
client.close();
}
@SneakyThrows
public void createIndex(){
public CreateIndexResponse createIndex(){
log.info("创建索引");
CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX_NAME);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
// 设置映射
createIndexRequest.mapping(
"{" +
" \"properties\": {" +
" \"id\": {" +
" \"type\": \"long\"" +
" }," +
" \"companyName\": {" +
" \"type\": \"text\"," +
" \"analyzer\": \"ik_max_word\"," +
" \"search_analyzer\": \"ik_smart\"" +
" }," +
" \"productFactoryCode\": {" +
" \"type\": \"text\"," +
" \"analyzer\": \"ik_max_word\"," +
" \"search_analyzer\": \"ik_smart\"" +
" }," +
" \"brandName\": {" +
" \"type\": \"text\"," +
" \"analyzer\": \"ik_max_word\"," +
" \"search_analyzer\": \"ik_smart\"" +
" }," +
" \"productName\": {" +
" \"type\": \"text\"," +
" \"analyzer\": \"ik_max_word\"," +
" \"search_analyzer\": \"ik_smart\"" +
" }," +
" \"specification\": {" +
" \"type\": \"text\"," +
" \"analyzer\": \"ik_max_word\"," +
" \"search_analyzer\": \"ik_smart\"" +
" }," +
" \"model\": {" +
" \"type\": \"text\"," +
" \"analyzer\": \"ik_max_word\"," +
" \"search_analyzer\": \"ik_smart\"" +
" }" +
" }" +
"}"
,XContentType.JSON
);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
return createIndexResponse;
}
@SneakyThrows
public void deleteIndex(){
public AcknowledgedResponse deleteIndex(){
log.info("删除索引");
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(INDEX_NAME);
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
return delete;
}
@Resource
......@@ -81,7 +121,7 @@ public class EsSearchHelper {
public void initData(){
log.info("同步数据");
StopWatch stopWatch = new StopWatch("开始");
Integer pageSize = 1000;
Integer pageSize = 5000;
Long lastId = 0L;
int i = 1;
try{
......@@ -92,7 +132,7 @@ public class EsSearchHelper {
if(CollectionUtils.isEmpty(records)){
return;
}
indexDocument(records);
this.indexDocument(records);
UdiVo udiVo = records.get(records.size()-1);
lastId = udiVo.getId();
i++;
......@@ -139,14 +179,6 @@ public class EsSearchHelper {
}
@SneakyThrows
public void updateIndex(String id, Map<String, Object> updatedDocument) throws IOException {
UpdateRequest request = new UpdateRequest(INDEX_NAME, id)
.doc(updatedDocument, XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
}
@SneakyThrows
public Map<String, Object> getId(String id){
GetRequest getRequest = new GetRequest(INDEX_NAME, id);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
......@@ -156,9 +188,6 @@ public class EsSearchHelper {
@SneakyThrows
public Object searchThree(String companyName,String productFactoryCode){
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
// BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
// .must(new MatchPhraseQueryBuilder("companyName", companyName))
// .must(new MatchPhraseQueryBuilder("productFactoryCode", productFactoryCode));
// 构建查询条件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.should(new MatchQueryBuilder("companyName", companyName))
......@@ -175,4 +204,28 @@ public class EsSearchHelper {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse;
}
@SneakyThrows
public Object searchFour(String brandName,String productName,String specification,String model){
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
// 构建查询条件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.should(new MatchQueryBuilder("brandName", brandName))
.should(new TermQueryBuilder("brandName.keyword", brandName))
.should(new MatchQueryBuilder("productName", productName))
.should(new TermQueryBuilder("productName.keyword", productName))
.should(new MatchQueryBuilder("specification", specification))
.should(new TermQueryBuilder("specification.keyword", specification))
.should(new MatchQueryBuilder("model", model))
.should(new TermQueryBuilder("model.keyword", model));
// 构建搜索源
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQueryBuilder);
// 设置搜索请求的源
searchRequest.source(searchSourceBuilder);
// 执行搜索
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse;
}
}
# Elasticsearch and Kibana Setup with IK Plugin
## 目录
1. [版本](#版本)
2. [Docker创建注意](#Docker创建注意)
3. [Linux创建注意](#Linux创建注意)
4. [检查服务](#检查服务)
5. [创建索引](#创建索引)
- [创建索引脚本](#创建索引脚本)
- [查询索引结构](#查询索引结构)
- [查询总数](#查询总数)
6. [查询语句](#查询语句)
- [查询CompanyName](#查询CompanyName)
- [查询ProductFactoryCode](#查询ProductFactoryCode)
- [规则三-转码查询](#规则三-转码查询)
- [规则四-转码查询](#规则四-转码查询)
7. [分词解析预测](#分词解析预测)
## 版本
- Elasticsearch: `7.12.1`
- Kibana: `7.12.1` (if needed)
- IK Plugin: `7.12.1`
-
## Docker创建注意
docker创建服务需要注意网关设置
```dockerfile
docker run -d --name es -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" -e "discovery.type=single-node" --privileged --net esnet -p 9200:9200 -p 9300:9300 elasticsearch:7.12.1
docker run -d --name kibana -e ELASTICSEARCH_HOSTS=http://es:9200 --net esnet -p 5601:5601 kibana:7.12.1
```
## Linux创建注意
linux创建服务需要注意
参考:[this guide](https://blog.csdn.net/m0_50287279/article/details/131819482).
## 检查服务
可以获取当前服务器是否创建好
```sh
curl 127.0.0.1:9200
```
linux安装要确认好是否提供外部服务
---
## 创建索引
### 创建索引脚本
```sh
PUT /udi/
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"companyName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"productFactoryCode": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"brandName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"productName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"specification": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"model": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
```
### 查询索引结构
```sh
GET /udi/_mapping
```
### 查询总数
```sh
GET /udi/_count
```
---
## 查询语句
### 查询CompanyName
```sh
POST /udi/_search
{
"query": {
"match": {
"companyName": ""
}
}
}
```
### 查询ProductFactoryCode
```sh
POST /udi/_search
{
"query": {
"match": {
"productFactoryCode": ""
}
}
}
```
### 规则三-转码查询
```sh
POST /udi/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"companyName": "大博医疗"
}
},
{
"term": {
"companyName": "大博医疗"
}
},
{
"match": {
"productFactoryCode": "09A221013"
}
},
{
"term": {
"productFactoryCode": "09A221013"
}
}
]
}
},
"_source": ["companyName","productFactoryCode"]
}
```
### 规则四-转码查询
```sh
POST /udi/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"productName": "/"
}
},
{
"match": {
"brandName": "大博医疗"
}
},
{
"match": {
"specification": "RHQ11"
}
},
{
"match": {
"model": "RHQ11"
}
}
]
}
},
"_source": ["productName","brandName","specification","model"]
}
```
### 分词解析预测
```sh
POST _analyze
{
"analyzer": "ik_smart",
"text": ["大博医疗"]
}
```
\ No newline at end of file
## 查询企业名称
POST /udi/_search
{
"query": {
"match": {
"companyName": ""
}
}
}
# 版本
# es版本: elasticsearch:7.12.1
# kibana版本: kibana:7.12.1 (如果需要)
# ik版本: 7.12.1
## 查询出厂编码
POST /udi/_search
{
"query": {
"match": {
"productFactoryCode": ""
}
}
}
# docker创建服务需要注意网关设置
# linux创建服务需要注意
# 参考:https://blog.csdn.net/m0_50287279/article/details/131819482
POST /udi/_search
{
"query": {
"multi_match": {
"query": "大博",
"minimum_should_match": "50%",
"fields": ["companyName","productFactoryCode"]
}
}
}
# 可以获取当前服务器是否创建好
curl 127.0.0.1:9200
# linux安装要确认好是否提供外部服务
######################################################################
# 创建索引
## 构建脚本
PUT /udi/
......@@ -71,6 +58,32 @@ PUT /udi/
}
}
## 查询索引结构
GET /udi/_mapping
################################################################################
## 查询企业名称
POST /udi/_search
{
"query": {
"match": {
"companyName": ""
}
}
}
## 查询出厂编码
POST /udi/_search
{
"query": {
"match": {
"productFactoryCode": ""
}
}
}
## 查询脚本
POST /udi/_search
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment