【Elasticsearch】ElasticSearch基本查询
学习elasticsearch查询用法的时候,发现这篇文章写得很详细,为以后方便查看,就直接搬过来了,原文链接在下面。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014646662/article/details/89010759
1、数据准备
put lib3
{
"settings":{
"number_of_shards":3,
"number_of_replicas":0
},
"mappings":{
"user":{
"properties":{
"name":{"type":"text"},
"address":{"type":"text"},
"age":{"type":"integer"},
"interests":{"type":"text"},
"birthday":{"type":"date"}
}
}
}
} post /lib3/user
{
"name":"lisi",
"address":"shandong",
"age":18,
"interests":"youyong shufa changge tiaowu",
"birthday":"2001-01-19"
} post /lib3/user
{
"name":"wangwu",
"address":"zhejiang",
"age":22,
"interests":"youyong shufa",
"birthday":"1997-01-19"
} post /lib3/user
{
"name":"zhangsan",
"address":"zhejiang",
"age":20,
"interests":"youyong shufa changge changpao",
"birthday":"1999-08-29"
} post /lib3/user
{
"name":"youyong",
"address":"zhejiang",
"age":20,
"interests":"youyong shufa changge changpao",
"birthday":"1999-08-29"
}
测试一下
get /lib3/user/_search?q=name:lisi
get /lib3/user/_search?q=name:wangwu&sort=age:desc
2、term查询和terms查询
- term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword 、numeric、date。
- term:查询某个字段里含有某个关键词的文档
- terms:查询某个字段里含有多个关键词的文档
get /lib3/user/_search/
{
"query":{"term":{ "interests":"youyong"}}
} get lib3/user/_search/
{
"query":{"terms":{"interests":["shufa","youyong"]}}
}
3、控制查询返回的数量
get lib3/user/_search
{
"from":,
"size":,
"query":{
"terms":{
"interests": ["changge","tiaowu"]
}
}
}
4、返回版本号
get lib3/user/_search
{
"version":true,
"query":{
"term":{"interests": "changge"}
}
}
5、match查询
- match query知道分词器的存在,会对filed进行分词操作,然后再查询
- match_all:查询所有文档
- multi_match:可以指定多个字段
- match_phrase:短语匹配查询,ElasticSearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变
GET lib3/user/_search
{
"query":{"match":{"age": 20}}
}
GET lib3/user/_search
{
"query":{"match":{"interests": "changge"}}
} GET lib3/user/_search
{
"query":{
"match_all": {}
}
} GET lib3/user/_search
{
"query":{
"multi_match": {
"query": "youyong",
"fields":["interests","name"]
}
}
} get lib3/user/_search
{
"query":{
"match_phrase":{"interests": "youyong shufa"}
}
}
6、指定返回的字段、显示字段
get lib3/user/_search
{
"_source":["name","age"],
"query":{
"match": {
"interests": "changge"
}
}
}
7、显示要的字段、去除不需要的字段
get lib3/user/_search
{
"query":{
"match_all": {}
},
"_source":{
"includes": "addr*",
"excludes": ["name","bir*"]
}
}
8、排序
- 前缀匹配查询"match_phrase_prefix",
- 使用sort实现排序:desc:降序,asc升序
GET /lib3/user/_search
{
"query": {
"match_phrase_prefix": {
"interests": "you"
}
}, "sort":[
{
"age":{"order": "desc"}
}
]
}
9、 范围查询
- range:实现范围查询
- 参数:from,to,include_lower,include_upper,boost
- include_lower:是否包含范围的左边界,默认是true
- include_upper:是否包含范围的右边界,默认是true
GET /lib3/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2000-05-01",
"include_lower": true,
"include_upper": false
}
}
}
} GET /lib3/user/_search
{
"query": {
"range": {
"age": {
"from": 18,
"to": 25,
"include_lower": true,
"include_upper": false
}
}
}
}
10、wildcard查询
- 允许使用通配符* 和 ?来进行查询
- *代表0个或多个字符
- ?代表任意一个字符
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "wang*"
}
}
} GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "li?i"
}
}
}
11、fuzzy实现模糊查询
- value:查询的关键字
- boost:查询的权值,默认值是1.0
- min_similarity:设置匹配的最小相似度,默认值为0.5,对于字符串,取值为0-1(包括0和1);对于数值,取值可能大于1;对于日期型取值为1d,1m等,1d就代表1天
- prefix_length:指明区分词项的共同前缀长度,默认是0
- max_expansions:查询中的词项可以扩展的数目,默认可以无限大
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": "chagge"
}
}
} GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": {
"value": "chagge"
}
}
}
}
12、高亮搜索结果
GET /lib3/user/_search
{
"query":{
"match":{
"interests": "changge"
}
},
"highlight": {
"fields": {
"interests": {}
}
}
}
【Elasticsearch】ElasticSearch基本查询的更多相关文章
- elasticsearch GIS空间查询问题解决
在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询 我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...
- Elasticsearch文档查询
简单数据集 到目前为止,已经了解了基本知识,现在我们尝试用更逼真的数据集,这儿已经准备好了一份虚构的JSON,关于客户银行账户信息的.每个文档的结构如下: { , , "firstname& ...
- Elasticsearch(GEO)空间检索查询
Elasticsearch(GEO)空间检索查询python版本 1.Elasticsearch ES的强大就不用多说了,当你安装上插件,搭建好集群,你就拥有了一个搜索系统. 当然,ES的集群优化和查 ...
- 利用kibana插件对Elasticsearch进行bool查询
#bool查询#老版本的filtered查询已经被bool代替#用 bool包括 must should must_not filter来完成 ,格式如下:#bool:{# "filter ...
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)
1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...
- java使用elasticsearch进行模糊查询-已在项目中实际应用
java使用elasticsearch进行模糊查询 使用环境上篇文章本人已书写过,需要maven坐标,ES连接工具类的请看上一篇文章,以下是内容是笔者在真实项目中运用总结而产生,并写的是主要方法和思路 ...
- Elasticsearch 常用基本查询
安装启动很简单,参考官网步骤:https://www.elastic.co/downloads/elasticsearch 为了介绍Elasticsearch中的不同查询类型,我们将对带有下列字段的文 ...
- kibana和ElasticSearch的信息查询检索
使用kibana来进行ElasticSearch的信息查询检索 大家经常会听到使用ELK搭建日志管理平台.完成日志聚合检索的功能,那么这个平台到底是个什么概念,怎么搭建,怎么使用呢? ELK包括Ela ...
- Elasticsearch之cur查询索引
前提, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 Elasticsearch之curl创建索引库和索引时注意事项 Elasticsearch之cur ...
随机推荐
- 130、TensorFlow操作多个计算图
# Programming with multiple graphs # 当训练一个模型的时候一个常用的方式就是使用一个图来训练你的模型 # 另一个图来评价和计算训练的效果 # 在许多情况下前向计算和 ...
- MyEclipse上有main函数类运行报错:Editor does not contain a
MyEclipse下有main函数类运行报错:Editor does not contain a main type?出现这种问题的原因是,该java文件 MyEclipse下有main函数类运行 ...
- python web自动化测试框架搭建(功能&接口)——测试用例执行和结果收集
由于unittest框架中结果收集在不同文件中,所以此处重写结果收集方法,加入执行时间,失败信息,失败截图等 TestRunner.py # coding=utf-8 import sys impor ...
- python实现压缩文件成zip格式
实现代码如下: #压缩文件 import time,zipfile class zip: def get_zip(self,files,zip_name): zp=zipfile.ZipFile(zi ...
- C++ bitset的使用
bitset 一般代替 bool 数组使用,常用于优化空间,因为 bitset 中一个元素只占 1 bit. bitset 的大小在定义使就需要确定.如果想要不定长的 bitset,就需要使用 vec ...
- 【题解】4879. 【NOIP2016提高A组集训第11场11.9】少女觉
Description 在幽暗的地灵殿中,居住着一位少女,名为古明地觉.据说,从来没有人敢踏入过那座地灵殿,因为人们恐惧于觉一族拥有的能力——读心.掌控人心者,可控天下. 咳咳.人的记忆可以被描述为一 ...
- 15.队列Queue的特点以及使用,优先级等
#生产者与消费者模式,模式解释:比如MVC设计模式 ''' 1.队列 (1)特点:先进先出 (2)python2 VS python3 python2:from Queue import queue ...
- Eclipse + pydev插件
在Eclipse中安装pydev插件 启动Eclipse, 点击Help->Install New Software... 在弹出的对话框中,点Add 按钮. Name中填:Pydev, ...
- CNN之池化层tf.nn.max_pool | tf.nn.avg_pool | tf.reduce_mean | padding的规则解释
摘要:池化层的主要目的是降维,通过滤波器映射区域内取最大值.平均值等操作. 均值池化:tf.nn.avg_pool(input,ksize,strides,padding) 最大池化:tf.nn.ma ...
- No-sql之redis常用命令
转自:http://blog.csdn.net/nicewuranran/article/details/51793760 No-SQL之Redis 介绍 Redis是一种基于内存存储的key-val ...