Custom dictionary controller

Start by creating a new controller to handle the add and remove dictionaries for Rest requests, as well as the query dictionaries.

package com.topcom.test.es.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/ * * *@Author BigKang
 * @Date 2021/1/28 1:58 下午
 * @MottoLaugh up to the sky, my generation is not the people *@SummarizeDictionary controller */
@RestController
public class DictController {

    /** * last modified time */
    private Date lastModified = new Date();

    /** * last modified time */
    private Date stopLastModified = new Date();

    /** ** ** /
    private List<String> dictTable = new ArrayList<>();

    /** ** */
    private List<String> stopDictTable = new ArrayList<>();


    /** * initializes the custom dictionary, which can also be read from the database and then loaded */
    @PostConstruct
    public void init(a) {
        dictTable.add("aaa");
        //dictTable. Add (" Galen great Sword ");

        stopDictTable.add("Guns");
        stopDictTable.add("Imitation gun");
    }

    /** * add dictionary *@paramDict dictionary *@paramStop Whether to stop the word *@return* /
    @PostMapping("addDict")
    public boolean addDict(String dict,Boolean stop) {
        // Check whether it exists and add it if it does not
        if(dict ! =null && dict.trim().length() > 0) {
            // Add words to the dictionary if they are not stopped
            if(stop == null| |! stop && ! dictTable.contains(dict)){ dictTable.add(dict); lastModified =new Date();
                return true;
            }else if(stop && ! stopDictTable.contains(dict)){ stopDictTable.add(dict); stopLastModified =new Date();
                return true; }}return false;
    }

    /** * delete the dictionary *@paramDict dictionary *@paramStop Whether to stop the word *@return* /
    @DeleteMapping("delDict")
    public boolean delDict(String dict,Boolean stop) {
        // Check whether it exists and add it if it does not
        if(dict ! =null && dict.trim().length() > 0) {
            // Add words to the dictionary if they are not stopped
            if(stop == null| |! stop && dictTable.contains(dict)){ dictTable.remove(dict); lastModified =new Date();
                return true;
            }else if(stop && stopDictTable.contains(dict)){
                stopDictTable.remove(dict);
                stopLastModified = new Date();
                return true; }}return false;
    }

    /** * get the dictionary *@param httpServletResponse
     */
    @GetMapping("getCustomDict")
    public void getCustomDict(HttpServletResponse httpServletResponse) throws IOException {
        getDict(null,httpServletResponse);
        httpServletResponse.flushBuffer();
    }

    /** * get the disabled dictionary *@param httpServletResponse
     */
    @GetMapping("getCustomStopDict")
    public void getCustomStopDict(HttpServletResponse httpServletResponse) throws IOException {
        getDict(true,httpServletResponse);
        httpServletResponse.flushBuffer();
    }

    /** * get the dictionary *@param stop
     * @param httpServletResponse
     */
    public void getDict(Boolean stop,HttpServletResponse httpServletResponse) {
        Long time = null;
        String eTag = null;
        List<String> dictTable = null;
        if(stop == null| |! stop){ time = lastModified.getTime(); dictTable =this.dictTable;
            eTag = String.valueOf(this.dictTable.hashCode()) + "-" +String.valueOf(this.dictTable.size());
        }else {
            time = stopLastModified.getTime();
            dictTable = this.stopDictTable;
            eTag = String.valueOf(this.stopDictTable.hashCode()) + "-" +String.valueOf(this.stopDictTable.size());
        }
        httpServletResponse.setDateHeader("Last-Modified",time);
        httpServletResponse.setHeader("ETag", eTag);
        httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        try(OutputStream outputStream = httpServletResponse.getOutputStream();) { StringBuilder str =new StringBuilder();
            for (String dict : dictTable) {
                str.append(dict);
                str.append("\n");
            }
            outputStream.write(str.toString().getBytes("utf-8"));
        } catch(IOException e) { e.printStackTrace(); }}}Copy the code

Corresponding interface address and description

