博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES10-检索入门
阅读量:6293 次
发布时间:2019-06-22

本文共 5876 字,大约阅读时间需要 19 分钟。

hot3.png

1.创建索引,准备数据

定义索引结构

DELETE telegraphPUT telegraph{  "mappings": {    "msg":{      "properties": {        "title":{          "type": "text",          "analyzer": "ik_max_word"        },        "content":{          "type": "text",          "analyzer": "ik_max_word"        },        "author":{          "type": "text"        },        "pubdate":{          "type": "date",          "format": "date_hour_minute_second"        }      }    }  }}

批量加入测试数据

POST _bulk{"index":{"_index":"telegraph","_type":"msg"}}{"title":"宝泰隆:半年报预增140%-156%","content":"公司主要产品焦炭、甲醇销售量及销售价格较上年同期有较大的上涨","author":"宝泰隆","pubdate":"2018-07-17T17:16:30"}{"index":{"_index":"telegraph","_type":"msg"}}{"title":"周五召开董事会会议 审议及批准更新后的一季报","content":"以审议及批准更新后的2018年第一季度报告","author":"中兴通讯","pubdate":"2018-07-17T12:33:11"}{"index":{"_index":"telegraph","_type":"msg"}}{"title":"长生生物再次跌停 三机构抛售近1000万元","content":"长生生物再次一字跌停,报收19.89元,成交1432万元","author":"长生生物","pubdate":"2018-07-17T10:03:11"}{"index":{"_index":"telegraph","_type":"msg"}}{"title":"碧桂园集团副主席杨惠妍","content":"杨惠妍分别于7月10日、11日买入碧桂园1000万股、1500万股","author":"小财注","pubdate":"2018-07-17T16:12:55"}{"index":{"_index":"telegraph","_type":"msg"}}{"title":"河北聚焦十大行业推进国际产能合作","content":"河北省政府近日出台积极参与“一带一路”建设推进国际产能合作实施方案","author":"财联社","pubdate":"2018-07-17T14:14:55"}

2.term查询

GET telegraph/_search{  "query": {    "term": {      "title": {        "value": "主席"      }    }  }}

查询结果

{  "took": 5,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 1,    "max_score": 0.2876821,    "hits": [      {        "_index": "telegraph",        "_type": "msg",        "_id": "A5etp2QBW8hrYY3zGJk7",        "_score": 0.2876821,        "_source": {          "title": "碧桂园集团副主席杨惠妍",          "content": "杨惠妍分别于7月10日、11日买入碧桂园1000万股、1500万股",          "author": "小财注",          "pubdate": "2018-07-17T16:12:55"        }      }    ]  }}

3.分页

from:起始行

size:返回条数

GET telegraph/_search{  "from": 0,  "size": 3,  "query": {    "match_all": {}  }}

查询结果

{  "took": 6,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 5,    "max_score": 1,    "hits": [      {        "_index": "telegraph",        "_type": "msg",        "_id": "AZetp2QBW8hrYY3zGJk7",        "_score": 1,        "_source": {          "title": "周五召开董事会会议 审议及批准更新后的一季报",          "content": "以审议及批准更新后的2018年第一季度报告",          "author": "中兴通讯",          "pubdate": "2018-07-17T12:33:11"        }      },      {        "_index": "telegraph",        "_type": "msg",        "_id": "A5etp2QBW8hrYY3zGJk7",        "_score": 1,        "_source": {          "title": "碧桂园集团副主席杨惠妍",          "content": "杨惠妍分别于7月10日、11日买入碧桂园1000万股、1500万股",          "author": "小财注",          "pubdate": "2018-07-17T16:12:55"        }      },      {        "_index": "telegraph",        "_type": "msg",        "_id": "AJetp2QBW8hrYY3zGJk7",        "_score": 1,        "_source": {          "title": "宝泰隆:半年报预增140%-156%",          "content": "公司主要产品焦炭、甲醇销售量及销售价格较上年同期有较大的上涨",          "author": "宝泰隆",          "pubdate": "2018-07-17T17:16:30"        }      }    ]  }}

