01:elasticsearch搜索引擎笔记一

结构化数据:数据库mysql
非结构化数据:比如音频,视频等(nosql,redis,mangdb)
半结构化数据:xml,html等(redis,mangdb)

  • 数据格式

Elasticsearcs是面向文档型数据库,一条数据在这里就是一个文档。
在这里插入图片描述
ES里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。
这里Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个
type Elasticsearch 7.X 中 , Type 的概念已经被删除了。

es基础操作各种请求发送url以及格式(postman)------------------------------------------------------------------------------------------------------------------------------------------

  • 创建索引

在Postman 中,向 ES 服务器发 PUT 请求 http://127.0.0.1:9200/shopping

在这里插入图片描述
在这里插入图片描述

  • 查看单个索引
    在这里插入图片描述
    在这里插入图片描述

  • 查看所有索引

http://127.0.0.1:9200/_cat/indices?v
这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES
服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下

在这里插入图片描述
在这里插入图片描述

  • 删除索引
    在这里插入图片描述

重新访问索引时,服务器返回响应:索引不存在

在这里插入图片描述

  • 创建文档

向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping /_doc

在这里插入图片描述
在这里插入图片描述

此处发送请求的方式必须为POST ,不能是 PUT ,否则会发生错误
在这里插入图片描述

如果想要自定义唯一性标识,需要在创建时指定http://127.0.0.1:9200/shopping/_doc/ 1001

在这里插入图片描述

此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为PUT

在这里插入图片描述

  • 查看文档

向 ES 服务器发 GET 请求 http://127.0.0.1:9200/sh opping /_doc/1

在这里插入图片描述
在这里插入图片描述

  • 查询全部文档

http://127.0.0.1:9200/shopping/_search

在这里插入图片描述

  • 全量修改文档

向 ES 服 务器发 POST 请求 http://127.0.0.1:9200/shopping /_doc/1

在这里插入图片描述
在这里插入图片描述

  • 局部修改字段

向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping/opping/_update/1

{
	"doc":{
	"address":"北京"
	}
}

在这里插入图片描述

  • 删除文档

向 ES 服务器发 DELETE 请求 http://127.0.0.1:9200/sh opping /_doc/1

在这里插入图片描述
在这里插入图片描述

如果删除一个并不存在的文档

在这里插入图片描述
在这里插入图片描述

  • 条件查询文档

http://127.0.0.1:9200/shopping/_search

{"query":{
    "match":{
        "address":"重庆"
    }
}
}

在这里插入图片描述

  • 条件全量查询

http://127.0.0.1:9200/shopping/_search

{"query":{
    "match_all":{
        
    }
}
}

在这里插入图片描述

  • 条件分页查询加排序

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "match_all":{
            
        }
},
    "from":5,
    "size":5,
    "_source":["address","age"],
    "sort":{
        "age":{
            "order":"asc"
        }
    }
}

在这里插入图片描述

  • 多个条件组合查询(and)

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "name":"冉述保"
                    }
                }
            ]
        }
    }
}

在这里插入图片描述

  • 多条件组合查询(or)

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"重庆"
                    }
                }
            ]
        }
    }
}

在这里插入图片描述

  • 多条件范围查询

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"重庆"
                    }
                }
            ],
            "filter":{
                "range":{
                    "age":{
                        "gt":26
                    }
                }
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述

  • 不分词,完全匹配查询且高亮显示

http://127.0.0.1:9200/shopping/_search

{
    "query":{
        "match_phrase":{
            "address":"北京"
        }
    },
    "highlight":{
        "fields":{
            "address":{}
        }
    }
}

在这里插入图片描述

  • 分组查询

http://127.0.0.1:9200/shopping/_search

{
    "aggs":{
        "age_group":{
            "terms":{
                "field":"age"
            }
        }
    },
    "size":0
}

在这里插入图片描述

  • 求平均值

http://127.0.0.1:9200/shopping/_search

{
    "aggs":{
        "age_avg":{
            "avg":{
                "field":"age"
            }
        }
    },
    "size":0
}

在这里插入图片描述

  • 新增映射关系

http://127.0.0.1:9200/user/_mapping

{
    "properties":{
        "name":{
            "type":"text",
            "index":true
        },
        "sex":{
            "type":"keyword",
            "index":true
        },
        "phone":{
            "type":"keyword",
            "index":false
        }
    }
}

在这里插入图片描述

  • 查询映射关系

http://127.0.0.1:9200/user/_mapping

在这里插入图片描述

  • 查询text(分词)-keyword(不分词,必须精准匹配)

http://127.0.0.1:9200/user/_search

{"query":{
    "match":{
        "name":"张"
    }
}
}

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

{"query":{
    "match":{
        "sex":"男"
    }
}
}

无结果

在这里插入图片描述

JavaAPI操作es(基于代码)---------------------------------------------------------------------------------------------------------------------

  • 准备工作- maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>web_demo</artifactId>
        <groupId>com.zgs.test</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>es-test</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!--elasticsearch的客户端-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!--elasticsearch 依赖 2.x 的 log4j-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9</version>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

</project>
  • 新建索引
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月01日 18:01:00
 */
@Slf4j
public class EsTest_Index_Create {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //创建索引
        CreateIndexRequest request = new CreateIndexRequest("zgs1");
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        log.info("-----------"+response.toString());
        boolean acknowledged = response.isAcknowledged();
        System.out.println("acknowledged-------"+acknowledged);
        client.close();
    }
}

  • 查询索引
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 15:32:00
 */
