Elasticsearch的基本功能和使用

这篇具有很好参考价值的文章主要介绍了Elasticsearch的基本功能和使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Elasticsearch ,简称为 ES,是一款非常强大的开源的高扩展的分布式全文 检索引擎,可以帮助我们从海量数据中快速找到需要的内容,它可以近乎实时的 存储、检索数据.还可以可以实现日志统计、分析、系统监控等功能.
官网:https://www.elastic.co/cn
例如京东,淘宝,头条等站内搜索功能.
 ES环境搭建
ES 下载地址: https://www.elastic.co/cn/downloads/elasticsearch 默认打开是最新版本 7.6.1 版下载
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.1-windows-x86_64.zip解压
在bin目录中双击elasticsearch.bat启动
Elasticsearch的基本功能和使用,JAVA进阶,elasticsearch,大数据,搜索引擎
访问 http://127.0.0.1:9200
安装数据可视化界面 elasticsearch head
安装 nodejs
github 下载: https://github.com/mobz/elasticsearch-head/
github 加速器: https://github.ur1.fun/
在 config 目录中的 elasticsearch.yml 文件中配置
# 开启跨域
http.cors.enabled: true
# 所有人访问
http.cors.allow-origin: "*"
命令行进入目录(第一次安装即可)
npm install
启动可视化界面 (需要启动ES才可启动可视化界面)
npm run start
Elasticsearch的基本功能和使用,JAVA进阶,elasticsearch,大数据,搜索引擎
安装可视化 kibana 组件 
Kibana 是一个针对 Elasticsearch 的开源分析及可视化平台,用来搜索、查看交互 存储在 Elasticsearch 索引中的数据。
使用 Kibana,可以通过各种图表进行高级数据分析及展示。Kibana 让海量数据更 容易理解。
下载版本要和 ES 版本一致
下载地址 : https://www.elastic.co/cn/downloads/kibana 默认打开是最新版本 7.6.1 下载版
https://artifacts.elastic.co/downloads/kibana/kibana-7.6.1-windows-x86_64.zip
汉化 kibana
修改 config 目录下的 kibana.yml 文件 i18n.locale: "zh-CN"
双击 bin 目录下的 kibana.bat 启动
Elasticsearch的基本功能和使用,JAVA进阶,elasticsearch,大数据,搜索引擎

访问http://localhost:5601 

显示界面

Elasticsearch的基本功能和使用,JAVA进阶,elasticsearch,大数据,搜索引擎

SpringBoot使用ES搜索

配置类

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}

使用

import com.fasterxml.jackson.databind.ObjectMapper;

import com.ffyc.news.config.ElasticSearchConfig;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.io.IOException;


@RestController
@RequestMapping(path = "/es")
public class EsController {
    @Autowired
    ElasticSearchConfig elasticSearchConfig;
    @GetMapping(path = "/indexOper")
    public String indexOper() throws IOException {
        //创建索引库
        CreateIndexRequest request = new CreateIndexRequest("users_index");
        elasticSearchConfig.restHighLevelClient().indices().create(request, RequestOptions.DEFAULT);
        return "success";
        //删除库
//        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("users");
//        AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//        System.out.println(delete.isAcknowledged());

//        判断库为例
//        GetIndexRequest getIndexRequest = new GetIndexRequest("users");
//        boolean users =  restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
//        System.out.println(users);

    }

    /*@GetMapping(path = "/docOper")
    public String docOper() throws IOException {
        //模拟从前端传递过来的数据
        News news = new News();
        news.setId(1);
        news.setTitle("美国今年要总统选择,拜登着急了");
        news.setImg("aaaaasssss.jpg");
        news.setContent("少时诵诗书所所所所所所所所所所所所所所所");
        news.setCount(300);

        //把News中的数据封装到NewsES
        NewsES newsES = new NewsES();
        //对象数据复制
        BeanUtils.copyProperties(news,newsES);
        //创建请求对象
        IndexRequest indexRequest  = new IndexRequest("news").id(newsES.getId().toString());
        indexRequest.source(new ObjectMapper().writeValueAsString(newsES), XContentType.JSON);
        //执行
        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        return "success";
    }*/

