dsl 呼叫elasticsearch7.8 、 bbossgroups外掛整合elasticsearch7.8,springboot整合elasticsearch7.8
阿新 • • 發佈:2020-12-11
技術標籤:javaelasticsearchelasticsearchjava
直接乾貨上原始碼
1、首先是pom 檔案
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.shamo.es</groupId> <artifactId>shamo-es</artifactId> <version>0.0.1-SNAPSHOT</version> <name>shamoes</name> <description>shamo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency> <dependency> <groupId>com.bbossgroups.plugins</groupId> <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId> <version>6.2.5</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!--匯入db-elasticsearch資料同步依賴包開始--> <dependency> <groupId>com.bbossgroups.plugins</groupId> <artifactId>bboss-elasticsearch-rest-jdbc</artifactId> <version>6.2.5</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> </plugins> </build> </project>
2、springboot 、es配置檔案
server.servlet.context-path=/ # 開發環境配置 server.port: 1800 # 日誌配置 logging.level.com.anqi.es=error logging.level.org.springframework=error logging.level.com.anqi.es.highclient=DEBUG logging.level.org.bboss=INFO logging.level.bboss=INFO #x-pack認證賬號和口令 elasticUser=elastic elasticPassword=changeme #es伺服器地址配置 elasticsearch.rest.hostNames=127.0.0.1:9200 #elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282 #動態索引表名稱日期格式配置 elasticsearch.dateFormat=yyyy.MM.dd elasticsearch.timeZone=Asia/Shanghai elasticsearch.ttl=2d #在控制檯輸出指令碼除錯開關showTemplate,false關閉,true開啟,同時log4j至少是info級別 elasticsearch.showTemplate=true #客戶端動態發現es叢集節點控制開關 elasticsearch.discoverHost=false #http連結池配置 http.timeoutConnection = 50000 http.timeoutSocket = 50000 http.connectionRequestTimeout=50000 http.retryTime = 1 http.maxLineLength = -1 http.maxHeaderCount = 200 http.maxTotal = 400 http.defaultMaxPerRoute = 200 # dsl配置檔案熱載入掃描時間間隔,毫秒為單位,預設5秒掃描一次,<= 0時關閉掃描機制 dslfile.refreshInterval = -1
2、es查詢語句放在esmapper/sj.xml下
<properties> <property name="searchDatas"> <![CDATA[ { "query": { "bool": { "should": [ { "match": { "name": #[keywords] } }, { "match": { "info.keyword": #[keywords] } } ] } } ,"size":1000, "highlight": { "pre_tags": [ "<mark>" ], "post_tags": [ "</mark>" ], "fields": { "*": {} }, "fragment_size": 2147483647 } } ]]> </property> </properties>
3、
EsService
@Service
public class EsService {
private Logger logger = LoggerFactory.getLogger(EsService.class);
@Autowired
private BBossESStarter bbossESStarter;
private String mappath = "esmapper/sj.xml";
public String saveDocEntity(String index, DocBean docBean){
ClientInterface clientUtil = bbossESStarter.getRestClient();
String response = clientUtil.addDocument( index , docBean,"refresh=true");
return response;
}
public DocSearchResult searchDoc(String docIndex, String keywords) {
List<DocBean> doclist = new ArrayList<>();
ClientInterface clientUtil = bbossESStarter.getConfigRestClient(mappath);
Map<String,Object> params = new HashMap<String,Object>();
params.put("keywords",keywords);
//Execute the query
ESDatas<DocESBaseData> esDatas =
clientUtil.searchList(docIndex+"/_search",
"searchDatas",
params,
DocESBaseData.class);
List<DocESBaseData> list = esDatas.getDatas();
if(list!=null){
for (DocESBaseData doc:list){
DocBean docBean = new DocBean();
String highlightname= doc.getHighlight().get("name").get(0).toString();
docBean.setId(doc.getId());
docBean.setName(doc.getName());
docBean.setSrc(doc.getSrc());
docBean.setUpdate_date(doc.getUpdate_date());
docBean.setHighlightname(highlightname);
doclist.add(docBean);
}
}
// String json = clientUtil.executeRequest("demo/_search",//demo as the index table, _search as the search action
// "searchDatas",//DSL statement name defined in esmapper/demo.xml
// params);//Query parameters
// String json = com.frameworkset.util.SimpleStringUtil.object2json(demos);
//Gets the total number of records
long totalSize = esDatas.getTotalSize();
DocSearchResult docSearchResult = new DocSearchResult();
docSearchResult.setDocBean(doclist);
docSearchResult.setTotalSize(totalSize);
return docSearchResult;
}
}
4、DocSearchResult
public class DocSearchResult {
private List<DocBean> docBean;
private long totalSize;
public List<DocBean> getDocBean() {
return docBean;
}
public void setDocBean(List<DocBean> docBean) {
this.docBean = docBean;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
}
5、DocESBaseData
public class DocESBaseData extends ESBaseData {
public String id;
public String src;
public String name;
public String info;
public String update_date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getUpdate_date() {
return update_date;
}
public void setUpdate_date(String update_date) {
this.update_date = update_date;
}
}
6、DocBean
public class DocBean {
public String id;
public String src;
public String name;
public String info;
public String update_date;
public String highlightname;
public String getName() {
return name;
}
public String getHighlightname() {
return highlightname;
}
public void setHighlightname(String highlightname) {
this.highlightname = highlightname;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getUpdate_date() {
return update_date;
}
public void setUpdate_date(String update_date) {
this.update_date = update_date;
}
}
7、DocSearchConfigController
@Controller
@RequestMapping(value = "/api/doc/config")
@PropertySource("classpath:application.properties")
public class DocSearchConfigController {
@Value("${doc.index}")
private String docIndex;
@Autowired
EsService esService;
@RequestMapping(value = "/get/detail")
@ResponseBody
public DocSearchResult godetail(@RequestParam("keywords") String keywords ){
DocSearchResult docSearchResult=null;
try{
if (keywords!=null&&keywords.length()>0 ){
docSearchResult= esService.searchDoc(this.docIndex,keywords);
}
} catch(Exception e){
e.printStackTrace();
}
return docSearchResult;
}