头图

注意 这些所以的东西版本都要一样

ElasticSearch安装

jdk至少1.8 ElasticSearch客户端,界面工具

注意

jdk必须与cpu的位数是一样的 否则会报JNA错误

下载地址

https://www.elastic.co/cn/ shearch和kibabn

https://github.com/medcl/elas... ik分词器

https://github.com/mobz/elast... head插件下载

这些都是解压可用

Elasticearch 启动

点击 bin\下的elasticsearch.bat

如果电脑内存不够大 启动前 记得去 config\jvm.options 修改

-Xms256m

   -Xmx256m

访问 http://127.0.0.1:9200/

head插件

必须要有 nodejs 因为他是前端写的

  1. cnpm install 安装依赖
  2. cnpm run start

注意如果9100端口被占用

netstat -ano | findstr "9100"

然后去任务管理器详细信息里去关

启动成功访问 http://localhost:9100/

9200 和 9100 是跨域的

需要在elasticearch.yaml中配置跨域

http.cors.enabled: true
http.cors.allow-origin: "*"

然后重启es服务

head就相当于navicat

kibana

这也是一个标准的前端化工程

bin\kibana.bat

http://localhost:5601

找到开发工具

image-20210815225652816

汉化!!!!

改配置kibana.yaml

i18n.locale: "zh-CN"

IK分词器(中文) elasticsreach默认的不是中文的

放在elasticsreach中的plugins中

有两种算法

  1. ik_smart 默认分词器 他所能理解 不重复的单词 或者字
  2. ik_max_word 最细粒度 穷进词库可能

    GET _analyze
    {
      "analyzer": "ik_smart",
      "text": "我超级喜欢多帅哦"
    }
    
    GET _analyze
    {
      "analyzer": "ik_max_word",
      "text": "我超级喜欢多帅哦"
    }

你自己需要的词,需要自己加到字典中

IKAnalyzer.cfg.xml

编写自己的kb.dic

然后在配置文件中添加

<entry key="ext_dict">kb.dic</entry>

重启es

Rest风格

image-20210815233809339

1、创建一个索引 随到在请求体中插入数据

PUT /索引名/类型名(可无)/文档id
{
    json请求体
}

image-20210816000106280

添加

创建一个索引,指定字段类型 但是没有插入信息

PUT /test2
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age": {
        "type": "long"
      },
      "birthday":{
        "type": "date"
      }
    }
  }
}

获得信息

GET test2

如果不写就是默认类型 _doc keyword是不可分割的

PUT /test3/_doc/1
{
  "name": "多帅哦",
  "age": 18,
  "birthday": "1999-10-20"
}
GET _cat/health   数据库状态
GET _cat/indices?v     数据库信息

修改

1、直接暴力PUT

PUT /test3/_doc/1{  "name": "多帅哦123",  "age": 18,  "birthday": "1999-10-20"}但是索引信息会改变   版本啊  状态啊

2、POST

POST /test3/_doc/1/_update{  "doc":{    "name":"shuaikb"  }}

3、删除索引

DELETE test1 根据你的请求是删除什么 索引或者文档

条件查询

GET /test3/_search?q=name:多帅哦/"_score" : 0.5753642,这个_score是匹配度   匹配度越高分数越高

复杂搜索

GET /test3/_search{  "query": {    "match": {      "name": "多帅"    }  }}name里包含 多帅 的都会出来"hits"里面包含了 索引的信息 和查询的结果GET /test3/_search{  "query": {    "match": {      "name": "多帅"    }  },  "_source": ["name","age"]}查询数据中只显示 name和age以后在java中操作es  所有的方法都是这里的key排序"sort": [    {      "age": {        "order": "desc"      }    }  ]通过什么字段进行排序   desc降序  asc升序分页"from": 0,  第几个数据开始"size": 1   返回多少条

布尔值查询

GET /test3/_search{  "query": {    "bool": {      "must": [        {          "match": {            "name": "多帅"          }        },        {          "match":{          "age": 18          }        }      ]    }  }}精确匹配  多条件查询   must   相当于 and  should 想当于 ornot    相当于  !=GET /test3/_search{  "query": {    "bool": {      "must": [        {          "match": {            "name": "多帅"          }        }      ],      "filter": [        {          "range": {            "age": {              "gte": 10,              "lte": 17            }          }        }      ]    }  }}filter 可以进行数据过滤 gt> gte>=    lt< lte<=  eq
GET /test3/_search{  "query": {    "match": {      "tag": "男 技术"    }  }}tag查询的多个属性 用空格隔开term,查询时通过倒排是索引进行精确查询match,会使用分词器解析 (先分析文档,然后通过分析文档进行查询!)

两个类型

  1. text 能被分词器解析
  2. keyword 不能被分词器解析
GET _analyze{  "analyzer": "standard",  "text": "多帅哦Shuaikb name1"}只要分词器不是keyword就不会被拆分GET testdb/_search{  "query": {    "term": {      "desc": {        "value": "多帅哦Shuaikb desc"      }    }  }}精确匹配         keyword类型的字段不会被分词器解析你会发现  多帅哦Shuaikb desc2不会被查询出来

多个值匹配的精确查询

GET testdb/_search{  "query": {    "bool": {      "should": [        {          "term": {            "t1": {              "value": "22"            }          }        },        {          "term": {            "t1": {              "value": "33"            }          }        }      ]    }  }}

高亮查询

