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": [ "周五召开董事会会议 审议及批准更新后的一季报" ] } } ] }}