manual/chinese/Searching/Search_results.md
当你通过SQL在MySQL协议下运行查询时,你将收到请求的列作为结果,或者如果未找到任何内容,则为空的结果集。
<!-- request SQL -->SELECT * FROM tbl;
+------+------+--------+
| id | age | name |
+------+------+--------+
| 1 | 25 | joe |
| 2 | 25 | mary |
| 3 | 33 | albert |
+------+------+--------+
3 rows in set (0.00 sec)
POST /search
{
"table": "tbl"
}
{
"took": 0,
"timed_out": false,
"hits": {
"total": 3,
"total_relation": "eq",
"hits": [
{
"_id": 1,
"_score": 2500,
"_source": {
"age": 25,
"name": "joe"
}
},
{
"_id": 2,
"_score": 2500,
"_source": {
"age": 25,
"name": "mary"
}
},
{
"_id": 3,
"_score": 2500,
"_source": {
"age": 33,
"name": "albert"
}
}
]
}
}
此外,你还可以使用 SHOW META 调用来查看最新查询的额外元信息。
<!-- request SQL -->SELECT id,story_author,comment_author FROM hn_small WHERE story_author='joe' LIMIT 3; SHOW META;
++--------+--------------+----------------+
| id | story_author | comment_author |
+--------+--------------+----------------+
| 152841 | joe | SwellJoe |
| 161323 | joe | samb |
| 163735 | joe | jsjenkins168 |
+--------+--------------+----------------+
3 rows in set (0.01 sec)
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| total | 3 |
| total_found | 20 |
| total_relation | gte |
| time | 0.010 |
+----------------+-------+
4 rows in set (0.00 sec)
POST /sql?mode=raw -d "SELECT id,f1,f2 FROM t WHERE f2=2 LIMIT 1; SHOW META"
[
{
"columns": [
{
"id": {
"type": "long long"
}
},
{
"f1": {
"type": "string"
}
},
{
"f2": {
"type": "long"
}
}
],
"data": [
{
"id": 724024784404348900,
"f1": "b",
"f2": 2
}
],
"total": 1,
"error": "",
"warning": ""
},
{
"columns": [
{
"Variable_name": {
"type": "string"
}
},
{
"Value": {
"type": "string"
}
}
],
"data": [
{
"Variable_name": "total",
"Value": "1"
},
{
"Variable_name": "total_found",
"Value": "1"
},
{
"Variable_name": "total_relation",
"Value": "gte"
},
{
"Variable_name": "time",
"Value": "0.000"
}
],
"total": 4,
"error": "",
"warning": ""
}
]
在某些情况下,例如执行 分面搜索 时,你可能会收到多个结果集作为对SQL查询的响应。
<!-- data for the following examples: DROP TABLE IF EXISTS tbl; CREATE TABLE tbl(id bigint, age int, name string); INSERT INTO tbl (id, age, name) VALUES (1, 25, 'joe'), (2, 25, 'mary'), (3, 33, 'albert'); --> <!-- request SQL -->SELECT * FROM tbl WHERE MATCH('joe') FACET age;
+------+------+
| id | age |
+------+------+
| 1 | 25 |
+------+------+
1 row in set (0.00 sec)
+------+----------+
| age | count(*) |
+------+----------+
| 25 | 1 |
+------+----------+
1 row in set (0.00 sec)
POST /sql -d "SELECT * FROM tbl WHERE MATCH('joe') FACET age"
{
"took": 0,
"timed_out": false,
"hits": {
"total": 1,
"total_relation": "eq",
"hits": [
{
"_score": 2500,
"_source": {
"name": "joe"
}
}
]
},
"aggregations": {
"rating": {
"buckets": [
{
"key": 1,
"doc_count": 1
}
]
}
}
}
如果出现警告,结果集将包含警告标志,你可以使用 SHOW WARNINGS 查看警告。
<!-- request SQL -->SELECT * from tbl where match('"joe"/3'); show warnings;
+------+------+------+
| id | age | name |
+------+------+------+
| 1 | 25 | joe |
+------+------+------+
1 row in set, 1 warning (0.00 sec)
+---------+------+--------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------------------------+
| warning | 1000 | quorum threshold too high (words=1, thresh=3); replacing quorum operator with AND operator |
+---------+------+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
POST /sql?mode=raw -d "SELECT * from t where match('\"a\"/3'); show warnings"
[
{
"columns": [
{
"id": {
"type": "long long"
}
},
{
"f2": {
"type": "long"
}
},
{
"f1": {
"type": "string"
}
}
],
"data": [],
"total": 0,
"error": "",
"warning": ""
},
{
"columns": [
{
"Level": {
"type": "string"
}
},
{
"Code": {
"type": "decimal"
}
},
{
"Message": {
"type": "string"
}
}
],
"data": [
{
"Level": "warning",
"Code": "1000",
"Message": "table t: quorum threshold too high (words=1, thresh=3); replacing quorum operator with AND operator"
}
],
"total": 1,
"error": "",
"warning": ""
}
]
如果查询失败,你将收到一个错误:
<!-- request SQL -->SELECT * from tbl where match('@surname joe');
ERROR 1064 (42000): index idx: query error: no field 'surname' found in schema
POST /sql -d "SELECT * from t where match('@surname joe')"
{
"error": "table t: query error: no field 'surname' found in schema"
}
通过HTTP JSON接口,查询结果将以JSON文档的形式发送。示例:
{
"took":10,
"timed_out": false,
"hits":
{
"total": 2,
"hits":
[
{
"_id": 1,
"_score": 1,
"_source": { "gid": 11 }
},
{
"_id": 2,
"_score": 1,
"_source": { "gid": 12 }
}
]
}
}
took:执行搜索所花费的毫秒数timed_out:查询是否超时hits:搜索结果,具有以下属性:
total:匹配文档的总数hits:包含匹配项的数组查询结果还可以包括查询剖析信息。请参阅 查询剖析。
hits数组中的每个匹配项具有以下属性:
_id:匹配ID_score:匹配权重,由排名器计算_source:包含此匹配项属性的数组默认情况下,所有属性都会返回在 _source 数组中。你可以在请求负载中使用 _source 属性来选择你想要包含在结果集中的字段。示例:
{
"table":"test",
"_source":"attr*",
"query": { "match_all": {} }
}
你可以指定你想要包含在查询结果中的属性作为字符串("_source": "attr*")或作为字符串数组("_source": [ "attr1", "attri*" ]")。每个条目可以是属性名或通配符(*,% 和 ? 符号受支持)。
你也可以显式指定你想要包含和排除的属性,使用 includes 和 excludes 属性:
"_source":
{
"includes": [ "attr1", "attri*" ],
"excludes": [ "*desc*" ]
}
空的 includes 列表被解释为“包含所有属性”,而空的 excludes 列表不匹配任何内容。如果一个属性同时匹配 includes 和 excludes,则 excludes 会获胜。