ElasticSearch入门学习笔记
ElasticSearch入门笔记
分页查询
-
from: 开始位置
-
size: 查多少条
GET /credit_enterprise_info/_search
{
"query": {
"match": {
"qymc": "大"
}
}
, "from": 0
, "size": 5
}
解决数据量很大时 总数只显示10000条
GET /credit_enterprise_info/_search
{
"track_total_hits": true
}
如修改完之后,通过api查询回来的totalhits还是只有10000条,解决如下:
在查询时候把 track_total_hits 设置为 true。
track_total_hits 设置为false禁用跟踪匹配查询的总点击次数
设置为true就会返回真实的命中条数。
GET 索引名/_search
{
"query": {
"match_all": {}
},
"track_total_hits":true
}
java代码在构建条件时候加上:
searchSourceBuilder.trackTotalHits(true);
只查询索引内文档数量
GET /credit_enterprise_info/_count
设置查询10000条以后的数据
PUT /credit_enterprise_info/_settings
{
"index.max_result_window" : "1000000"
}
查询配置
GET /credit_enterprise_info/_settings
返回:
{
"credit_enterprise_info" : {
"settings" : {
"index" : {
"number_of_shards" : "5",
"provided_name" : "credit_enterprise_info",
"max_result_window" : "1000000000",
"creation_date" : "1630920063923",
"number_of_replicas" : "1",
"uuid" : "CXgroui1SyqmWNDlg2-ifQ",
"version" : {
"created" : "7020099"
}
}
}
}
}
精确查询!
term查询是直接通过倒排索引指定的词条进行精确查找的!
- 通过倒排索引
关于分词
-
trem,直接查询精确的(倒排索引直接查询)
-
match,会使用分词器解析!(先分析文档,然后在通过分析的文档进行查询!)
两个类型 text keyword
keyword字段类型不会被分词器解析
text类型可以被解析
PUT /testdb/_doc/1
{
"name": "Java name",
"desc": "Java desc"
}
PUT /testdb/_doc/2
{
"name": "Java name",
"desc": "Java desc2"
}
GET _analyze
{
"analyzer": "keyword",
"text": "Java name"
}
GET _analyze
{
"analyzer": "standard",
"text": "Java name"
}
GET /testdb/_search
{
"query": {
"term": {
"name": "测试"
}
}
}
GET /testdb/_search
{
"query": {
"term": {
"desc": "测试"
}
}
}
GET /testdb/_search
{
"query": {
"term": {
"desc": "Java desc"
}
}
}
多个值匹配的精确查询
PUT /testdb/_doc/3
{
"t1": "22",
"t2": "2020-4-6"
}
PUT /testdb/_doc/4
{
"t1": "33",
"t2": "2020-4-7"
}
GET /testdb/_search
{
"query": {
"bool": {
"should": [
{"term": {
"t1": "22"
}},
{"term": {
"t1": "33"
}}
]
}
}
}
must (and),所有条件都要符合 where id = 1 and name = XXX
must not(not)
should(or),所有条件都要符合 where id = 1 or name = XXX
filter
GET /testdb/_search
{
"query": {
"bool": {
"should": [
{"term": {
"t1": "22"
}},
{"term": {
"t1": "33"
}}
]
, "filter": {
"range": {
"t1": {
"gte": 20,
"lte": 30
}
}
}
}
}
}
- gt 大于
- gte 大于等于
- lt 小于
- lte 小于等于!
匹配多个条件
直接分词 空格
GET /credit_enterprise_info/_search
{
"query": {
"match": {
"qymc": "沈阳 齐齐哈尔"
}
}
}
高亮查询
GET /credit_enterprise_info/_search
{
"query": {
"match": {
"qymc": "沈阳 齐齐哈尔"
}
}
, "highlight": {
"fields": {
"qymc": {}
}
}
}
自定义高亮查询
GET /credit_enterprise_info/_search
{
"query": {
"match": {
"qymc": "沈阳 齐齐哈尔"
}
}
, "highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"qymc": {}
}
}
}
GET /jd_goods/_search
{
"from": 1,
"size": 20,
"timeout": "20s",
"query": {
"term": {
"title": {
"value": "java",
"boost": 1.0
}
}
},
"highlight": {
"pre_tags": ["<sapn style='color:red'>"],
"post_tags": ["</span>"],
"require_field_match": false,
"fields": {
"title": {}
}
}
}
模糊查询 wildcard
GET /credit_enterprise_info/_search
{
"from": 0,
"size": 20,
"timeout": "20s",
"query": {
"bool": {
"must": [{
"wildcard": {
"tyshxydm": "*925309*"
}
}],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"_source": {
"includes": ["inserttime", "qymc", "tyshxydm"],
"excludes": []
},
"sort": [{
"_score": {
"order": "desc"
}
}]
}
这些其实mysql也能做,知识mysql的效率比较低
- 匹配
- 按照条件匹配
- 精确匹配
- 区间范围匹配
- 匹配字段过滤
- 多条件查询
- 高亮查询
索引相关
# 查看全部索引
GET _cat/indices
# 获取一个文档
GET /index/type/id
# 删除索引
DELETE /index
# 查看mapping
GET /index/_mapping
# 创建索引mapping
PUT /index
{
"mappings": {
"type": {
"properties": {
"id": {
"type": "integer"
},
"industry": {
"type": "text",
"index": false
},
"report_type": {
"type": "text",
"index": false
},
"title": {
"type": "text",
"index":true
},
"update_time": {
"type": "date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"url": {
"type": "text",
"index": false
}
}
}
}
}
说明
ignore_malformed:true 忽略格式错误的数值
# 部分更新
POST /index/type/id/_update
{
"doc": {
"update_time": "2019-11-13 12:12:03"
}
}
# 查询,并过滤没有删除,分页,时间排序
get /index/_search
{
"query": {
"bool": {
"filter": {
"bool": {
"must_not": {
"term": {
"is_del": 1
}
}
}
},
"must": {
"match_phrase": {
"title": "国"
}
}
}
},
"size": 10,
"from": 0,
"sort": [
{"publish_date": {"order": "desc"}},
{"_score": {"order": "desc"}}
]
}
# 新增字段
PUT <index>/_mapping/<type>
{
"properties": {
"<name>": {
"type": "integer"
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!