返回全部记录 html
使用 GET 方法,直接请求/Index/_search,就会返回全部记录。数据库
GET /accounts/_search
{
"took": 683,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_score": 1.0,
"_source": {
"user": "张三",
"title": "工程师",
"desc": "数据库管理,软件开发"
}
}
]
}
} 数组
上面代码中,返回结果的 took字段表示该操做的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录,里面子字段的含义以下。 elasticsearch
返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。ide
全文搜索 ui
Elastic 的查询很是特别,使用本身的查询语法,要求 GET 请求带有数据体。 spa
GET /accounts/_search
{
"query": {
"match": {
"desc": "软件"
}
}
} htm
上面代码使用 Match 查询,指定的匹配条件是desc字段里面包含"软件"这个词。返回结果以下。 ip
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.5753642,
"hits": [
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_score": 0.5753642,
"_source": {
"user": "张三",
"title": "工程师",
"desc": "数据库管理,软件开发"
}
}
]
}
} 开发
精确匹配
全面的match是模糊匹配的,若是咱们须要精确匹配,能够使用match_phrase来实现。具体见后面的示例。
分页
Elastic 默认一次返回10条结果,能够经过size字段改变这个设置。
GET /person/_search
{
"query": {
"match": {
"desc": "管理"
}
},
"size": 1
}
上面代码指定,每次只返回一条结果。
还能够经过from字段,指定位移。
GET /accounts/_search
{
"query": {
"match": {
"desc": "管理"
}
},
"from": 1,
"size": 1
}
上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。
逻辑运算
若是有多个搜索关键字, Elastic 认为它们是or关系。
GET /accounts/_search
{
"query": {
"match": {
"desc": "软件系统"
}
},
"from": 0,
"size": 1
}
上面代码搜索的是软件 or 系统。
若是要执行多个关键词的and搜索,必须使用布尔查询。
GET /accounts/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"user": "张三"
}
},
{
"match": {
"desc": "数据库"
} } ] } }}