Elasticsearch之基本查询及组合查询操作示例

目录
  • Elasticsearch查询
    • 一 基本查询
      • 1.1 match查询
      • 1.2 term查询
      • 1.3 terms查询
      • 1.4 控制查询的返回数量(分页)
      • 1.5 match_all 查询
      • 1.6 match_phrase查询
      • 1.7 multi_match
      • 1.8 指定返回的字段
      • 1.9 sort 结果排序
      • 1.10 range范围查询
      • 1.11 wildcard查询
      • 1.12 exists存在
    • 二 组合查询
      • 2.1 bool查询
      • 2.2 简单过滤查询
      • 2.3 查询多个值
      • 2.4 bool过滤查询,可以做组合过滤查询

Elasticsearch查询

查询分类:

基本查询:使用es内置查询条件进行查询

组合查询:把多个查询组合在一起进行复合查询

过滤:查询的同时,通过filter条件在不影响打分的情况下筛选数据

一 基本查询

#添加映射
PUT lago
{
  "mappings": {
    "properties":{
      "title":{
        "stort":true,
        "type":"text",
        "analyzer":"ik_max_word"
      },
      "company_name":{
         "stort":true,
        	"type":"keyword",
      },
      "desc":{
        "type":"text"
      },
      "comments":{
        "type":"integer"
      },
      "add_time":{
        "type":"date",
        "format":"yyy-MM-dd"
      }
    }
  }
}
#测试数据
POST lago/job
{
  "title":"python django 开发工程师",
  "company_name":"美团科技有限公司",
  "desc":"对django熟悉,掌握mysql和非关系型数据库,网站开发",
  "comments:200,
  "add_time":"2018-4-1"
}
POST lago/job
{
  "title":"python数据分析",
  "company_name":"百度科技有限公司",
  "desc":"熟悉python基础语法,熟悉数据分析",
  "comments:5,
  "add_time":"2018-10-1"
}
POST lago/job
{
  "title":"python自动化运维",
  "company_name":"上海华为",
  "desc":"熟悉python基础语法,精通Linux",
  "comments:90,
  "add_time":"2019-9-18"
}

1.1 match查询

GET lagou/job/_search
{
  "query":{
    "match":{
      "title":"python"
    }
  }
}
#因为title字段做了分词,python都能搜索出来
#搜索python网站也能搜索出来,把python和网站分成两个词
#搜索爬取也能搜索到,把爬和取分词,去搜索
#只搜取 搜不到

1.2 term查询

GET lagou/_search
{
  "query":{
    "term":{
      "title":"python"
    }
  }
}
#会拿着要查询的词不做任何处理,直接查询
#用python爬虫,查不到,用match就能查到
{
  "query":{
    "term":{
      "company_name":"美团"
    }
  }
}
#通过美团,就查询不到

1.3 terms查询

GET lagou/_search
{
  "query":{
    "terms":{
      "title":["工程师","django","运维"]
    }
  }
}
#三个词,只要有一个,就会查询出来

1.4 控制查询的返回数量(分页)

GET lagou/_search
{
  "query":{
    "match":{
      "title":"python"
    }
  },
  "form":1,
  "size":2
}
#从第一条开始,大小为2

1.5 match_all 查询

GET lagou/_search
{
  "query":{
    "match_all":{}
  }
}
#所有数据都返回

1.6 match_phrase查询

GET lagou/_search
{
  "query":{
    "match_phrase":{
      "title":{
        "query":"python系统",
        "slop":6
      }
    }
  }
}
#短语查询,
#会把查询条件python和系统分词,放到列表中,再去搜索的时候,必须满足python和系统同时存在的才能搜出来
#"slop":6 :python和系统这两个词之间最小的距离

1.7 multi_match

GET lagou/_search
{
  "query":{
    "multy_match":{
   			"query":"python",
      	"fields":["title","desc"]
    }
  }
}
#可以指定多个字段
#比如查询title和desc这个两个字段中包含python关键词的文档
#"fields":["title^3","desc"]:权重,title中的python是desc中的三倍

1.8 指定返回的字段

GET lagou/_search
{
  "query":{
    "stored_fields":["title","company_name"]
    "match":{
   			"title":"python"
    }
  }
}
#只返回title和company_name字段
#"stored_fields":["title","company_name",'dsc'],不会返回dsc,因为我们要求stroed_fields,之前desc字段设为false(默认),不会显示

1.9 sort 结果排序

GET lagou/_search
{
  "query":{
 			"match_all":{}
  },
  "sort":[
    {
      "comments":{
        "order":"desc"
      }
    }
  ]
}
#查询所有文档,按comments按desc降序排序

1.10 range范围查询

GET lagou/_search
{
  "query":{
 			"range":{
        "comments":{
          "gte":10,
          "lte":20,
          "boost":2.0
        }
      }
  }
}
#指定comments字段大于等于10,小于等于20
#boost:权重
GET lagou/_search
{
  "query":{
 			"range":{
        "add_time":{
          "gte":"2019-10-11",
          "lte":"now",
        }
      }
  }
}
#对时间进行查询

