学习用Node.js和Elasticsearch构建搜索引擎(2):一些检索命令

上一篇文章介绍了Elasticsearch的基础知识,安装和启动,并学习一个示例。 《学习用Node.js和Elasticsearch构建搜索引擎(一)》

这篇文章主要记录Elasticsearch检索的一些知识。下面所有的搜索例子都是使用上一篇文章中的示例测试的。

 

1、Elasticsearch搜索数据有两种方式。

一种方式是通过REST请求URI,发送搜索参数;

另一种是通过REST请求体,发送搜索参数。而请求体允许你包含更容易表达和可阅读的JSON格式。这个是DSL查询(Query DSL).

2、Elasticsearch搜索基本语法。

[GET|POST] http://domain.com/your_index/type1,type2/_search{?search_type=count|scan|...}

  注意,随着ES版本变化,搜索语法也有小调整。本文以5.3为准。

3、搜索例子。

1)查询单个文档

格式: get /_index/_type/_id 
例如:http://localhost:9200/library/article/57508457556e30622882ba58 
说明:查询索引是library下面的,类型是article的,_id是57508457556e30622882ba58的这个文档。
{
  "_index":"library", //索引
  "_type":"article", //类型
  "_id":"57508457556e30622882ba58", //文档id(这个id在导入时不指定会自动生成)
  "_version":1, //版本号,每次改动会+1
  "found":true, //true表示document存在
  "_source":{ //文档全部内容
    "id":"57508457556e30622882ba58",
    "title":"Adipisicing pariatur quis magna do et.",
    "journal":"quis nostrud",
    "volume":61,
    "number":11,
    "pages":"42-59"
    ...
  }
}

2)以query_string参数查询

// 查询标题title中含有pariatur的数据
get http://localhost:9200/library/article/_search?q=title:pariatur
{
  "took": 8,  //查询花费的时间,单位毫秒
  "timed_out": false, //查询是否超时
  "_shards": {   //描述分片的信息
    "total": 5,  //查询了多少个分片
    "successful": 5,  //成功的分片数量
    "failed": 0     //失败的分片数量
  },
  "hits": {  //搜索的结果
    "total": 145,  //全部的满足条件的文档数目
    "max_score": 2.8908465,  //文档最大得分
    "hits": [ 
      {
        "_index": "library",  //索引
        "_type": "article",  //类型
        "_id": "57508457556e30622882ba58", //文档id
        "_score": 2.8908465,  //文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果
        "_source": {  //文档原数据
          "id": "57508457556e30622882ba58",
          "title": "Adipisicing pariatur quis magna do et.",
          "journal": "quis nostrud",
          "volume": 61,
          "number": 11,

3)使用DSL查询,提交JSON格式参数。 

//实现上面例子一样的查询结果,可以用工具ElasticSearch Toolbox测试效果
请求:get http://localhost:9200/library/article/_search
参数:
{
  "query": {
    "match": {
      "title": "pariatur"
    }
  },
  "from": 0, //表示从第几行开始(默认0) 
  "size": 10 //表示查询多少条文档(默认10)
} 
//注意:如果搜索size大于10000,需要设置index.max_result_window参数,size的大小不能超过index.max_result_window这个参数的设置,默认为10,000。

4)多条件查询 bool:must、filter、should

官方例子是:Query-DSL-bool-query

{
  "query": {
    "bool" : {  //成为query的过滤器,还有其它的,如:and,or,not,limit
      "must" : {  //must,filter,should为过滤条件,如果有多个子条件,使用[]
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,  //用于现在should的子条件匹配个数。
      "boost" : 1.0 //表示此过滤器的权重,默认1.0
    }
  }
}

由于 Query-DSL 查询语言过于复杂,关键字非常多,需要再用到时查询文档。

官网帮助文档地址 :https://www.elastic.co/guide/en/elasticsearch/reference/5.3/index.html

 

posted @ 2017-04-01 13:51  光尘9022  阅读(984)  评论(0编辑  收藏  举报