GET testdb/_search{ "query": {   "match": {     "name": "帅"   } }, "highlight": {   "fields": {     "name": {}   } }}"多<em>帅</em>哦Shuaikb name2"这个<em>就是高亮的html自定义搜索高亮条件GET testdb/_search{ "query": {   "match": {     "name": "帅"   } }, "highlight": {   "pre_tags": "<p class='key' style='color:red'>",   "post_tags": "</p>",    "fields": {     "name": {}   } }}

Spirngboot集成es

原生依赖<repositories>    <repository>        <id>es-snapshots</id>        <name>elasticsearch snapshot repo</name>        <url>https://snapshots.elastic.co/maven/</url>    </repository></repositories>实际我们导入的依赖<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>注意依赖的版本必须要和自己的本地的es版本一致所以需要自定义版本依赖初始化RestHighLevelClient client = new RestHighLevelClient(        RestClient.builder(                new HttpHost("localhost", 9200, "http"),                new HttpHost("localhost", 9201, "http")));用完记得关client.close();@Bean    public RestHighLevelClient restHighLevelClient(){        RestHighLevelClient client =new RestHighLevelClient(                RestClient.builder(                        new HttpHost("localhost", 9200, "http"),                        new HttpHost("localhost", 9201, "http")));        return client;    }

API

索引操作

 @Test    public void CreateIndex() throws IOException {        //创建索引        CreateIndexRequest requst = new CreateIndexRequest("shuaikb_index");        //执行请求 获得响应        CreateIndexResponse createIndexResponse =                client.indices().create(requst, RequestOptions.DEFAULT);        System.out.println(createIndexResponse);    }    @Test    void testExisIndex() throws IOException {        //判断存在        GetIndexRequest request =new GetIndexRequest("shuaikb_index");        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);        System.out.println(exists);    }    @Test    void testDeleteIndex() throws IOException {        //删除索引        DeleteIndexRequest request =new DeleteIndexRequest("shuaikb_index");        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);        System.out.println(delete);    }

文档操作

@Test    void testAddDocument() throws IOException {        //添加文档        Dog dog = new Dog();        dog.setName("多帅哦");        dog.setAge(12);        IndexRequest requst = new IndexRequest("shuaikb_index");        //规则 PUT /shuaikb_index/_doc/1        requst.id("1");        requst.timeout(TimeValue.timeValueSeconds(1));        requst.timeout("1s");        //将我们的数据放入请求 json        requst.source(JSON.toJSONString(dog), XContentType.JSON);        //客户端发送请求        IndexResponse indexResponse = client.index(requst, RequestOptions.DEFAULT);        System.out.println(indexResponse.toString());        System.out.println(indexResponse.status());    }    @Test    void testExisDocument() throws IOException {        //判断文档存在        GetRequest getRequest = new GetRequest("shuaikb_index","1");        //不获取返回的_source的上下文        getRequest.fetchSourceContext(new FetchSourceContext(false));        getRequest.storedFields("_noe_");        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);        System.out.println(exists);    }    @Test    void testGetDocument() throws IOException {        //获取文档信息        GetRequest getRequest = new GetRequest("shuaikb_index","1");        GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);        System.out.println(documentFields.getSourceAsString());        System.out.println(documentFields);    }    @Test    void testUpdateDocument() throws IOException {        //更新文档信息        UpdateRequest updateRequest = new UpdateRequest("shuaikb_index","1");        updateRequest.timeout("1s");        Dog dog = new Dog("你怎么说",20);        updateRequest.doc(JSON.toJSONString(dog),XContentType.JSON);        client.update(updateRequest,RequestOptions.DEFAULT);    }    @Test    void testDeleteDocument() throws IOException {        //删除文档信息        DeleteRequest deleteRequest = new DeleteRequest("shuaikb_index","1");        deleteRequest.timeout("1s");        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);        System.out.println(delete);    }

大量数据操作

//大量数据操作    @Test    void testBulkRequest() throws IOException {        BulkRequest bulkRequest = new BulkRequest();        bulkRequest.timeout("10s");        ArrayList<Dog> dogArrayList = new ArrayList<Dog>();        dogArrayList.add(new Dog("test1",1));        dogArrayList.add(new Dog("test2",1));        dogArrayList.add(new Dog("test3",1));        dogArrayList.add(new Dog("test4",1));        dogArrayList.add(new Dog("shuaikb1",1));        dogArrayList.add(new Dog("shuaikb2",1));        dogArrayList.add(new Dog("shuaikb3",1));        dogArrayList.add(new Dog("shuaikb4",1));        for (int i = 0; i <dogArrayList.size() ; i++) {            bulkRequest.add(new IndexRequest("shuaikb_index")                    .id(""+(i+1))//不指定id就会随机生成一个复杂id                    .source(JSON.toJSONString(dogArrayList.get(i)),XContentType.JSON));        }        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);        System.out.println(bulk.hasFailures());//返回false代表成功    }    //查询    //searchRequest  搜索请求    //searchSourceBuilder  条件构造    // HighlightBuilder  构造高亮    //MatchAllQueryBuilders  全部查询    //TermQueryBuilder   精确查询    @Test    void testSearch() throws IOException {        SearchRequest searchRequest = new SearchRequest("shuaikb_index");        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();        //精确        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "test1");        //全部        //MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();        searchSourceBuilder.query(termQueryBuilder);        searchSourceBuilder.from();        searchSourceBuilder.size();        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));        searchRequest.source(searchSourceBuilder);        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);        System.out.println(JSON.toJSONString(search));        for (SearchHit hit : search.getHits().getHits()) {            System.out.println(hit.getSourceAsMap());        }    }

Shuaikb
1 声望0 粉丝