ElasticSearch(四):基本搜索

学习课程链接《Elasticsearch核心技术与实战》

## URI Search
使用HTTP的GET方法,在URL中使用查询参数进行查询。
```
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
"profile":"true"
}
```
* `q`指定查询语句,使用`Query String Syntax`
* `df`默认字段,若不指定,会对所有字段进行查询
* `sort`用于排序
* `from`、`size`用于分页
* `profile`用于展示查询是如何被执行的

#基本查询
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
#基本查询返回结果
{
"took" : 50,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "105254",
"_score" : null,
"_source" : {
"id" : "105254",
"genre" : [
"Adventure",
"Comedy"
],
"title" : "Crystal Fairy & the Magical Cactus and 2012",
"year" : 2013,
"@version" : "1"
},
"sort" : [
2013
]
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "72378",
"_score" : null,
"_source" : {
"id" : "72378",
"genre" : [
"Action",
"Drama",
"Sci-Fi",
"Thriller"
],
"title" : "2012",
"year" : 2009,
"@version" : "1"
},
"sort" : [
2009
]
}
]
}
}
#带profile
GET /movies/_search?q=2012&df=title
{
"profile":"true"
}
#指定字段q=title:2012等价于q=2012&df=title
GET /movies/_search?q=title:2012&sort=year:desc&from=0&size=10&timeout=1s
{
"profile":"true"
}
#带profile返回结果
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 11.303033,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "72378",
"_score" : 11.303033,
"_source" : {
"id" : "72378",
"genre" : [
"Action",
"Drama",
"Sci-Fi",
"Thriller"
],
"title" : "2012",#只查询title中的2012
"year" : 2009,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "105254",
"_score" : 5.2497,
"_source" : {
"id" : "105254",
"genre" : [
"Adventure",
"Comedy"
],
"title" : "Crystal Fairy & the Magical Cactus and 2012",
"year" : 2013,
"@version" : "1"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[u-4S1mfbQiuA1Bqe-wfPJQ][movies][0]",
"searches" : [
{
"query" : [
{
"type" : "TermQuery",
"description" : "title:2012",
"time_in_nanos" : 1105745,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 7966,
"match" : 0,
"next_doc_count" : 4,
"score_count" : 2,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 133876,
"build_scorer_count" : 9,
"create_weight" : 187971,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 775916
}
}
],
"rewrite_time" : 4990,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 574426,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 158099
}
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}
#泛查询,正对_all,所有字段,查询所有字段中的2012
GET /movies/_search?q=2012
{
"profile":"true"
}
#使用引号,Phrase查询,还要求前后顺序保持一致,"Beautiful Mind"等效于Beautiful AND Mind
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile":"true"
}
#Phrase查询返回结果
{
"took" : 16,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 13.68748,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4995",
"_score" : 13.68748,
"_source" : {
"id" : "4995",
"genre" : [
"Drama",
"Romance"
],
"title" : "Beautiful Mind, A", #查询条件
"year" : 2001,
"@version" : "1"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[u-4S1mfbQiuA1Bqe-wfPJQ][movies][0]",
"searches" : [
{
"query" : [
{
"type" : "PhraseQuery", #查询类型为PhraseQuery
"description" : """title:"beautiful mind"""",
"time_in_nanos" : 10670089,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 1,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 28250,
"match" : 27343,
"next_doc_count" : 3,
"score_count" : 1,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 9171,
"build_scorer_count" : 9,
"create_weight" : 7583491,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 3021819
}
}
],
"rewrite_time" : 7818,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 39950,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 25137
}
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}
# Term查询为泛查询,(Beautiful Mind)等效于Beautiful OR Mind
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile":"true"
}
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 20,
"relation" : "eq"
},
"max_score" : 13.687479,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4995",
"_score" : 13.687479,
"_source" : {
"id" : "4995",
"genre" : [
"Drama",
"Romance"
],
"title" : "Beautiful Mind, A",
"year" : 2001,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "3912",
"_score" : 8.723258,
"_source" : {
"id" : "3912",
"genre" : [
"Comedy",
"Drama"
],
"title" : "Beautiful",
"year" : 2000,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "47404",
"_score" : 8.576847,
"_source" : {
"id" : "47404",
"genre" : [
"Adventure",
"Animation",
"Comedy",
"Fantasy",
"Romance",
"Sci-Fi"
],
"title" : "Mind Game",
"year" : 2004,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "1046",
"_score" : 7.317063,
"_source" : {
"id" : "1046",
"genre" : [
"Drama",
"Romance"
],
"title" : "Beautiful Thing",
"year" : 1996,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "3302",
"_score" : 7.317063,
"_source" : {
"id" : "3302",
"genre" : [
"Comedy"
],
"title" : "Beautiful People",
"year" : 1999,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4242",
"_score" : 7.317063,
"_source" : {
"id" : "4242",
"genre" : [
"Comedy",
"Crime",
"Drama",
"Thriller"
],
"title" : "Beautiful Creatures",
"year" : 2000,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4372",
"_score" : 7.317063,
"_source" : {
"id" : "4372",
"genre" : [
"Drama",
"Romance"
],
"title" : "Crazy/Beautiful",
"year" : 2001,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "94",
"_score" : 7.317063,
"_source" : {
"id" : "94",
"genre" : [
"Comedy",
"Drama",
"Romance"
],
"title" : "Beautiful Girls",
"year" : 1996,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "90353",
"_score" : 7.317063,
"_source" : {
"id" : "90353",
"genre" : [
"Drama"
],
"title" : "Beautiful Boy",
"year" : 2010,
"@version" : "1"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "100487",
"_score" : 7.317063,
"_source" : {
"id" : "100487",
"genre" : [
"Drama",
"Fantasy",
"Romance"
],
"title" : "Beautiful Creatures",
"year" : 2013,
"@version" : "1"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[u-4S1mfbQiuA1Bqe-wfPJQ][movies][0]",
"searches" : [
{
"query" : [
{
"type" : "BooleanQuery",
"description" : "title:beautiful title:mind",
"time_in_nanos" : 963305,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 6,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 133259,
"match" : 2086,
"next_doc_count" : 27,
"score_count" : 20,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 32590,
"build_scorer_count" : 14,
"create_weight" : 401324,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 393978
},
"children" : [
{
"type" : "TermQuery",
"description" : "title:beautiful",
"time_in_nanos" : 492649,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 6,
"set_min_competitive_score" : 0,
"next_doc" : 49055,
"match" : 0,
"next_doc_count" : 15,
"score_count" : 16,
"compute_max_score_count" : 6,
"compute_max_score" : 85498,
"advance" : 6172,
"advance_count" : 6,
"score" : 20213,
"build_scorer_count" : 17,
"create_weight" : 255976,
"shallow_advance" : 4730,
"create_weight_count" : 1,
"build_scorer" : 70938
}
},
{
"type" : "TermQuery",
"description" : "title:mind",
"time_in_nanos" : 187326,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 6,
"set_min_competitive_score" : 0,
"next_doc" : 2447,
"match" : 0,
"next_doc_count" : 4,
"score_count" : 5,
"compute_max_score_count" : 6,
"compute_max_score" : 11373,
"advance" : 4506,
"advance_count" : 5,
"score" : 5483,
"build_scorer_count" : 15,
"create_weight" : 120204,
"shallow_advance" : 3324,
"create_weight_count" : 1,
"build_scorer" : 39947
}
}
]
}
],
"rewrite_time" : 16452,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 85953,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 60755
}
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}
#布尔操作符
# 必须包括Beautiful 和 Mind
GET /movies/_search?q=title:(Beautiful AND Mind)
{
"profile":"true"
}
# 必须包括Beautiful 但不能包括 Mind
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
"profile":"true"
}
#‘%2B’即为‘+’号表示必须包括包括 Mind
GET /movies/_search?q=title:(Beautiful %2BMind)
{
"profile":"true"
}
#范围查询 ,区间写法年份在2002~2018
GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018]
{
"profile":"true"
}
#通配符查询
GET /movies/_search?q=title:b*
{
"profile":"true"
}
#模糊匹配&近似度匹配
GET /movies/_search?q=title:beautifl~1
{
"profile":"true"
}
GET /movies/_search?q=title:"Lord Rings"~2
{
"profile":"true"
}

## Request Body Search
使用Elasticsearch提供的,基于JSON格式的更加完备的Query Domain Specific Language (DSL)

#query查询
#ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错
POST /movies,404_idx/_search?ignore_unavailable=true
{
"profile": true,
"query": {
"match_all": {}
}
} #查询movies分页
POST /movies/_search
{
"from":10,
"size":20,
"query":{
"match_all": {}
}
} #对日期排序
POST kibana_sample_data_ecommerce/_search
{
"sort":[{"order_date":"desc"}],
"query":{
"match_all": {}
} } #_source 过滤显示的字段
POST kibana_sample_data_ecommerce/_search
{
"_source":["order_date"],#返回结果只显示"order_date"
"query":{
"match_all": {}
}
} #脚本字段
GET kibana_sample_data_ecommerce/_search
{
"script_fields": {
"new_field": {
"script": {
"lang": "painless",
"source": "doc['order_date'].value+'hello'"
}
}
},
"query": {
"match_all": {}
}
} #match查询,last OR christmas
POST movies/_search
{
"query": {
"match": {
"title": "last christmas"
}
}
} #match查询,last AND christmas
POST movies/_search
{
"query": {
"match": {
"title": {
"query": "last christmas",
"operator": "and"
}
}
}
} #match_phrase查询,one AND love,且顺序不能乱
POST movies/_search
{
"query": {
"match_phrase": {
"title":{
"query": "one love"#不可以查出 "title" : "One I Love, The", }
}
}
} #match_phrase查询,slop在one love中间插入指定数量单词
POST movies/_search
{
"query": {
"match_phrase": {
"title":{
"query": "one love",#可以查出 "title" : "One I Love, The",
"slop": 1 }
}
}
}

PUT /users/_doc/1
{
"name":"Ruan Yiming",
"about":"java, golang, node, swift, elasticsearch"
} PUT /users/_doc/2
{
"name":"Li Yiming",
"about":"Hadoop"
} #query_string查询
POST users/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "Ruan AND Yiming"
}
}
} #query_string查询
POST users/_search
{
"query": {
"query_string": {
"fields":["name","about"],
"query": "(Ruan AND Yiming) OR (Java AND Elasticsearch)"
}
}
} #Simple Query 类似query_string查询,但会忽略错误的语法,同时只支持部分查询语法;默认的operator是 Or,可以指定;不支持AND OR NOT,会当字符串处理;支持部分逻辑+替代AND,|替代OR,-替代NOT
POST users/_search
{
"query": {
"simple_query_string": {
"query": "Ruan AND Yiming",
"fields": ["name"]
}
}
} #Simple Query
POST users/_search
{
"query": {
"simple_query_string": {
"query": "Ruan Yiming",
"fields": ["name"],
"default_operator": "AND"
}
}
} GET /movies/_search
{
"profile": true,
"query":{
"query_string":{
"default_field": "title",
"query": "Beafiful AND Mind"
}
}
} # 多fields
GET /movies/_search
{
"profile": true,
"query":{
"query_string":{
"fields":[
"title",
"year"
],
"query": "2012"
}
}
} GET /movies/_search
{
"profile":true,
"query":{
"simple_query_string":{
"query":"Beautiful +mind",
"fields":["title"]
}
}
}

ElasticSearch(四):基本搜索的更多相关文章

  1. elasticsearch的rest搜索--- 查询

    目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0   三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 四. 查询 1. 查询的官网的文档   ...

  2. Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...

  3. 笔记13:Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 1 ES基本介绍 概念介绍 Elasticsearch是一个基于Lucene库的搜索引擎.它提供了一个分布式.支持多租户的全文搜索引擎,它可 ...

  4. 畅购商城(五):Elasticsearch实现商品搜索

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 畅购商城(一):环境搭建 畅购商 ...

  5. Lucene.Net 2.3.1开发介绍 —— 四、搜索(三)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(三) Lucene有表达式就有运算符,而运算符使用起来确实很方便,但另外一个问题来了. 代码 4.3.4.1 Analyzer anal ...

  6. Lucene.Net 2.3.1开发介绍 —— 四、搜索(二)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(二) 4.3 表达式用户搜索,只会输入一个或几个词,也可能是一句话.输入的语句是如何变成搜索条件的上一篇已经略有提及. 4.3.1 观察 ...

  7. Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(一) 既然是内容筛选,或者说是搜索引擎,有索引,必然要有搜索.搜索虽然与索引有关,那也只是与索引后的文件有关,和索引的程序是无关的,因此 ...

  8. 【高德地图API】从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索

    原文:[高德地图API]从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索 摘要:地图服务,大家能想到哪些?POI搜素,输入提示,地址解析,公 ...

  9. elasticsearch实现网站搜索

    使用elasticsearch 实现网站搜索,可以支持商品搜索,筛选项过滤搜索 ,价格排序, 打分 筛选项聚合,还有其他综合排序 后续推出搜索人工干预排序,根据销量,好评率,售卖率 进行全方位的搜索实 ...

  10. CentOS 7.4 下搭建 Elasticsearch 6.3 搜索群集

    上个月 13 号,Elasticsearch 6.3 如约而至,该版本和以往版本相比,新增了很多新功能,其中最令人瞩目的莫过于集成了 X-Pack 模块.而在最新的 X-Pack 中 Elastics ...

随机推荐

  1. NoUniqueBeanDefinitionException常见异常!!

    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of ...

  2. web开发基础之HTTP协议

    HTTP协议 HTTP协议简介 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数 ...

  3. django根据已有数据库表生成model类

    django根据已有数据库表生成model类 创建一个Django项目 django-admin startproject 'xxxx' 修改setting文件,在setting里面设置你要连接的数据 ...

  4. 攻防世界(XCTF)WEB(进阶区)write up(三)

    挑着做一些好玩的ctf题 FlatScience web2 unserialize3upload1wtf.sh-150ics-04web i-got-id-200 FlatScience 扫出来的lo ...

  5. [Luogu3787] 冰精冻西瓜

    题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...

  6. 懒要懒到底,能自动的就不要手动,Hibernate正向工程完成Oracle数据库到MySql数据库转换(含字段转换、注释)

    需求描述 需求是这样的:因为我们目前的一个老项目是Oracle数据库的,这个库呢,数据库是没有注释的,而且字段名和表名都是大写风格,比如 在代码层面的po呢,以前也是没有任何注释的,但是经过这些年,大 ...

  7. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  8. CSS3、jQuery实现3D翻书动画

    使用CSS3 ,jQuery实现点击翻书动画效果,完整效果可在firefox中查看 HTML <div class="desktop"> <div class=& ...

  9. FTPClient连续读取文件

    最近在使用FTPClient连续读取ftp上的多个文件内容时,遇到了两个问题: 1. 在for循环中,FTPClient只能读取到第一个文件内容,读取第二个时遇到NPE问题. 2. 遇到程序锁死. 下 ...

  10. windows下cmd组合命令和管道命令

    组合命令:&& 管道命令:|