   /* @GetMapping(path = "/docOper")
    public String docOper() throws IOException {
        //模拟从前端传递过来的数据
        News news = new News();
        news.setId(1);
        news.setTitle("中国航母开往美国,准备开战,拜登着急了");
        news.setImg("ddddddddddddddddddddd.jpg");
        news.setContent("少时诵诗书所所所所所所所所所所所所所所所");
        news.setCount(30000);

        //把News中的数据封装到NewsES
        NewsES newsES = new NewsES();
        //对象数据复制
        BeanUtils.copyProperties(news,newsES);
        //创建一个修改的请求
        UpdateRequest updateRequest = new UpdateRequest("news",newsES.getId().toString());
        updateRequest.doc(new ObjectMapper().writeValueAsString(newsES), XContentType.JSON);

        restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
        return "success";
    }*/

  /*  @GetMapping(path = "/docOper")
    public String docOper() throws IOException {
        //创建一个修改的请求
        DeleteRequest deleteIndexRequest = new DeleteRequest("news","1");
        restHighLevelClient.delete(deleteIndexRequest,RequestOptions.DEFAULT);
        return "success";
    }*/

  /*  @GetMapping(path = "/docOper")
    public String docOper() throws IOException {
         //创建一个搜索的请求
        SearchRequest searchRequest = new SearchRequest("news");
                     //封装搜索条件
                     searchRequest.source().query(QueryBuilders.termQuery("title","免费"));
         //发送搜索请求, 接收搜索的结果
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        ArrayList<NewsES> arrayList  = new ArrayList<>();
        SearchHits hits = search.getHits();
        SearchHit[] hits1 = hits.getHits();
        for (SearchHit searchHit : hits1){
             String json =    searchHit.getSourceAsString();
             NewsES newsES = new ObjectMapper().readValue(json,NewsES.class);
            arrayList.add(newsES);
        }



        return "success";
    }*/

//    @GetMapping(path = "/docOper")
//    public String docOper() throws IOException {
//        //创建一个搜索的请求
//        SearchRequest searchRequest = new SearchRequest("news");
//        //封装搜索条件
//        //设置关键字 高亮显示
//        searchRequest.source().highlighter(new HighlightBuilder().field("title").requireFieldMatch(false));
//        searchRequest.source().query(QueryBuilders.termQuery("title","免费"));
//        //发送搜索请求, 接收搜索的结果
//        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//
//        ArrayList<NewsES> arrayList  = new ArrayList<>();
//        SearchHits hits = search.getHits();
//        SearchHit[] hits1 = hits.getHits();
//        for (SearchHit searchHit : hits1){
//            String json =    searchHit.getSourceAsString();
//            NewsES newsES = new ObjectMapper().readValue(json,NewsES.class);
//
//            //获取高亮名称
//            Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
//            HighlightField titleField = highlightFields.get("title");
//            String highttitle = titleField.getFragments()[0].string();
//            newsES.setTitle(highttitle);//用添加了高亮的标题替换原始文档标题内容
//            arrayList.add(newsES);
//        }
//
//
//
//        return "success";
//    }
}

 文章来源地址https://www.toymoban.com/news/detail-797127.html

