下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
注意:版本号要与Elasticsearch的版本一致,例如我的是
在这里插入图片描述
就下载
在这里插入图片描述
下载之后在elasticsearch的plugins中创建一个叫ik的文件夹,然后将下载的ik压缩包解压出来并全部复制到ik文件夹中,我的路径是这样
在这里插入图片描述
放进去之后启动elasticsearch,得到如下信息则启动成功
在这里插入图片描述
在这里插入图片描述
每个人的消息位置可能不能,自行细心查看,然后我们启动kibana,我启动kibana的时候报了一个错:Request Timeout after 30000ms,说我连接超时,当时我正开着idea,所以特别吃内存,解决办法有两种:
1.:关闭idea然后重新启动kibana
2.在elasticsearch–>config–>jvm.options中配置(多一点内存)

-Xms10g
-Xmx10g

也就是配置文件的
在这里插入图片描述
重新启动elasticsearch加kibana,启动成功后进入http://localhost:5601,在Kibana中测试ik

GET _analyze?pretty 
{
    "analyzer":"ik_max_word",
    "text":"中国人民警察的服务宗旨"
}

结果:在这里插入图片描述

接下来我们再测试一个,创建索引,添加值

PUT /iktest
{
  "mappings": {
    "properties": {
      "name":{
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word", 
        "type": "text"
      }
    }
  }
}

POST /iktest/_doc
{
  "name":"你是大懒猪"
}

进行查询

GET _analyze?pretty 
{
    "analyzer":"ik_max_word",
    "text":"你是大懒猪"
}

结果:
在这里插入图片描述

我们来查询值为你:

GET /iktest/_search
{
  "query": {
    "match": {
      "name": "你"
    }
  }
}

在这里插入图片描述
查询值为猪

GET /iktest/_search
{
  "query": {
    "match": {
      "name": "猪"
    }
  }
}

在这里插入图片描述
我们得出结果:分词查询时并不一定这个值中包含这个字就得出结果,而是根据自己特定的规则去查询得出相应的值。

接下来在演示在Java中使用,由于之前一直学的springcloud的,就在原有的项目上演示:
在项目提供者项目中导入jar包:

<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.6.2</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.6.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.67</version>
            <scope>compile</scope>
        </dependency>

配置类:

package com.jbit.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class ESUtil {

    private RestHighLevelClient client;

    /**
     * 根据索引名以及ID查询数据
     * @param index 索引名称
     * @param id    数据ID
     * @return
     */
    public String getDocument(String index, String id){
        GetResponse getResponse = null;
        try{
            client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http")));

            GetRequest getRequest = new GetRequest(index,"_doc", id );
            getResponse = client.get(getRequest, RequestOptions.DEFAULT);
            System.out.println(JSONObject.toJSONString(getResponse));
            client.close();
        }catch(IOException e){
            e.printStackTrace();
        }
        return JSONObject.toJSONString(getResponse);
    }

    /**
     * 进行全文检索
     * @param index
     * @param value
     * @param current
     * @param size
     * @return
     */
    public String keywordSearch(String index, String value,
                                int current, int size){
        List<Map<String, Object>> result = null;
        try{
            client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices(index);

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //支持全词搜索的字段有:keywordName,keywordAuthor"
            searchSourceBuilder.query(QueryBuilders
                    .multiMatchQuery(value, "name"));
            searchSourceBuilder.from(current);
            searchSourceBuilder.size(size);

            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println(JSONObject.toJSONString(searchResponse));
            //处理返回结果
            result = dealResult(searchResponse.getHits());
            client.close();
        }catch(IOException e){
            e.printStackTrace();
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * 分页查询索引所有数据
     * @param index
     * @param current
     * @param size
     * @return
     * @throws IOException
     */
    public String searchAll(String index, int current, int size) throws IOException {
        client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.from(current);
        searchSourceBuilder.size(size);

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSONObject.toJSONString(searchResponse));
        //处理返回结果
        SearchHits hits = searchResponse.getHits();
        client.close();
        return JSONObject.toJSONString(hits);
    }

    private List<Map<String, Object>> dealResult(SearchHits hits){
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (SearchHit hit : hits.getHits()) {
            Map<String, Object> map = hit.getSourceAsMap();
            result.add(map);
        }
        return result;
    }

}

Service:

public String es(String index,String str);

ServiceImpl:

@Autowired
    private ESUtil esUtil;

    @RequestMapping("es")
    public String es(@RequestParam("index") String index,@RequestParam("str") String str){
        esUtil.keywordSearch(index,str,0,10);
        return "success";
    }

消费者项目的Service中:

@RequestMapping("es")
    public String es(@RequestParam("index") String index,@RequestParam("str") String str);

controller层:

@RequestMapping("es")
    public String es(String index,String str){
        System.out.println("es");
        return orderService.es(index,str);
    }

依次启动服务进行访问,我的项目访问的路径为:http://localhost:8080/order/es?index=iktest&str=弟弟

index:访问的索引
str:搜索访问的值

我访问后的结果如下:
请求中:
在这里插入图片描述
后台输出:

{"clusters":{"fragment":true,"skipped":0,"successful":0,"total":0},"failedShards":0,"fragment":false,"hits":{"fragment":true,"hits":[{"fields":{},"fragment":false,"highlightFields":{},"id":"-JyjBnIBYcY3zijdoTJ8","matchedQueries":[],"primaryTerm":0,"rawSortValues":[],"score":1.3034774,"seqNo":-2,"sortValues":[],"sourceAsMap":{"name":"臭弟弟"},"sourceAsString":"{\"name\":\"臭弟弟\"}","sourceRef":{"fragment":true},"type":"_doc","version":-1},{"fields":{},"fragment":false,"highlightFields":{},"id":"-ZyjBnIBYcY3zijdxDJ1","matchedQueries":[],"primaryTerm":0,"rawSortValues":[],"score":1.3034774,"seqNo":-2,"sortValues":[],"sourceAsMap":{"name":"香弟弟"},"sourceAsString":"{\"name\":\"香弟弟\"}","sourceRef":{"fragment":true},"type":"_doc","version":-1}],"maxScore":1.3034774,"totalHits":{"relation":"EQUAL_TO","value":2}},"numReducePhases":1,"profileResults":{},"shardFailures":[],"skippedShards":0,"successfulShards":1,"timedOut":false,"took":{"days":0,"daysFrac":1.0416666666666667E-7,"hours":0,"hoursFrac":2.5E-6,"micros":9000,"microsFrac":9000.0,"millis":9,"millisFrac":9.0,"minutes":0,"minutesFrac":1.5E-4,"nanos":9000000,"seconds":0,"secondsFrac":0.009,"stringRep":"9ms"},"totalShards":1}

带弟弟的都搜索出来了

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