Make writing a habit together! This is the sixth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

preface

In the 5th day of the activity, we have completed the IK segmentation search using Kibana to call ES query in the common e-shopping scene. Today, we will integrate yesterday’s practices into the Spring Boot project

Define the highlight identifier

When we want to achieve search results highlighting, we first need to attach a fixed identifier to the corresponding results, so that it can be identified when rendering to highlight. We first anchor a highlighted response body, with the corresponding interface defined as follows

@RestController @RequestMapping("es/search") public class EsSearchController { @GetMapping("html") public void html(HttpServletResponse response) throws IOException { response.setHeader("Content-Type", "text/html; charset=utf-8"); PrintWriter writer = response.getWriter(); Writer. Print (" < HTML > < body > "+" color is < span class = 'red' > red < / span > "+" < / body > "+" < style >. Red {color: red} < / style > "+ "</html>"); writer.flush(); writer.close(); }}Copy the code

We start the project (if you don’t have a project to build, you can download my project: gitee.com/liangminghu… Access IP :port/es/search/ HTML to display the following effect

How do I set the highlight in ES

To set the response highlighting in ES, we need to use the highlight tag. We search for “Huawei Mobile 001 as an example” in ascending order by sort default, and we request the following in Kibana

Post goods / _search {" query ": {" bool" : {" should ": [{" match" : {" name ":" huawei mobile phone 001 "}}, {" match ": {" keyWords" : "Huawei mobile phone 001"}}}}], "highlight" : {" pre_tags ": [" < span class = 'red' >"], "post_tags:" [] "< / span >", "fields" : {" name ": {}, "keyWords": {} } }, "from": 0, "size": 2, "sort": { "sort": "asc" } }Copy the code

Tags such as Query were explained in the previous article, but here we’ll focus on the tags in Highlight

  • Pre_tags +post_tags: These two tags define what tag to enclose the split result in, similar to our front end <>
  • Fields: Defines the attributes to be highlighted for the search, with name for the name and keyWords for the keyword

Note: we set the index to ik when creating the index. If you create the index by default, you will use es notation

Integrate our search using Spring Boot

At first I thought it would be difficult to integrate ES search to IK, but I found out that the 8.1.2 client supports jSON-based wrapping of request bodies. The following code ideas come from the official documentation: es official documentation, which should be the easiest way to integrate ES search

@GetMapping("doSearch") public void doSearch(HttpServletResponse response, @RequestParam(value = "word") String word, @RequestParam(value = "sort", defaultValue = "sort") String sort) throws IOException { if (StringUtils.isBlank(word)) { return; } response.setHeader("Content-Type", "text/html; charset=utf-8"); RestClient = restClient. builder(new HttpHost("127.0.0.1", 9200, "HTTP ")).build(); Request Request = new Request("POST", "goods/_search"); / / based on json structure query request. SetJsonEntity (" {\ "query \" : {\ "bool \" : {\ "should \" : "+" [{\ "match \" : {\ "name \" : \ "" + +" \ "word}}," + "{\"match\":{\"keyWords\":\"" + word + "\"}}]}}," + "\"highlight\":{\"pre_tags\":[\"<span class='red'>\"]" + ",\"post_tags\":[\"</span>\"],\"fields\":{\"name\":{}," + "\"keyWords\":{}}},\"from\":0,\"size\":5,\"sort\":{\"sort\":\"asc\"}}"); Response esRes = restClient.performRequest(request); log.info("{}", esRes); RequestLine requestLine = esRes.getRequestLine(); HttpHost host = esRes.getHost(); int statusCode = esRes.getStatusLine().getStatusCode(); Header[] headers = esRes.getHeaders(); String responseBody = EntityUtils.toString(esRes.getEntity()); ##### log.info("{}{}{}{}{}", requestLine, host, statusCode, headers, responseBody); restClient.close(); PrintWriter writer = response.getWriter(); writer.print( "<html><body>" + responseBody + "</body>" + "<style> .red{color:red }</style>" + "</html>"); writer.flush(); writer.close(); }Copy the code

Note that the client needs to be closed. We access the following addresses and get the following results

  1. Search huawei mobile phone 001: http://127.0.0.1:4001/es/search/doSearch? Word = Huawei mobile 001

You can see that both name and keyWords are matched.Kirin http://127.0.0.1:4001/es/search/doSearch?word=

As you can see, only our keywords were hit

To sum up, we have completed the spring Boot integrated ES8.1.2IK segmentation highlighting search. We spent 6 days on the exploration of ES, and we hope you can learn something in these 6 days

Conclusion 7

As of today, our es related articles have come to an end for the time being. Starting with the next article, I will choose one direction from big data or taro multifaceted development practices. Welcome to give your support! \