1.11 wildcard查询

GET lagou/_search
{
  "query":{
    "wildcard":{
      "title":{
        "value":"pyth*n",
        "boost":2.0
      }
    }
  }
}
#模糊查询,title中,有pyth任意值n得都能查出来

1.12 exists存在

exists:字段包含,存在的
# 包含followers_count字段
GET user_toutiao/_search
{
  "query": {
      "bool": {
        "must": [
          {"exists": {
            "field": "followers_count"
          }}
        ]
      }
  }
}
# 不包含followers_count字段
GET user_toutiao/_count
{
  "query": {
      "bool": {
        "must_not": [
          {"exists": {
            "field": "followers_count"
          }}
        ]
      }
  }
}
# 不包含followers_count且updata_timestamp>1614221216
GET user_toutiao/_count
{
  "query": {
      "bool": {
        "must_not": [
          {
            "exists": {
              "field": "followers_count"
            }
          }
        ],
        "must": [
          {"range": {
            "updata_timestamp": {
              "gt": 1614221216
            }
          }}
        ]
      }
  }
}

二 组合查询

2.1 bool查询

#bool查询包括must should must_not filter
'''
bool:{
	"filter":[],   字段过滤
	"must":[],     所有查询条件都满足
	"should":[],   满足一个或多个
	"must_not":{}  都不满足于must相反
}
'''
# 建立测试数据
POST lago/testjob/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"Python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":30,"title":"Elasticsearch"}

2.2 简单过滤查询

#select * from testjob where salary=20
GET lagou/testjob/_search
{
  "query":{
    	"bool":{
        "must":{
          "match_all":{}
        },
        "filter":{
          "term":{
            "salary":20
          }
        }
      }
  }
}

2.3 查询多个值

#查询薪资是10k或20k的
GET lagou/testjob/_search
{
  "query":{
    	"bool":{
        "must":{
          "match_all":{}
        },
        "filter":{
          "terms":{
            "salary":[10,20]
          }
        }
      }
  }
}
#select * from testjob where title="python"
GET lagou/testjob/_search
{
  "query":{
    	"bool":{
        "must":{
          "match_all":{}
        },
        "filter":{
          "term":{
            "title":"Python"
          }
        }
      }
  }
}
#title 是text字段,会做大小写转换,term不会预处理,拿着大写Python去查查不到
#可以改成小写,或者用match来查询
'''
   "filter":{
          "match":{
            "title":"Python"
          }
        }
'''
#查看分析器解析结果
GET _analyze
{
  "analyzer":"ik_max_word",
  "text":"python网络开发工程师"
}

2.4 bool过滤查询,可以做组合过滤查询

#select * from testjob where (salary=20 or title=Python) and (salary!=30)
#查询薪资等于20k或者工作为python的工作,排除价格为30k的
{
  "query":{
    "bool":{
      "should":[
        {"term":{"salary":20}},
        {"term":{"title":"python"}}
      ],
      "must_not":{
        "term":{"salary":30}
      }
    }
  }
}
#select * from testjob where title=python or (title=django and salary=30)
{
  "query":{
    "bool":{
      "should":[
        {"term":{"title":"python"}},
        {
          "bool":{
            "must":[
              {"term":{"title":"django"}},
              {"term":{"salary":30}}
            ]
          }
        }
      ]
    }
  }
}

以上就是Elasticsearch之基本查询及组合查询操作示例的详细内容,更多关于Elasticsearch基本查询组合查询的资料请关注我们其它相关文章!

(0)