4.过滤字段

指定只需要返回的字段值

GET telegraph/_search{  "_source": ["title","content"],  "query": {    "term": {      "title": {        "value": "主席"      }    }  }}

查询结果

{  "took": 13,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 1,    "max_score": 0.2876821,    "hits": [      {        "_index": "telegraph",        "_type": "msg",        "_id": "A5etp2QBW8hrYY3zGJk7",        "_score": 0.2876821,        "_source": {          "title": "碧桂园集团副主席杨惠妍",          "content": "杨惠妍分别于7月10日、11日买入碧桂园1000万股、1500万股"        }      }    ]  }}

5.显示version

设置version字段为true,显示文档版本号

GET telegraph/_search{  "_source": "title",  "version": true,   "query": {    "term": {      "title": {        "value": "主席"      }    }  }}

查询结果

{  "took": 8,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 1,    "max_score": 0.2876821,    "hits": [      {        "_index": "telegraph",        "_type": "msg",        "_id": "A5etp2QBW8hrYY3zGJk7",        "_version": 1,        "_score": 0.2876821,        "_source": {          "title": "碧桂园集团副主席杨惠妍"        }      }    ]  }}

6.评分过滤

过滤满足最小评分的文档

GET telegraph/_search{  "min_score":"0.2",  "query": {    "term": {      "title": {        "value": "碧桂园"      }    }  }}

查询结果

{  "took": 5,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 1,    "max_score": 0.2876821,    "hits": [      {        "_index": "telegraph",        "_type": "msg",        "_id": "A5etp2QBW8hrYY3zGJk7",        "_score": 0.2876821,        "_source": {          "title": "碧桂园集团副主席杨惠妍",          "content": "杨惠妍分别于7月10日、11日买入碧桂园1000万股、1500万股",          "author": "小财注",          "pubdate": "2018-07-17T16:12:55"        }      }    ]  }}

7.高亮关键字

设置属性,且该属性中有对应查询条件的关键字时高亮显示。

GET telegraph/_search{  "query": {    "term": {      "title": {        "value": "会议"      }    }  },  "highlight": {    "fields": {      "title": {}    }  }}

查询结果

{  "took": 9,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped": 0,    "failed": 0  },  "hits": {    "total": 1,    "max_score": 0.2876821,    "hits": [      {        "_index": "telegraph",        "_type": "msg",        "_id": "AZetp2QBW8hrYY3zGJk7",        "_score": 0.2876821,        "_source": {          "title": "周五召开董事会会议 审议及批准更新后的一季报",          "content": "以审议及批准更新后的2018年第一季度报告",          "author": "中兴通讯",          "pubdate": "2018-07-17T12:33:11"        },        "highlight": {          "title": [            "周五召开董事会会议 审议及批准更新后的一季报"          ]        }      }    ]  }}

 

转载于:https://my.oschina.net/u/3100849/blog/1858369

你可能感兴趣的文章
SHELL实现跳板机,只允许用户执行少量允许的命令
查看>>
SpringBoot 整合Redis
查看>>
2014上半年大片早知道
查看>>
Android 6.0指纹识别App开发案例
查看>>
正文提取算法
查看>>
轻松学PHP
查看>>
Linux中的网络监控命令
查看>>
this的用法
查看>>
windows下安装redis
查看>>
CentOS7 yum 安装git
查看>>
启动日志中频繁出现以下信息
查看>>
httpd – 对Apache的DFOREGROUND感到困惑
查看>>
分布式锁的一点理解
查看>>
idea的maven项目,install下载重复下载本地库中已有的jar包,而且下载后jar包都是lastupdated问题...
查看>>
2019测试指南-web应用程序安全测试(二)指纹Web服务器
查看>>
树莓派3链接wifi
查看>>
js面向对象编程
查看>>
Ruby中类 模块 单例方法 总结
查看>>
jQuery的validate插件
查看>>
5-4 8 管道符 作业控制 shell变量 环境变量配置
查看>>