当前位置: 首页 > news >正文

ElasticSearch入门语法基础知识

1、创建测试索引

PUT /test_index_person
{"settings": {"analysis": {"analyzer": {"ik_analyzer": {"type": "custom","tokenizer": "ik_smart"}}}},"mappings": {"properties": {"person_id": { "type": "integer" },"person_name": { "type": "text","analyzer": "ik_analyzer"},"gmt_create": { "type": "date","format": "strict_date_optional_time"},"gmt_modified": { "type": "date","format": "strict_date_optional_time"}}}
}

2、添加数据

POST /test_index_person/_doc
{"person_id": 1,"person_name": "张三 李四","gmt_create": "2024-07-31T10:00:00","gmt_modified": "2024-07-31T10:00:00"
}POST /test_index_person/_doc
{"person_id": 2,"person_name": "王五 赵六","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 3,"person_name": "葛哈哈哈张三","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 4,"person_name": "葛哈哈哈小黑子","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 5,"person_name": "赵六不是小黑子","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 6,"person_name": "张三是一个小黑子","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}

3、修改数据

#更新数据,因为没有设置id,所以需要先查询到id
GET /test_index_person/_search
{"query": {"term": {"person_id": {"value": "1"}}}
}
POST /test_index_person/_update/VHvlB5EBxT0rb-io56C7
{"doc": {"person_name": "张三 李四哈哈哈"}
}

4、查询索引下的分词和ik分词器对搜索内容的分词

#查看test_index_person索引下对person_name某个内容的分词
GET /test_index_person/_analyze
{"field": "person_name","text": "张三是一个小黑子"
}
# IK分词器对搜索内容的分词
GET /_analyze
{"analyzer": "ik_max_word", "text": "张三"
}

5、查询所有文档

#匹配所有文档
GET /test_index_person/_search
{"query": {"match_all": {}}
}

6、分词查询测试

#查询分词测试
GET /test_index_person/_search
{"query": {"match": {"person_name": "葛赵六"}}
}

7、精确查询

#term查询适合用于匹配未分词的字段或者需要精确匹配的场景
GET /test_index_person/_search
{"query": {"term": {"person_id": {"value": "1"}}}
}

8、短语匹配

#短语匹配 返回所有person_name字段精确匹配短语"小黑子"的文档
GET /test_index_person/_search
{"query": {"match_phrase": {"person_name": "小黑子"}}
}

9、范围查询

#范围查询 gt(>) gte(>=)  lt(<) lte(<=) 
GET /test_index_person/_search
{"query": {"range": {"gmt_create": {"gte": "2024-01-31T11:00:00","lt": "2024-07-31T11:00:00"}}}
}

10、模糊匹配

#模糊匹配
GET /test_index_person/_search
{"query": {"fuzzy": {"person_name": "张三"}}
}

11、前缀匹配查询(适用非分词字段)

#前缀匹配查询(适用非分词字段)
GET /test_index_person/_search
{"query": {"prefix": {"person_name": {"value": "张三"}}}
}

12、布尔查询

#布尔查询 必须匹配(must) 过滤(filter) 至少匹配一个(should)
GET /test_index_person/_search
{"query": {"bool": {"must": [{"term": {"person_id": {"value": "1"}}}],"filter": [{"range": {"gmt_create": {"gte": "2024-07-31T10:00:00"}}}],"should": [{"term": {"person_name": {"value": "11"}}}],"must_not": [{"term": {"person_name": {"value": "哈哈"}}}]}}
}

13、分页

#分页 from开始 size 结束
GET /test_index_person/_search
{"query": {"match_all": {}},"from": 1, "size":  2
}

14、聚合查询

#对查询出来的值进行聚合查询 统计每个唯一值的个数
GET /test_index_person/_search
{"query": {"match": {"person_name": "张三"}}, "aggs": {"test_aggs": {"terms": {"field": "gmt_create"}}}
}
#对查询出来的值进行聚合查询 统计每个唯一值的个数  仅仅返回聚合结果
GET /test_index_person/_search
{"size": 0, "query": {"match": {"person_name": "张三"}}, "aggs": {"test_aggs": {"terms": {"field": "gmt_create"}}}
}
#多个聚合
GET /test_index_person/_search
{"aggs": {"test_aggs": {"terms": {"field": "gmt_create"}},"test2_aggs":{"avg": {"field": "person_id"}}}
}
#子聚合 先分组,再计算
GET /test_index_person/_search
{"aggs": {"test_aggs": {"terms": {"field": "gmt_create"}, "aggs": {"son_aggs_test": {"avg": {"field": "person_id"}}}}}
}
http://www.lryc.cn/news/411783.html

相关文章:

  • 【C++】C++应用案例-dolphin海豚记账本
  • Matlab数据处理学习笔记
  • 浏览器中的同源策略、CORS 以及相关的 Fetch API 使用
  • 爬虫 APP 逆向 ---> 粉笔考研
  • 2024河南萌新联赛第(三)场 河南大学
  • 回溯法---分割回文串
  • DDR等长,到底长度差多少叫等长?
  • 程序员面试题------N皇后问题算法实现
  • 【C++学习】6、继承
  • 从零开始的MicroPython(三) 按键与外部中断
  • Windows下编译安装Kratos
  • 汽车-腾讯2023笔试(codefun2000)
  • 软测面试二十问(最新面试)
  • 风吸杀虫灯采用新型技术 无公害诱虫捕虫
  • 随手记录第十二话 -- JDK8-21版本的新增特性记录(Lambda,var,switch,instanceof,record,virtual虚拟线程等)
  • SpringCloud网关 SpringBoot服务 HTTP/HTTPS路由/监听双支持
  • JavaScript做网页是否过期的处理
  • python coding时遇到的问题
  • 攻防演练号角吹响,聚铭铭察高级威胁检测系统助您零失分打赢重保攻坚战
  • 个人量化交易兴起!有什么好用的量化软件推荐?迅投QMT量化平台简介!
  • SQL labs-SQL注入(七,sqlmap对于post传参方式的注入,2)
  • SAM 2: Segment Anything in Images and Videos
  • 软件测试面试,如何自我介绍?
  • 力扣第四十七题——全排列II
  • Springer旗下中科院2区TOP,国人优势大!
  • 【C++】C++入门知识详解(下)
  • 分压电阻方式的ADC电压校准
  • 使用Postman测试API短轮询机制:深入指南
  • 明清进士人数数据
  • C# 串口通信(通过serialPort控件发送及接收数据)