相关推荐

  • elasticsearch python 查询的两种方法

    elasticsearch python 查询的两种方法,具体内容如下所述: from elasticsearch import Elasticsearch es = Elasticsearch res1 = es.search(index="2018-07-31", body={"query": {"match_all": {}}}) print(es1) {'_shards': {'failed': 0, 'skipped': 0, 'suc

  • Elasticsearch QueryBuilder简单查询实现解析

    elasticsearch中存储的全部文档 1.matchAllQuery() matchAllQuery()方法用来匹配全部文档 public class QueryTest { public static void main(String[] args) { //创建对象,设置集群名称和IP地址 ElasticsearchUtils es = new ElasticsearchUtils("im_shan", "localhost"); String index

  • Python-ElasticSearch搜索查询的讲解

    Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上. Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索引擎功能的库.但是 Lucene 仅仅只是一个库.为了利用它,你需要编写 Java 程序,并在你的 java 程序里面直接集成 Lucene 包. 更坏的情况是,你需要对信息检索有一定程度的理解才能明白 Lucene 是怎么工作的.Lucene 是 很 复杂的. 在上一篇文章中介绍了ElasticS

  • Elasticsearch实现复合查询高亮结果功能

    一.Es的配置 实现es的全文检索功能的第一步,首先从与es进行连接开始,这里我使用的是es的5.x java api语法. public TransportClient esClient() throws UnknownHostException{ Settings settings = Settings.builder() .put("cluster.name", "my-application") //节点的名字 .put("client.trans

  • elasticsearch kibana简单查询讲解

    一.简单的CRUD操作 1.添加 PUT /index/type/id { "json数据" } 2.查询 GET /index/type/id 3.修改 POST /index/type/id/_update { "doc": { "FIELD": "值" } } 4.删除 DELETE /index/type/id 二.搜索 搜索可以分成六大类 1.query string search 2.query DSL 3.que

  • Elasticsearch之基本查询及组合查询操作示例

    目录 Elasticsearch查询 一 基本查询 1.1 match查询 1.2 term查询 1.3 terms查询 1.4 控制查询的返回数量(分页) 1.5 match_all 查询 1.6 match_phrase查询 1.7 multi_match 1.8 指定返回的字段 1.9 sort 结果排序 1.10 range范围查询 1.11 wildcard查询 1.12 exists存在 二 组合查询 2.1 bool查询 2.2 简单过滤查询 2.3 查询多个值 2.4 bool过

  • go语言实现Elasticsearches批量修改查询及发送MQ操作示例

    目录 update_by_query批量修改 索引添加字段 查询es发送MQ update_by_query批量修改 POST post-v1_1-2021.02,post-v1_1-2021.03,post-v1_1-2021.04/_update_by_query { "query": { "bool": { "must": [ { "term": { "join_field": { "val

  • ElasticSearch学习之多条件组合查询验证及示例分析

    目录 多条件组合查询 bool constant_score 查询验证 & 分析 验证 分析 排序 默认排序 自定义排序 tips 单字段排序 多字段 scroll分页 初始化快照 & 快照保存10分钟 根据快照ID滚动查询 多条件组合查询 bool es中使用bool来控制多条件查询,bool查询支持以下参数: must:被查询的数据必须满足当前条件 mush_not:被查询的数据必须不满足当前条件 should:被查询的数据应该满足当前条件.should查询被用于修正查询结果的评分.需

  • Python实现的排列组合计算操作示例

    本文实例讲述了Python实现的排列组合计算操作.分享给大家供大家参考,具体如下: 1. 调用 scipy 计算排列组合的具体数值 >> from scipy.special import comb, perm >> perm(3, 2) 6.0 >> comb(3, 2) 3.0 2. 调用 itertools 获取排列组合的全部情况数 >> from itertools import combinations, permutations >>

  • Elasticsearch聚合查询及排序操作示例

    目录 1 es排序 2 match和match的区别 3 分页查询 4 es 组合查询 5 结果过滤展示字端 6 结果高亮展示 7 聚合查询avg.max.min.sum.分组 8 mapping和_template模版 9 ik分词 10 term和match的区别 1 es排序 # 1 排序 GET jeff/doc/_search { "query": { "match": { "from": "gu" } }, &qu

  • Go 在 MongoDB 中常用查询与修改的操作

    以下所有例子中结构定义如下: type User struct { Id_ bson.ObjectId `bson:"_id"` Name string `bson:"name"` Age int `bson:"age"` JoinedAt time.Time `bson:"joined_at"` Interests []string `bson:"interests"` Num []int `bson:&

  • MySQL 数据库聚合查询和联合查询操作

    目录 1. 插入被查询的结果 2. 聚合查询 2.1 介绍 2.2 聚合函数 2.3 group by 子句 2.4 having 3. 联合查询 3.1 介绍 3.2 内连接 3.3 外连接 3.4 自连接 3.5 子查询 3.6 合并查询 1. 插入被查询的结果 语法: insert into 要插入的表 [(列1, ..., 列n)] select {* | (列1, ..., 列n)}from 要查询的表 上述语句可以将要查询的表的某些列插入到新的表中对应的某些列 示例1: 将 stud

  • MySQL 数据库聚合查询和联合查询操作

    目录 1.插入被查询的结果 2.聚合查询 2.1介绍 2.2聚合函数 2.3groupby子句 2.4having 3.联合查询 3.1介绍 3.2内连接 3.3外连接 3.4自连接 3.5子查询 3.6合并查询 1. 插入被查询的结果 语法: insert into 要插入的表 [(列1, ..., 列n)] select {* | (列1, ..., 列n)}from 要查询的表 上述语句可以将要查询的表的某些列插入到新的表中对应的某些列 2. 聚合查询 2.1 介绍 聚合查询:是指对一个数

  • Java操作MongoDB模糊查询和分页查询

    本文实例为大家分享了Java操作MongoDB模糊查询和分页查询,供大家参考,具体内容如下 模糊查询条件: 1.完全匹配 Pattern pattern = Pattern.compile("^name$", Pattern.CASE_INSENSITIVE); 2.右匹配 Pattern pattern = Pattern.compile("^.*name$", Pattern.CASE_INSENSITIVE); 3.左匹配 Pattern pattern =

  • Bootstrap Table使用整理(五)之分页组合查询

    推荐阅读: Bootstrap Table使用整理(一) http://www.jb51.net/article/115789.htm Bootstrap Table使用整理(二)  http://www.jb51.net/article/115791.htm Bootstrap Table使用整理(三)  http://www.jb51.net/article/115795.htm Bootstrap Table使用整理(四)之工具栏 http://www.jb51.net/article/1

随机推荐