到了这里,关于Elasticsearch的基本功能和使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ElasticSearch基本使用--ElasticSearch文章一

    https://www.elastic.co/cn/ 1、在当前软件行业中,搜索是一个软件系统或平台的基本功能, 学习ElasticSearch就可以为相应的软件打造出良好的搜索体验。 2、其次,ElasticSearch具备非常强的大数据分析能力。虽然Hadoop也可以做大数据分析,但是ElasticSearch的分析能力非常高,具备Hadoo

    2024年02月14日
    浏览(25)
  • ElasticSearch Java API 基本操作

    ElasticSearch Java API是ES官方在8.x版本推出的新java api,也可以适用于7.17.x版本的es。 本文主要参考了相关博文,自己手动编写了下相关操作代码,包括更新mappings等操作的java代码。 代码示例已上传github。 elasticsearch 版本: 7.17.9 ,修改 /elasticsearch-7.17.9/config/elasticsearch.yml ,新增

    2024年02月08日
    浏览(33)
  • Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了。在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题。看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得都不是很全面。因此就有了写ElasticSearch开发教程的想法,将学习到的技术经验分享出来,

    2024年02月05日
    浏览(34)
  • ElasticSearch进阶:一文全览各种ES查询在Java中的实现

    ElasticSearch进阶:一文全览各种ES查询在Java中的实现 es基本语句详解 查询语句详解 ElasticSearch第一篇: ElasticSearch基础:从倒排索引说起,快速认知ES 完整项目已上传至:ElasticSearch Demo 项目,该项目是关于springboot的集成项目,ElasticSearch部分请关注【elasticSearch-demo】模块。觉得

    2024年02月02日
    浏览(40)
  • ElasticSearch进阶:多种查询操作,各种ES查询以及在Java中的实现

    目录 前言 1 词条查询 1.1 等值查询-term 1.2 多值查询-terms 1.3 范围查询-range 1.4 前缀查询-prefix 1.5 通配符查询-wildcard 2 复合查询 2.1 布尔查询 2.2 Filter查询 3 聚合查询 3.1 最值、平均值、求和 3.2 去重查询 3.3 分组聚合 3.3.1 单条件分组 3.3.2 多条件分组 3.4 过滤聚合 ElasticSearch 第一篇

    2024年02月02日
    浏览(34)
  • Elasticsearch基本使用

    Elaticsearch 简称为 ES ,是一个开源的可扩展的分布式的全文检索引擎,它可以近乎实时的存储、检索数据。本身扩展性很好,可扩展到上百台服务器,处理PB级别的数据。 本文基于7.x版本,总结 ES 常用的基本操作。 相关链接: 官网 es版本与jvm版本 下载地址 Rest风格API Java客户

    2024年01月23日
    浏览(50)
  • ELasticsearch基本使用——基础篇

    1.1.1.elasticsearch的作用 elasticsearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容 例如: 在GitHub搜索代码 在电商网站搜索商品 在谷歌搜索答案 在打车软件搜索附近的车 1.1.2.ELK技术栈 elasticsearch结合kibana、Logstash、Beats,

    2024年02月02日
    浏览(27)
  • Elasticsearch(三)聚合基本使用

    基础概念 bucket 数据分组,一些数据按照某个字段进行bucket划分,这个字段值相同的数据放到一个bucket中。可以理解成Java中的MapString, List结构,类似于Mysql中的group by后的查询结果。 metric: 对一个数据分组执行的统计,比如计算最大值,最小值,平均值等 类似于Mysql中的max

    2024年02月09日
    浏览(24)
  • ElasticSearch 基本使用

    系统环境 ElasticSearch:elasticsearch:7.14.1 kibana:7.14.1 服务器:Ubuntu 16.04.6 LTS Elasticsearch 是一个 分布式 的、开源的 搜索分析引擎 ,支持各种数据类型,包括文本、数字、地理、结构化、非结构化 Elastic Search基于lucene, 封装 了许多 lucene 底层功能,提供了分布式的服务、简单易用

    2023年04月15日
    浏览(22)
  • elasticSearch数据存储与搜索基本原理

    为啥想学习es,主要是在工作中会用到,但是因为不了解原理,所以用起来畏手畏脚的,就想了解下es是怎么存储数据,以及es是怎么搜索数据的,我们平时应该如何使用es,以及使用时候需要注意的方面。 es:https://github.com/elastic/elasticsearch lucene:https://github.com/apache/lucene.git es是

    2024年02月16日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包