栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch基础知识

ElasticSearch基础知识

ElasticSearch基础知识

文章目录
  • ElasticSearch基础知识
    • es是什么
    • 启动es
    • 常见问题
    • 客户端安装
    • 数据格式
    • 索引操作
    • 索引查询和删除
    • 文档创建
      • 数据的修改
        • 全量修改
        • 局部修改
        • 删除操作
    • 查询操作
      • 条件查询
      • 分页查询
      • 查询排序
    • 多条件查询
    • 全文检索、完全匹配、高亮查询
      • 全文检索
      • 完全匹配
      • 高亮查询
    • 聚合查询
    • 映射关系
      • 创建映射
      • 体现映射效果
    • java api操作es
      • 环境准备
      • 创建连接
      • 创建索引
      • 索引查询
      • 索引删除
      • 索引添加数据
      • 索引修改
      • 查询某条数据
      • 删除某条数据
      • 批量新增
      • 批量删除

学习地址:尚硅谷
看到第24集

es是什么

启动es


访问

常见问题

1.ElasticsearchException[X-Pack is not supported and Machine Learning is not available for

解决:在config/elasticsearch.yml添加一条配置:

xpack.ml.enabled: false
客户端安装

数据格式

倒排索引和正排索引

索引操作


索引创建过程
PUT请求具有密等性

索引查询和删除

查询

查看创建的所有索引的信息
访问地址:

http://localhost:9200/_cat/indices?v



删除索引

文档创建

创建文档

创建自己的文档id

查询索引中指定id的数据

查询索引下的所有数据

数据的修改

测试数据

{
    "title":"es的学习",
    "name":"hjx",
    "time":"2021-12-23"
}
全量修改

局部修改

删除操作

查询操作 条件查询
http://localhost:9200/shopping/_search?q=price:100


上面这种查询方式不方便(因为查询条件是在url中的,下面这种将查询条件放入请求体中)

使用请求体全部查询

分页查询

from:表示当前页数据的起始位置
size:每页查询的数据条数

显示指定字段的数据

查询排序

多条件查询



注意:这里的must相当于mysql中的and 连接符

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "price":23
                    }
                },
                {
                    "match":{
                        "title":"JSP入门"
                    }
                },
            ]
        }
    }
}

或查询

范围查询

全文检索、完全匹配、高亮查询 全文检索

注意:这种查询方式会将含有其中一个词的数据的每条数据都查询出来

完全匹配

高亮查询

聚合查询

查询平均值

映射关系 创建映射


体现映射效果

java api操作es 环境准备

        
        
            org.elasticsearch
            elasticsearch
            7.11.2
        
        
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.11.2
        


        
        
            org.apache.logging.log4j
            log4j-api
            2.10.0
        


        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.9.3
        


        
            junit
            junit
            4.12
            test
        
    
创建连接
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
		client.close();
创建索引
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
//		创建索引
		CreateIndexRequest request=new CreateIndexRequest("fruit"); // 索引名称
		CreateIndexResponse response =client.indices().create(request, RequestOptions.DEFAULT);

//		响应状态
		boolean acknowledged=response.isAcknowledged();
		System.out.println("索引操作:"+acknowledged);
		client.close();
索引查询
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
//		查询索引
		GetIndexRequest request=new GetIndexRequest("fruit");
		GetIndexResponse response=client.indices().get(request, RequestOptions.DEFAULT);
//		响应状态
		System.out.println(response.getAliases());
		System.out.println(response.getMappings());
		System.out.println(response.getSettings());
索引删除
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
//		查询索引
		DeleteIndexRequest request=new DeleteIndexRequest("fruit");

		AcknowledgedResponse response=client.indices().delete(request, RequestOptions.DEFAULT);
//		响应状态
		System.out.println(response.isAcknowledged());入代码片
索引添加数据
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
//		插入数据
		IndexRequest request=new IndexRequest();
		request.index("fruit").id("101");

//		设置对象数据
		Fruit fruit=new Fruit();
//		fruit.setId();
		fruit.setName("苹果");
		fruit.setPrice(5.5);
		fruit.setColor("yellow");
//		client.index(request, RequestOptions.DEFAULT);

//		注意:将数据插入之前,必须转换为json
		ObjectMapper objectMapper=new ObjectMapper();
		String fruit_json=objectMapper.writevalueAsString(fruit);
		request.source(fruit_json, XContentType.JSON);

		IndexResponse response=client.index(request,RequestOptions.DEFAULT);
		System.out.println(response.getResult());
//		关闭资源
		client.close();
索引修改

局部数据修改

//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
//		修改数据
		UpdateRequest request=new UpdateRequest();
		request.index("fruit").id("101");
		request.doc(XContentType.JSON,"name","橘子");
		UpdateResponse response=client.update(request, RequestOptions.DEFAULT);
		System.out.println("响应状态:"+response.getResult());
查询某条数据
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
		GetRequest request=new GetRequest();
		request.index("fruit").id("101");
		GetResponse response=client.get(request,RequestOptions.DEFAULT);
		System.out.println(response.getSourceAsString());
删除某条数据
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
		DeleteRequest request=new DeleteRequest();
		request.index("fruit").id("101");

		DeleteResponse response=client.delete(request, RequestOptions.DEFAULT);
		System.out.println(response);
		//		关闭资源
		client.close();
批量新增
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
		BulkRequest request=new BulkRequest();

		Fruit fruit1=new Fruit();
		fruit1.setName("黄瓜");
		fruit1.setPrice(5);
		fruit1.setColor("green");

		Fruit fruit2=new Fruit();
		fruit2.setName("西红柿");
		fruit2.setPrice(3);
		fruit2.setColor("red");

		Fruit fruit3=new Fruit();
		fruit3.setName("橙子");
		fruit3.setPrice(8.7);
		fruit3.setColor("yellow");


		request.add(new IndexRequest().index("fruit").id("101").source(XContentType.JSON,"name","西红柿","price",3, "color","red"));
		request.add(new IndexRequest().index("fruit").id("102").source(XContentType.JSON,"name","黄瓜","price",5, "color","green"));
		request.add(new IndexRequest().index("fruit").id("103").source(XContentType.JSON,"name","橙子","price",8.7, "color","yellow"));

		BulkResponse response=client.bulk(request, RequestOptions.DEFAULT);
		System.out.println(response.getTook());
		System.out.println(response.getItems());
		client.close();
批量删除
//		创建es客户端
		RestHighLevelClient client=new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost",9200,"http"))
		);
		BulkRequest request=new BulkRequest();

//		request.add(new DeleteRequest().index("fruit").id("101"));
		request.add(new DeleteRequest().index("fruit").id("102"));
		request.add(new DeleteRequest().index("fruit").id("103"));

		BulkResponse response=client.bulk(request, RequestOptions.DEFAULT);
		System.out.println(response.getTook());
		System.out.println(response.getItems());
		client.close();

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677022.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号