Zinsearch implements website content search

As an alternative to elasticsearch - zincsearch , its biggest advantage is its light weight. It is implemented by the Go language, which inherently has the advantages of taking up less resources and starting quickly. For some small sites, if you don't want to use the fuzzy query of the database as the search method of the website, then zincsearch might be a better choice.

Next, introduce the installation and use of zinsearch.

Install

Official website: Installation - "ZincSearch Chinese Documentation - Help Manual - Tutorial" - Geekdaxue.co

Zincsearch official website has an installation tutorial, it is recommended to use Docker for installation, which can save a lot of time in configuring the environment.

The official website does not use the docker-compose installation tutorial, so here is my docker-compose.yml file.

version: '3.5'

services:
  zinc: ## mqtt 服务
    image: public.ecr.aws/zinclabs/zinc:latest
    environment:
      - ZINC_FIRST_ADMIN_USER=admin
      - ZINC_FIRST_ADMIN_PASSWORD=admin
    ports:
      - "4080:4080"
    volumes:
      - ./data:/data
    restart: always

By default, the latest zinc image is pulled, and the default account password is admin.

use

Zincsearch is compatible with some APIs of elasticsearch. If you have used it before, you will get started with zincsearch very quickly.

First you need to create an index. index is equivalent to mysql database. It is recommended to call the api to create an index, and write it directly in the code, or you have to create it manually every time.

Because I just started using it and don't know much about it, I will briefly introduce the insertion and search functions used by the website .

insert

Zinsearch has its own swagger page, you can find the implementation interface of related functions.

For example, the insert function can use the **/api/{index}/_doc/{id}** interface. Specify the id to know which one is being operated when updating the document, so if you want to insert a document with an id equal to 1, you can do the following.

The following is an example of inserting a document using java encapsulation call.

public void insertData(ZincInsertDto zincInsertDto){
    
    
    String uri = zincProperties.getIp()+"/api/"+zincProperties.getIndex()+"/_doc/"+zincInsertDto.getId();
    String auth = "Basic "+ Base64Util.base64Encode(zincProperties.getUsername()+":"+zincProperties.getPassword());
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Authorization",auth);
    Map<String,String> map = new HashMap<>();
    map.put("title",zincInsertDto.getTitle());
    map.put("content",zincInsertDto.getContent());
    HttpEntity<Object> httpEntity = new HttpEntity<Object>(map,httpHeaders);
    restTemplate.put(uri,httpEntity);
}

In the above example, index has two additional attributes title and content , both of which are passed through dto. At the same time, authentication is required when requesting, that is, the content of httpHeaders .

Inquire

It is recommended to use the **/es/{index}/_search** interface for the zinsearch query function.

Because there are too many conditions that can be configured when querying, I will not introduce them one by one here, but briefly introduce a few commonly used parameters.

{
    
    
    "from": 0,
    "size": 20,
    "query":{
    
    
        "multi_match":{
    
    
            "query":"测试",
            "fields":["title","content"]
        }
    },
    "highlight": {
    
    
        "fields": {
    
    
            "title":{
    
    },
            "content":{
    
    }
        }
    }
}

from : Which one to start querying from.

size : The maximum number of query results to return.

query : query condition, where multi_match means that it can be queried from multiple attributes; the index in the example has two attribute values ​​of title and content, that is, the query condition is to query all documents containing tests in the title and content.

highlight : Indicates the returned document, in which the matched words will be highlighted.

The figure below is an example of a java-encapsulated query request.

public ResponseDto getSearchResult(@RequestBody String condition){
    
    
    String query = "{\n" +
    "    \"from\": 0,\n" +
    "    \"size\": 20,\n" +
    "    \"query\":{\n" +
    "        \"multi_match\":{\n" +
    "            \"query\":\""+condition+"\",\n" +
    "            \"fields\":[\"title\",\"content\"]\n" +
    "        }\n" +
    "    },\n" +
    "    \"highlight\": {\n" +
    "        \"fields\": {\n" +
    "            \"title\":{},\n" +
    "            \"content\":{}\n" +
    "        }\n" +
    "    }\n" +
    "}";
    Map map = JSON.parseObject(query,new TypeReference<Map>(){
    
    });
    return ResponseDto.Ok(zincUtil.getSearchResult(map));
}
public Object getSearchResult(Map query){
    
    
    String uri = zincProperties.getIp()+"/es/"+zincProperties.getIndex()+"/_search";
    String auth = "Basic "+ Base64Util.base64Encode(zincProperties.getUsername()+":"+zincProperties.getPassword());
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Authorization",auth);
    HttpEntity<Object> httpEntity = new HttpEntity<Object>(query,httpHeaders);
    Object responseDto = restTemplate.postForObject(uri,httpEntity, Object.class);
    return responseDto;
}

Similarly, identity authentication is required when adjusting the interface, just encapsulate it and pass it to httpEntity .

Original link: https://xiaoliu.life/p/20230518a

Guess you like

Origin blog.csdn.net/weixin_46630782/article/details/130756808