Elasticsearch 2.20 搜索详解

TodThurston 8年前

来自: http://my.oschina.net/secisland/blog/614690


    之前的文章中简单介绍过搜索,在这里对搜索进行详细的介绍。在之前的介绍中可以知道,搜索有两种方式,一种是通过url参数进行搜索,另一种是通过POST请求参数进行搜索。

url参数搜索

请求:GET http://localhost:9200/secilog/log/_search?参数,多个参数用&分来。

参数详解:

参数 解释
q 查询字符串,例如:q=syslog
df 查询中没有定义前缀的时候默认使用的字段
analyzer 当分析查询字符串的时候使用的分词器
lowercase_expanded_terms 搜索的时候忽略大小写标志,默认为true
analyze_wildcard 通配符或者前缀查询是否被分析,默认false
default_operator 默认多个条件的关系,AND或者OR,默认OR
lenient 如果设置为true,字段类型转换失败的时候将被忽略,默认为false
explain 在每个返回结果中,将包含评分机制的解释
_source 是否包含元数据,同时支持_source_include 和_source_exclude
fields 只放回索引中指定的列,多个列中间用逗号分开
sort 排序,例如fieldName:asc或者fieldName:desc
track_scores 评分轨迹,当排序的时候,设置为true的时候返回评分的信息
timeout 超时的时间设置
terminate_after 在每个分片中查询的最大条数,如果设置返回结果中会有一个terminated_early字段
from 开始的记录数
size 搜索结果中的条数
search_type 搜索的类型,可以是dfs_query_then_fetch,query_then_fetch,默认query_then_fetch

POST请求参数

请求:POST http://localhost:9200/secilog/log/_search

参数在请求头中。

参数是JSON格式的查询领域语法(query dsl)。例如:

{      "query" : {          "term" : { "type" : "syslog" }      }  }

返回的结果:

{    "took" : 2,    "timed_out" : false,    "_shards" : {      "total" : 1,      "successful" : 1,      "failed" : 0    },    "hits" : {      "total" : 1,      "max_score" : 1.0,      "hits" : [ {        "_index" : "secilog",        "_type" : "log",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "type" : "syslog",          "message" : "secilog test test test "        }      } ]    }  }

    如果需要搜索分页,可以通过from size组合来进行。from表示从第几行开始,size表示查询多少条文档。from默认为0,size默认为10,例如:

{      "from" : 0, "size" : 10,      "query" : {          "term" : { "type" : "syslog" }      }  }

注意:size的大小不能超过index.max_result_window这个参数的设置,默认为10,000。

排序sort

    可以通过一个或者多个字段进行排序。例如:

{  "sort" : [           {"type" :{"order": "asc"}},           {"message" :{"order": "asc"}},      ]  }

返回的结果为:

{    "took" : 2,    "timed_out" : false,    "_shards" : {      "total" : 1,      "successful" : 1,      "failed" : 0    },    "hits" : {      "total" : 2,      "max_score" : null,      "hits" : [ {        "_index" : "secilog",        "_type" : "log",        "_id" : "2",        "_score" : null,        "_source" : {          "type" : "file",          "message" : "Another secilog test "        },        "sort" : [ "file", "test" ]      }, {        "_index" : "secilog",        "_type" : "log",        "_id" : "1",        "_score" : null,        "_source" : {          "type" : "syslog",          "message" : "secilog test test test "        },        "sort" : [ "syslog", "test" ]      } ]    }  }

    当一个字段的内容有多个值的时候,系统支持一些计算进行排序,包括min,max,sum,avg,median(中间值),例如下面的请求表示order有多个值,取平均值排序:

{     "query" : {      ...     },     "sort" : [        {"price" : {"order" : "asc", "mode" : "avg"}}     ]}

数据列过滤

    数据列过滤允许在查询的时候不显示原始数据或者显示部分原始字段。例如不显示原始文档:

{      "_source": false,      "query" : {          "term" : { "type" : "syslog" }      }}

例如显示部分文档列:

{      "_source": "obj.*",      "query" : {          "term" : { "type" : "syslog" }      }  }
{      "_source": [ "obj1.*", "obj2.*" ],      "query" : {          "term" : { "type" : "syslog" }      }  }

例如可以包含或者排除某些列:

{      "_source": {          "include": [ "obj1.*", "obj2.*" ],          "exclude": [ "*.description" ]      },      "query" : {           "term" : { "type" : "syslog" }      }  }

脚本支持

同样对搜索也支持脚本,例如:

请求:POST http://localhost:9200/secilog/log/_search?pretty

参数:

{      "query": {          "term": {              "type": "syslog"          }      },       "script_fields": {          "test1": {              "script": "doc['type'].value * 2"          }      }  }

得到返回结果:

{    "took" : 16,    "timed_out" : false,    "_shards" : {      "total" : 1,      "successful" : 1,      "failed" : 0    },    "hits" : {      "total" : 1,      "max_score" : 1.0,      "hits" : [ {        "_index" : "secilog",        "_type" : "log",        "_id" : "1",        "_score" : 1.0,        "fields" : {          "test1" : [ "syslogsyslog" ]        }      } ]    }  }

    赛克蓝德(secisland)后续会逐步对Elasticsearch的最新版本的各项功能进行分析,近请期待。也欢迎加入secisland公众号进行关注