ES基本查询
数据准备
# 创建索引
PUT /lib1
{
"settings": {
"number_of_shards": 5
, "number_of_replicas": 0
},
"mappings": {
"user":{
"properties": {
"name":{"type":"text"},
"adress":{"type":"text"},
"age":{"type":"integer"},
"interests":{"type":"text"},
"birthday":{"type":"date"}
}
}
}
}
# 添加数据
PUT /lib1/user/1
{
"name":"zhaoliu",
"adress":"hei long jiang sheng tie ling shi",
"age":50,
"birthday":"1970-10-12",
"interests":"xi huan hejiu,duanlian,lvyou"
}
PUT /lib1/user/2
{
"name":"zhaoming",
"adress":"bei ling dian qu qing he zhen",
"age":20,
"birthday":"1998-10-12",
"interests":"xi huan hejiu,duanlian,changge"
}
PUT /lib1/user/3
{
"name":"lisi",
"adress":"bei ling dian qu qing he zhen",
"age":50,
"birthday":"1998-10-12",
"interests":"xi huan hejiu,duanlian,lvyou"
}
PUT /lib1/user/4
{
"name":"wangwu",
"adress":"bei ling dian qu qing he zhen",
"age":20,
"birthday":"1995-10-12",
"interests":"xi huan hejiu,duanlian,changge"
}
PUT /lib1/user/5
{
"name":"zhangsan",
"adress":"bei jing chao yang qu",
"age":29,
"birthday":"1988-10-12",
"interests":"xi huan tingyinyue,changge,lvyou"
}
# 简单条件查询
GET /lib1/user/_search?q=name:lisi
GET /lib1/user/_search?q=interests:changge&sort=age:desc
term查询和terms查询
term query回去倒排索引中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword、numeric、date。
term:查询某个字段含有某个关键词的文档
GET /lib1/user/_search
{
"query": {
"term": {
"name": "zhaoliu"
}
}
}
# 查询结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}terms:查询某个字段里含有多个关键词的文档
# 查询
GET /lib1/user/_search
{
"query": {
"terms": {
"interests": ["hejiu","changge"]
}
}
}
# 查询的结果为interests字段包含heijiu、changge、hejiu和changge的文档
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.5467954,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 1.5467954,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "5",
"_score": 0.2824934,
"_source": {
"name": "zhangsan",
"adress": "bei jing chao yang qu",
"age": 29,
"birthday": "1988-10-12",
"interests": "xi huan tingyinyue,changge,lvyou"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2824934,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}
# 指定返回结果为2个文档
GET /lib1/user/_search
{
"from": 0,
"size": 2,
"query": {
"terms": {
"interests": ["hejiu","changge"]
}
}
}
# 指定返回结果中含版本号
GET /lib1/user/_search
{
"from": 0,
"size": 2, "version": true, "query": { "terms": { "interests": ["hejiu","changge"] } }}# 查询结果{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 1.5467954, "hits": [ { "_index": "lib1", "_type": "user", "_id": "2", "_version": 3, "_score": 1.5467954, "_source": { "name": "zhaoming", "adress": "bei ling dian qu qing he zhen", "age": 20, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" } }, { "_index": "lib1", "_type": "user", "_id": "5", "_version": 2, "_score": 0.2824934, "_source": { "name": "zhangsan", "adress": "bei jing chao yang qu", "age": 29, "birthday": "1988-10-12", "interests": "xi huan tingyinyue,changge,lvyou" } } ] }}
match查询
math query指定分词器的存在,会对filed进行分词操作,然后再查询
GET /lib1/user/_search
{
"query": {
"match": {
"name": "zhaoliuwu"
}
}
}
# 执行结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}
# 查询
GET /lib1/user/_search
{
"query": {
"match": {
"name": "zhaoliuwu zhaoming"
}
}
}
# 执行结果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.80259144,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 0.80259144,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12", "interests": "xi huan hejiu,duanlian,lvyou" } } ] }}match_all:查询所有文档
GET /lib1/user/_search
{
"query": {
"match_all": {}
}
}multi_match:指定多个字段
GET /lib1/user/_search
{
"query": {
"multi_match": {
"query": "changge",
"fields": ["interests","name"]
}
}
}match_phrase:短语匹配查询
ElasticSearch引擎首先分析查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变:
GET /lib1/user/_search
{
"query": {
"match_phrase": {
"interests": "duanlian,changge"
}
}
}
# 执行结果
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.5467954,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 1.5467954,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
}
}
]
}
}指定查询字段
# 指定查询字段
GET /lib1/user/_search
{
"_source": {
"includes": ["name","adress"]
},
"query": {
"match_all": {}
}
}
# 排除查询字段
GET /lib1/user/_search
{
"_source": {
"excludes": ["name","adress"]
},
"query": {
"match_all": {}
}
}
# 通配符匹配查询字段
GET /lib1/user/_search
{
"_source": {
"includes":"adr*",
"excludes": ["name","bir*"]
},
"query": {
"match_all": {}
}
}
# 执行结果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "5",
"_score": 1,
"_source": {
"adress": "bei jing chao yang qu"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "4",
"_score": 1,
"_source": {
"adress": "北京海淀区清河"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"adress": "bei ling dian qu qing he zhen"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"adress": "hei long jiang sheng tie ling shi"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "3",
"_score": 1, "_source": { "adress": "北京海淀区清河" } } ] }}
排序
使用sort实现排序:desc降序,asc升序
GET /lib1/user/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
GET /lib1/user/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
前缀匹配
GET /lib1/user/_search
{
"query": {
"match_phrase_prefix": {
"name": {
"query": "zhao"
}
}
}
}
范围查询
# []
GET /lib1/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01"
}
}
}
}
# ()
GET /lib1/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1995-10-12",
"to": "1998-10-12",
"include_lower":false,
"include_upper":false
}
}
}
}
GET /lib1/user/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 28
}
}
}
}
wildcard查询
允许使用通配符*和?来进行查询
*代表0个或多个字符
?代表任意一个字符
GET /lib1/user/_search
{
"query": {
"wildcard": {
"name": {
"value": "li?i"
}
}
}
}
GET /lib1/user/_search
{
"query": {
"wildcard": {
"name": "zhao*"
}
}
}
fuzzy实现模糊查询
value:查询的关键字
boost:查询的权值,默认值是1.0
min_similarity:设置匹配的最小相似度,默认值是0.5,对于字符串,取值为0-1(包括0和1);对于数值,取值可能大于1;对于日期型取值为1d,1m等。
prefix_length:指名区分词项的共同前缀长度,默认是0
max_expansion:查询中的词项可以扩展的数目,默认可以无限大
# 查询
GET /lib1/user/_search
{
"query": {
"fuzzy": {
"name":"zhliuwu"
}
}
}
# 查询结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2054872,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2054872,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}
# 查询
GET /lib1/user/_search
{
"query": {
"fuzzy": {
"interests": {
"value": "chagge"
}
}
}
, "highlight": {
"fields": {
"interests": {}
}
}
}
# 执行结果
{
"took": 43,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.64449805,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 0.64449805,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
},
"highlight": {
"interests": [
"xi huan hejiu,duanlian,<em>changge</em>"
]
}
},
{
"_index": "lib1",
"_type": "user", "_id": "5", "_score": 0.23541118, "_source": { "name": "zhangsan", "adress": "bei jing chao yang qu", "age": 29, "birthday": "1988-10-12", "interests": "xi huan tingyinyue,changge,lvyou" }, "highlight": { "interests": [ "xi huan tingyinyue,<em>changge</em>,lvyou" ] } } ] }}
高亮搜索结果
GET /lib1/user/_search
{
"query": {
"match": {
"interests": "changge"
}
},
"highlight": {
"fields": {"interests": {}}
}
}
# 查询结果
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.7733977,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 0.7733977,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
},
"highlight": {
"interests": [
"xi huan hejiu,duanlian,<em>changge</em>"
]
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "5",
"_score": 0.2824934,
"_source": {
"name": "zhangsan",
"adress": "bei jing chao yang qu",
"age": 29,
"birthday": "1988-10-12",
"interests": "xi huan tingyinyue,changge,lvyou"
},
"highlight": {
"interests": [
"xi huan tingyinyue,<em>changge</em>,lvyou"
]
}
}
]
}
}
ES基本查询的更多相关文章
- ES 复合查询
ES在查询过程中比较多遇到符合查询,既需要多个字段过滤也需要特殊情况处理,本文简单介绍几种查询组合方便快捷查询ES. bool布尔查询有一个或者多个布尔子句组成 filter 只过滤符合条件的 ...
- es的查询、排序查询、分页查询、布尔查询、查询结果过滤、高亮查询、聚合函数、python操作es
今日内容概要 es的查询 Elasticsearch之排序查询 Elasticsearch之分页查询 Elasticsearch之布尔查询 Elasticsearch之查询结果过滤 Elasticse ...
- ES高级查询
Query Content 在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件的匹配有多好 # POST 192.168.100.1 ...
- ES 20 - 查询Elasticsearch中的数据 (基于DSL查询, 包括查询校验match + bool + term)
目录 1 什么是DSL 2 DSL校验 - 定位不合法的查询语句 3 match query的使用 3.1 简单功能示例 3.1.1 查询所有文档 3.1.2 查询满足一定条件的文档 3.1.3 分页 ...
- [ES]Python查询ES导出数据为Excel
版本 elasticsearch==5.5.0 python==3.7 说明 用python查询es上存储的状态数据,将查询到的数据用pandas处理成excel code # -*- coding: ...
- 面试系列九 es 提高查询效率
,es性能优化是没有什么银弹的,啥意思呢?就是不要期待着随手调一个参数,就可以万能的应对所有的性能慢的场景.也许有的场景是你换个参数,或者调整一下语法,就可以搞定,但是绝对不是所有场景都可以这样. 一 ...
- ES模糊查询来对应mysql的like查询
使用ES查询来对应mysql的like查询 建立一个测试索引 PUT /test_like1 { "mappings" : { "properties" : { ...
- es crul查询(一)
C:\Users\Administrator>elasticdump --input=D:\test --output=http://localhost:9200/logs_apipki_201 ...
- ES系列十、ES常用查询API
1.term查询 { "query": { "term": { "title": "crime" } } } 1.1.指 ...
随机推荐
- Android.Tools.Summary
Android平台上工具的总结 每个工具的详细使用和深入理解参考每个工具相关的blog. 1. Android SDK中提供的工具 http://developer.android.com/tools ...
- 图片延时加载原理 和 使用jquery实现的一个图片延迟加载插件(含图片延迟加载原理)
图片加载技术分为:图片预加载和图片延时加载. javascript图片预加载和延时加载的区别主要体现在图片传输到客户端的时机上,都是为了提升用户体验的,延时加载又叫懒加载.两种技术的本质:两者的行为是 ...
- Windows下war包部署到Linux下Tomcat出现的问题
最近,将Windows下开发的war包部署到Linux下的Tomcat时报了一个错误:tomcat error in opening zip file.按理说,如果正常,当把war包复制到webapp ...
- HapMap
HapMap五周年回顾 2011-01-12 | 作者: [关闭] 作者简介:曾长青,中国科学院北京基因组所研究员,博士生导师.CUSBEA奖学金.百人计划.杰出青年基金.首批新世纪百千万人才工程国家 ...
- Java 中转换为String类型的四种方法
1. 使用 String 的构造方法,用于 byte[], char[], StringBuffer, StringBuilder 类型 2. 使用 String 的静态方法 valueOf() 推荐 ...
- Virtual Machine Kernel Panic : Not Syncing : VFS : Unable To Mount Root FS On Unknown-Block (0,0)
Virtual Machine Kernel Panic : Not Syncing : VFS : Unable To Mount Root FS On Unknown-Block (0,0) 33 ...
- Spring Boot REST(一)核心接口
Spring Boot REST(一)核心接口 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) SpringBoot RE ...
- Python之路(第十五篇)sys模块、json模块、pickle模块、shelve模块
一.sys模块 1.sys.argv 命令行参数List,第一个元素是程序本身路径 2.sys.exit(n) 退出程序,正常退出时exit(0) 3.sys.version . sys.maxint ...
- 使用yarn 安装 Vue-DevTools
1. 从 github 下载 vuejs/vue-devtools https://github.com/vuejs/vue-devtools/archive/dev.zip 2.安装yarn 及 编 ...
- 华为QOS原理及配置
http://www.tudou.com/programs/view/GWCiHfWC9FI/ FLASH : http://www.tudou.com/v/GWCiHfWC9FI/&reso ...