首页 > 其他分享 >Elasticsearch 基础入门

Elasticsearch 基础入门

时间:2024-10-12 19:36:36浏览次数:1  
标签:入门 基础 application Elasticsearch https curl pretty Type 9200

查询集群文档数量

查询集群文档数量

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/_count?pretty'

curl -i 显示响应头信息

curl -XGET -i -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/_count?pretty'

示例数据

员工数据

  • 结构化查询
  • 全文搜索、短语搜索
  • 高亮显示
  • 数据分析
curl -XPUT -i -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_bulk?pretty' -d'
{ "index" : { "_id" : "1" } }
{ "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] }
{ "index" : { "_id" : "2" } }
{ "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] }
{ "index" : { "_id" : "3" } }
{ "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] }
'

检索数据

  • POST 创建文档
  • PUT 替换文档
  • GET 查询文档
  • DELETE 删除文档
  • HEAD 检查文档是否存在
curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_doc/1?pretty'

简单搜索

查询所有数据,默认返回 10 条

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty'

搜索 last_name 为 Smith 的文档

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty&q=last_name:Smith'

精确匹配

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
    "query": {
        "constant_score": {
            "filter": {
                "term": {"last_name.keyword": "Smith"}
            }
        }
    }
}
'

全文搜索

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
    "query": {
        "match": {
            "last_name": "Smith"
        }
    }
}
'

复杂查询

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
    "query": {
        "bool": {
            "must": {
                "match": {"last_name": "smith"}
            },
            "filter": {
                "range": {"age": {"gt": 30}}
            }
        }
    }
}
'

短语搜索

结果必须包含 "rock climbing"

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
    "query": {
        "match_phrase": {
            "about": "rock climbing"
        }
    }
}
'

高亮搜索

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
    "query": {
        "match_phrase": {
            "about": "rock climbing"
        }
    },
    "highlight": {
        "fields": {
            "about": {}
        }
    }
}
'

聚合

根据 interests 分组统计,all_interests 是别名

https://stackoverflow.com/questions/59298209/how-to-fix-elasticsearch-fielddata-is-disabled-on-text-fields-by-default-for-k

curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "interests.keyword"
            }
        }
    }
}'

先进行搜索,在对结果根据 interests 分组统计

curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
    "query": {
        "match": {
            "last_name": "Smith"
        }
    },
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "interests.keyword"
            }
        }
    }
}'

根据 interests 分组,再计算每组的平均年龄

curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "interests.keyword"
            },
            "aggs": {
                "avg_age": {
                    "avg": {"field": "age"}
                }
            }
        }

    }
}'

标签:入门,基础,application,Elasticsearch,https,curl,pretty,Type,9200
From: https://www.cnblogs.com/liaozibo/p/18461188

相关文章

  • 2024-2025-1 20241411《计算机基础与程序设计》第三周学习总结
    |这个作业属于哪个课程|<班级的链接>(2024-2025-1-计算机基础与程序设计](https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP))||-- |-- ||这个作业要求在哪里|<作业要求的链接>((https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP/homework/13276))||这个作业的......
  • python统计人的视角(1)——python基础
    3D画图importmatplotlibimportmatplotlib.pyplotaspltimportnumpyasnpfig=plt.figure()ax=fig.add_subplot(111,projection="3d")x=np.arange(-10,10,0.5)y=np.arange(-10,10,0.5)X,Y=np.meshgrid(x,y)Z=X**2+Y**2ax.plot_wireframe(X,Y,......
  • SpringCloud Alibaba-01 入门简介
    1.SpringCloudAlibaba是由阿里巴巴结合自身丰富的微服务实践而推出的微服务开发的一站式解决方案。它是SpringCloud生态中的第二代实现,提供了包括服务注册与发现、分布式配置管理、服务限流降级、消息驱动能力、阿里云对象存储、分布式任务调度等在内的多种功能。1.1......
  • 黑客入门【五大浏览器四大内核】
    一、五大浏览器‌五大主流浏览器分别是‌GoogleChrome、‌MozillaFirefox、‌MicrosoftEdge、Safari和‌Opera。‌‌ 1、IE浏览器IE是微软公司旗下浏览器,是目前国内用户量最多的浏览器。IE诞生于1994年,当时微软为了对抗市场份额占据将近百分之九十的网景NetscapeNavig......
  • 2024-2025-1 20241401 《计算机基础与程序设计》 第三周学习总结
    班级的链接2024计算机基础与程序设计作业要求第三周作业作业目标1、数字分类与计数法位置计数法,2、进制转换,3、模拟数据与数字数据,4、压缩与解压,5、数字化,6、信息安全作业正文本博客教材学习内容总结《计算机科学概论》第二章、第三章二进制数值计数系......
  • Elasticsearch 安装
    Elasticsearch权威指南启动dockerubuntudockerrun-it-d-p22:22--nameubuntu_devubuntudockerexec-itubuntu_devbash安装工具aptupdateaptinstall-ycurlvimsudoadduseropenssh-server重置ubuntu用户密码passwdubuntu切换ubuntu用户(ES不能......
  • C/C++语言基础--C++神奇的多态
    本专栏目的更新C/C++的基础语法,包括C++的一些新特性前言通过前面几节课,我们学习了抽象、封装、继承相关的概念,接下来我们将讲解多态,多态他非常神奇,正式有了他,类才能出现多样性特征;C语言后面也会继续更新知识点,如内联汇编;欢迎收藏+关注,本人将会持续更新。文章目录......
  • 高频SQL50题(基础版)
    前言:本篇文章主要是更新有关查询的基础SQL题,后续会持续更新连接,聚合函数等类型的sql题目录1757.可回收且低脂的产品(简单)584.寻找用户推荐人(简单)595.大的国家(简单)1148.文章浏览I(简单)1683.无效的推文(简单)1757.可回收且低脂的产品(简单)表:Products+-------------+-------......
  • 2024版最新AI大模型知识点大梳理,零基础入门到精通,收藏这篇就够了
    文章目录AI大模型是什么AI大模型发展历程AI大模型的底层原理AI大模型解决的问题大模型的优点和不足影响个人观点AI大模型是什么AI大模型是指具有巨大参数量的深度学习模型,通常包含数十亿甚至数万亿个参数。这些模型可以通过学习大量的数据来提高预测能力,从而在自然语言......
  • QD1-P21-P22 CSS 基础语法、注释、使用方法
    本节学习:CSS基础语法和注释,以及如何使用CSS定义的样式。本节视频https://www.bilibili.com/video/BV1n64y1U7oj?p=21CSS基本语法CSS(层叠样式表)的基本语法相对简单,由选择器和一组包含在花括号{}​中的声明组成。​​组成部分:选择器选择器用于指定你想要样式化......