Add dictionary: "APP address" /addDict? Dict = core &stop=true Delete dictionary: "APP address" /delDict? Dict = disallow &stop=false to get dictionary: "APP address" /getCustomDict to get disabled dictionary: The "APP address" /getCustomStopDict # parameter indicates the dictionary word stop # indicates whether to stop the word, the stop word is True, empty indicates the core dictionaryCopy the code

Then we go to the IK configuration to modify the configuration file and add two remote dictionaries


      
<! DOCTYPEproperties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>.<! Add the following -->
        <! -- Remote extension dictionary address -->
        <entry key="remote_ext_dict">http://124.71.9.101:8080/getCustomDict</entry>
        <! -- Remote disable dictionary address -->
        <entry key="remote_ext_stopwords">http://124.71.9.101:8080/getCustomStopDict</entry>
</properties>
Copy the code

Restart Es to view logs

[2021-01-28T09:48:40.407][INFO ][o.w.a.d.Monitor          ] [elasticsearch] try load config from /usr/share/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml
[2021-01-28T09:48:40.522][INFO ][o.w.a.d.Monitor          ] [elasticsearch] [Dict Loading] /usr/share/elasticsearch/config/analysis-ik/custom/mydict.dic
[2021-01-28T09:48:40.523][INFO ][o.w.a.d.Monitor          ] [elasticsearch] [Dict Loading] http:/ / 124.71.9.101:8080 / getCustomDict
[2021-01-28T09:48:40.531][INFO ][o.w.a.d.Monitor          ] [elasticsearch] aaa
[2021-01-28T09:48:40.532][INFO ][o.w.a.d.Monitor] [ElasticSearch] [2021-01-28T09:48:40.532][INFO ][o.w.a.d.Monitor          ] [elasticsearch] [Dict Loading] http:/ / 124.71.9.101:8080 / getCustomStopDict
[2021-01-28T09:48:40.541][INFO ][o.w.a.d.Monitor[elasticSearch] Gun [2021-01-28T09:48:40.542][INFO ][o.w.a.d.Monitor[ElasticSearch] [ElasticSearch]2021-01-28T09:48:40.542][INFO ][o.w.a.d.Monitor[elasticSearch] [elasticSearch]Copy the code

We added a stop word test to find that Es will refresh the dictionary. If there is no modification at an interval of about 1 minute, the dictionary will not be refreshed

[2021-01-28T09:59:40.238][INFO ][o.w.a.d.Monitor[elasticSearch] [elasticSearch] [2021-01-28T09:59:40.238][INFO ][o.w.a.d.Monitor          ] [elasticsearch] try load config from /usr/share/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml
[2021-01-28T09:59:40.341][INFO ][o.w.a.d.Monitor          ] [elasticsearch] [Dict Loading] /usr/share/elasticsearch/config/analysis-ik/custom/mydict.dic
[2021-01-28T09:59:40.342][INFO ][o.w.a.d.Monitor          ] [elasticsearch] [Dict Loading] http:/ / 124.71.9.101:8080 / getCustomDict
[2021-01-28T09:59:40.353][INFO ][o.w.a.d.Monitor          ] [elasticsearch] aaa
[2021-01-28T09:59:40.353][INFO ][o.w.a.d.Monitor] [ElasticSearch] [2021-01-28T09:59:40.353][INFO ][o.w.a.d.Monitor[elasticSearch] Galen sword [2021-01-28T09:59:40.354][INFO ][o.w.a.d.Monitor          ] [elasticsearch] [Dict Loading] http:/ / 124.71.9.101:8080 / getCustomStopDict
[2021-01-28T09:59:40.363][INFO ][o.w.a.d.Monitor[elasticSearch] Gun [2021-01-28T09:59:40.363][INFO ][o.w.a.d.Monitor[ElasticSearch] [ElasticSearch]2021-01-28T09:59:40.363][INFO ][o.w.a.d.Monitor[elasticSearch] [elasticSearch]Copy the code