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

ElasticSearch入门学习笔记的更多相关文章

  1. Elasticsearch入门学习重点笔记

    原文:Elasticsearch入门学习重点笔记 必记知识点 Elasticsearch可以接近实时的搜索和存储大量数据.Elasticsearch是一个近实时的搜索平台.这意味着当你导入一个文档并把 ...

  2. 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  3. Hadoop入门学习笔记---part4

    紧接着<Hadoop入门学习笔记---part3>中的继续了解如何用java在程序中操作HDFS. 众所周知,对文件的操作无非是创建,查看,下载,删除.下面我们就开始应用java程序进行操 ...

  4. Hadoop入门学习笔记---part3

    2015年元旦,好好学习,天天向上.良好的开端是成功的一半,任何学习都不能中断,只有坚持才会出结果.继续学习Hadoop.冰冻三尺,非一日之寒! 经过Hadoop的伪分布集群环境的搭建,基本对Hado ...

  5. PyQt4入门学习笔记(三)

    # PyQt4入门学习笔记(三) PyQt4内的布局 布局方式是我们控制我们的GUI页面内各个控件的排放位置的.我们可以通过两种基本方式来控制: 1.绝对位置 2.layout类 绝对位置 这种方式要 ...

  6. PyQt4入门学习笔记(一)

    PyQt4入门学习笔记(一) 一直没有找到什么好的pyqt4的教程,偶然在google上搜到一篇不错的入门文档,翻译过来,留以后再复习. 原始链接如下: http://zetcode.com/gui/ ...

  7. Hadoop入门学习笔记---part2

    在<Hadoop入门学习笔记---part1>中感觉自己虽然总结的比较详细,但是始终感觉有点凌乱.不够系统化,不够简洁.经过自己的推敲和总结,现在在此处概括性的总结一下,认为在准备搭建ha ...

  8. Hadoop入门学习笔记---part1

    随着毕业设计的进行,大学四年正式进入尾声.任你玩四年的大学的最后一次作业最后在激烈的选题中尘埃落定.无论选择了怎样的选题,无论最后的结果是怎样的,对于大学里面的这最后一份作业,也希望自己能够尽心尽力, ...

  9. Scala入门学习笔记三--数组使用

    前言 本篇主要讲Scala的Array.BufferArray.List,更多教程请参考:Scala教程 本篇知识点概括 若长度固定则使用Array,若长度可能有 变化则使用ArrayBuffer 提 ...

  10. OpenCV入门学习笔记

    OpenCV入门学习笔记 参照OpenCV中文论坛相关文档(http://www.opencv.org.cn/) 一.简介 OpenCV(Open Source Computer Vision),开源 ...

随机推荐

  1. 万字长文概述单目3D目标检测算法

    一,理论基础-相机与图像 1.1,单目相机介绍 1.2,针孔相机模型 1.3,坐标系间的欧式变换 1.4,世界坐标与像素坐标的转换 1.5,三维旋转:欧拉角.旋转矩阵之间的转换 二,单目3D目标检测概 ...

  2. 超声和免疫学指标的特征能否反映RA临床缓解的表型?[EULAR2015_THU0121]

    超声和免疫学指标的特征能否反映RA临床缓解的表型?   THU0121 DO THE IMMUNOLOGICAL AND ULTRASOUND CHARACTERISTICS REFLECT THE ...

  3. Oracle 账户【2023/2/5最新】

    下载Oracle产品一般都需要登录Oracle账户才可以下载,这里提供一个账户方便大家使用,觉得有用的希望能点个小小的赞! 账号:191049742@qq.com 密码:1234qwerASDF!@# ...

  4. vue-cli 更新遇到的问题,卸载不掉旧版本2.9.6(可行)

    今天更新vue-cli时,遇到问题:卸载不掉旧版本2.9 官网给的信息是:Vue CLI 的包名称由 vue-cli 改成了 @vue/cli. 如果你已经全局安装了旧版本的 vue-cli (1.x ...

  5. 置顶TOP

    HTML <a href="javascript:void(0)" class="go-top" @click="goTop"> ...

  6. 在 WXML 中使用 JS 代码

    {{}} 里面可以写任何的 JS 表达式,(一定是表达式,例如定义函数等都不属于表达式).例如,下面是在 wxml 中使用 JS 表达式: <view class="price tex ...

  7. LeetCode算法训练 93.复原IP地址 78.子集 90.子集II

    欢迎关注个人公众号:爱喝可可牛奶 LeetCode算法训练 93.复原IP地址 78.子集 90.子集II LeetCode 93. 复原 IP 地址 分析 字符串全部由数字组成,ipv4每一段数字不 ...

  8. torch.nn.Embedding使用详解

    torch.nn.Embedding: 随机初始化词向量,词向量值在正态分布N(0,1)中随机取值.输入:torch.nn.Embedding(num_embeddings, – 词典的大小尺寸,比如 ...

  9. js手动触发页面元素点击事件,自定义点击事件模拟点击

    // initEvent事件已经弃用1. 创建MouseEvents事件const clickEvent = document.createEvent('MouseEvents')2. 初始化点击事件 ...

  10. QT个人笔记

    1.QT的setCursor(Qt::CrossCursor) 干什么用 ui->View->setCursor(Qt::CrossCursor); setCursor函数用于设置窗口上的 ...