ES10-检索入门

1.创建索引,准备数据

定义索引结构

DELETE telegraph

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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值