@Slf4j
public class EsTest_Index_Search {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("localhost",9200,"http")));

        //查询索引
        GetIndexRequest request = new GetIndexRequest("zgs");
        GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
        log.info("#########################"+response.getAliases());
        log.info("#########################"+response.getDataStreams());
        log.info("#########################"+response.getMappings());
        log.info("#########################"+response.getSettings());
        client.close();
    }
}

  • 删除索引
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 15:41:00
 */
@Slf4j
public class EsTest_Index_Delete {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("localhost",9200,"http"))
        );
        //删除索引

        DeleteIndexRequest request = new DeleteIndexRequest("zgs");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        log.info("#########################"+response.isAcknowledged());
        client.close();
    }
}

  • 新增文本信息
package com.zgs.es.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 16:24:00
 */
@Slf4j
public class EsTest_Doc_Insert {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        IndexRequest request = new IndexRequest();
        request.index("zgs").id("1001");

        ZgsVo zgsVo = new ZgsVo();
        zgsVo.setName("张贵松");
        zgsVo.setAge(24);
        zgsVo.setSex("男");
        //转换为json
        ObjectMapper mappers = new ObjectMapper();
        String zgsJson = mappers.writeValueAsString(zgsVo);
        request.source(zgsJson, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        log.info("########################"+response.getResult());
        client.close();
    }
}

  • 修改文本信息
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 17:28:00
 */
@Slf4j
public class EsTest_Doc_Update {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        UpdateRequest request = new UpdateRequest();
        request.index("zgs").id("1001");
        request.doc(XContentType.JSON,"name","张帅");
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        log.info("###########################"+response.getGetResult());
        client.close();
    }
}

  • 查询文本信息
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 17:38:00
 */
@Slf4j
public class EsTest_Doc_Get {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        GetRequest request = new GetRequest();
        request.index("zgs").id("1001");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        log.info("###########################"+response.getSourceAsString());
        client.close();
    }
}

  • 删除文本信息
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 17:40:00
 */
@Slf4j
public class EsTest_Doc_Delete {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        DeleteRequest request = new DeleteRequest();
        request.index("zgs").id("1001");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        log.info("###########################"+response.toString());
        client.close();
    }
}

  • 批量新增数据
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 17:55:00
 */
@Slf4j
public class EsTest_Doc_Insert_Batch {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new IndexRequest().index("zgs").id("1001").source(XContentType.JSON,"name","张帅1"));
        bulkRequest.add(new IndexRequest().index("zgs").id("1002").source(XContentType.JSON,"name","张帅2"));
        bulkRequest.add(new IndexRequest().index("zgs").id("1003").source(XContentType.JSON,"name","张帅3"));
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("#########################"+response.getIngestTook());
        log.info("#########################"+response.getItems());
        client.close();
    }
}

  • 批量删除数据
package com.zgs.es.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @author zgs
 * @date 2021年12月02日 18:08:00
 */
@Slf4j
public class EsTest_Doc_Delete_Batch {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new DeleteRequest().index("zgs").id("1002"));
        bulkRequest.add(new DeleteRequest().index("zgs").id("1003"));
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("#########################"+response.getIngestTook());
        log.info("#########################"+response.getItems());
        client.close();
    }
}
  • 30
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值