ElasticSearch入门

一、倒排索引

将文档进行分词,形成词条和数据id的对应关系即为反向索引。

 二、ElasticSearch和Kibana安装

安装:略。

Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索、查看交互存储在Elasticsearch索引中的数据。使用Kibana,可以通过各种图表进行高级数据分析及展示。

三、ElasticSearch核心概念

索引(index)

ElasticSearch存储数据的地方,可以理解成关系型数据库中的数据库概念。

映射(mapping)

mapping定义了每个字段的类型、字段所使用的分词器等。相当于关系型数据库中的表结构。

文档(document)

Elasticsearch中的最小数据单元,常以json格式显示。一个document相当于关系型数据库中的一行数据。

四、操作索引

1、添加索引

put请求:http://ip:端口/索引名称

2、查询

get请求:

http://ip:端口/索引名称  # 查询单个索引信息
http://ip:端口/索引名称1,索引名称2...  # 查询多个索引信息
http://ip:端口/_all  # 查询所有索引信息

3、删除索引

delete请求:http://ip:端口/索引名称

4、关闭、打开索引

post请求:

http://ip:端口/索引名称/_close  
http://ip:端口/索引名称/_open 

五、操作映射

一、数据类型

1、简单数据类型

(1)字符串

text:会分词,不支持聚合(sum)

keyword:不会分词,将全部内容作为一个词条,支持聚合(sum)

(2)数值

(3)布尔:boolean

(4)二进制:binary

(5)范围类型

integer_range, float_range, long_range, double_range, date_range 

(6)日期:date

二、复杂数据类型

1、数组:[]

2、对象:{}

三、操作映射

(1)创建索引并添加映射
 PUT /person1
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      }
    }
  }
}

GET person1/_mapping

 (2)添加字段

PUT /person1/_mapping
{
  "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      }
    }
}

六、操作文档

1、添加文档,指定id

POST /person1/_doc/2
{
  "name":"张三",
  "age":18,
  "address":"北京"
}

2、添加文档,不指定id,会自动生成随机id

POST /person1/_doc/
{
  "name":"张三",
  "age":18,
  "address":"北京"
}

3、删除指定id文档

DELETE /person1/_doc/1

4、查询文档

#根据id查询文档      GET /person1/_doc/1

#查询所有文档         GET /person1/_search

七、查询文档

1、IK分词器

IK分词器有两种分词模式:ik_max_word(细粒度拆分)和 ik_smart模式(粗粒度拆分)。

2、查询文档

(1)词条查询:term

词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时才匹配搜索

GET /person2/_search
{
  "query": {
    "term": {
      "address": {
        "value": "北京"
      }
    }
  }
}

(2)全文查询:match

全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集

GET /person2/_search
{
  "query": {
    "match": {
      "address":"北京昌平"
    }
  }
}

八、ElasticSearch Java操作

一、Springboot整合ES

<!--引入es的坐标-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.4.0</version>
        </dependency>

二、添加索引,并添加映射;查询索引;删除索引

//添加索引
//1.使用client获取操作索引对象
IndicesClient indices = client.indices();
//2.具体操作获取返回值
//2.具体操作,获取返回值
CreateIndexRequest createIndexRequest = new CreateIndexRequest("itcast");
//2.1 设置mappings
String mapping = "{\n" +
                "      \"properties\" : {\n" +
                "        \"address\" : {\n" +
                "          \"type\" : \"text\",\n" +
                "          \"analyzer\" : \"ik_max_word\"\n" +
                "        },\n" +
                "        \"age\" : {\n" +
                "          \"type\" : \"long\"\n" +
                "        },\n" +
                "        \"name\" : {\n" +
                "          \"type\" : \"keyword\"\n" +
                "        }\n" +
                "      }\n" +
                "    }";
createIndexRequest.mapping(mapping,XContentType.JSON);

CreateIndexResponse createIndexResponse = indices.create(createIndexRequest,         
    RequestOptions.DEFAULT);
//3.根据返回值判断结果
System.out.println(createIndexResponse.isAcknowledged());

//查询索引
GetIndexRequest getRequest=new GetIndexRequest("itcast");
GetIndexResponse response = indices.get(getRequest, RequestOptions.DEFAULT);
Map<String, MappingMetaData> mappings = response.getMappings();
for (String key : mappings.keySet()) {
    System.out.println(key+"==="+mappings.get(key).getSourceAsMap());
}

//删除索引
DeleteIndexRequest deleteRequest=new DeleteIndexRequest("itcast");
        AcknowledgedResponse delete = indices.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());

//索引是否存在
boolean exists = indices.exists(getRequest, RequestOptions.DEFAULT);

 三、文档操作

//添加文档
Person person=new Person();
person.setId("2");
person.setName("李四");
person.setAge(20);
person.setAddress("北京三环");
String data = JSON.toJSONString(person);
IndexRequest request=new 
           IndexRequest("itcast").id(person.getId()).source(data,XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getId());

//修改文档
Person person=new Person();
person.setId("2");
person.setName("李四");
person.setAge(20);
person.setAddress("北京三环车王");

String data = JSON.toJSONString(person);

IndexRequest request=new 
           IndexRequest("itcast").id(person.getId()).source(data,XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getId());

//查询文档
//设置查询的索引、文档
GetRequest indexRequest=new GetRequest("itcast","2");
GetResponse response = client.get(indexRequest, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());

//删除文档
//设置要删除的索引、文档
DeleteRequest deleteRequest=new DeleteRequest("itcast","1");
DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(response.getId());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值