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"
}
}
}

最新文章

  1. Ajax.BeginForm VS Html.BeginForm
  2. WPF的Binding学习笔记(一)
  3. Linq ExecuteQuery,ExecuteCommand
  4. 17.Python笔记之memcached&amp;redis
  5. 虚拟机VMware tools作用以及其安装
  6. C#,.net获取字符串中指定字符串的个数、所在位置与替换字符串
  7. 软件测试-nextDate问题
  8. Python的maketrans() 方法
  9. poj1463(树形dp)
  10. UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
  11. 创建文本节点createTextNode
  12. ZooKeeper 实现分布式队列
  13. ubuntu 16.04安装smatrgitHG工具
  14. 搞懂 JavaScript 继承原理
  15. 基于OpenCV做“三维重建”(1)--找到并绘制棋盘
  16. Linux 驱动——Button8(输入子系统)
  17. 34对MyBatis的博客的整理心得
  18. 【读书笔记】iOS-微信公众平台开发最佳实践
  19. docker——容器(container)
  20. 误删 EhCache 中的数据?

热门文章

  1. 2021级《JAVA语言程序设计》上机考试试题5
  2. 什么是MES(Manufacturing Execution System)
  3. [java安全基础 01]SQL+反序列化
  4. JZOJ 2020.01.11【NOIP提高组】模拟B组
  5. Nacos服务调用(基于Openfeign)
  6. 自己动手从零写桌面操作系统GrapeOS系列教程——5.GrapeOS开发环境测试
  7. postgresql索引使用情况及坏索引处理
  8. 在Github的fork项目中切换分支来提交PR
  9. 若依-更换数据库-sqlite
  10. 28.yield return 语法