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. 【线性表基础】顺序表和单链表的插入、删除等基本操作【Java版】

    本文表述了线性表及其基本操作的代码[Java实现] 参考书籍 :<数据结构 --Java语言描述>/刘小晶 ,杜选主编 线性表需要的基本功能有:动态地增长或收缩:对线性表的任何数据元素进行 ...

  2. Java8新特性时间日期库DateTime API及示例

    Java8新特性的功能已经更新了不少篇幅了,今天重点讲解时间日期库中DateTime相关处理.同样的,如果你现在依旧在项目中使用传统Date.Calendar和SimpleDateFormat等API ...

  3. node与mysql的相互使用————node+mysql

    node与mysql的相互使用----node+mysql 为什么选node???因为我是个前端. 为什么选mysql???因为成熟,稳定,听说容易学. 一.mysql数据库: mysql下载和使用我 ...

  4. 购买https证书以及nginx配置https

    文章来源 运维公会:购买https证书以及nginx配置https 1.https的作用 https的全名是安全超文本传输协议,是在http的基础上增加了ssl加密协议.在信息传输的过程中,信息有可能 ...

  5. java生成32位UUID

    java生成32位UUID,具体代码如下: package com.fxsen.uuid; import java.util.UUID; /** * Copyright: Copyright (c) ...

  6. flask 微电影网站

    flask简介 轻量级web应用框架 WSGI工具箱才用Werkzeug 模版引擎则使用Jinja2 Flask使用BSD授权 1.virtualenv的使用 (1)创建虚拟环境:virtualenv ...

  7. 题解:2018级算法第二次上机 Zexal的流水线问题

    题目描述: 样例: 实现解释: 最基础的流水线调度问题,甚至没有开始和结束的值 实现方法即得出状态转移方程后完善即可,设a[][i]存储着第一二条线上各家的时间花费,t[][i]存储着i处进行线路切换 ...

  8. django中CBV

    08.13自我总结 django中CBV 一.django处理业务逻辑的两种方式 FBV (function based views):使用函数来处理业务逻辑 CBV (class based vie ...

  9. C# 委托 (一)—— 委托、 泛型委托与Lambda表达式

    C# 委托 (一)—— 委托. 泛型委托与Lambda表达式 2018年08月19日 20:46:47 wnvalentin 阅读数 2992   版权声明:此文乃博主之原创.鄙人才疏,望大侠斧正.此 ...

  10. STL的vector略解

    本文部分内容参考于这儿. vector 的基础知识,上文已经阐述地很详尽了.笔者谨给出 vector 的声明及其常用函数. 代码抬头需包含 #include<vector> using n ...