Elasticsearch(六)| search(搜索)的命令行
有了数据就可以开始搜索。
官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
准备测试数据
为了进行检索,插入一些测试数据:
curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国","date":"2021-10-16T13:12:00", "counts":2}'
curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国万岁","date":"2021-11-15T13:12:00", "counts":3}'
curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国国旗","date":"2021-12-15T13:12:00", "counts":4}'
返回所有数据
获取索引下所有的数据:
curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty -H 'Content-Type: application/json' -d '{"query":{"match_all":{}}}'
如果成功返回下面内容
{
"took" : 552,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_record",
"_id" : "pzK5HIQB8xpN1XtdHm7Z",
"_score" : 1.0,
"_source" : {
"mappings" : {
"properties" : {
"key" : "中华人民共和国国歌",
"date" : "2021-10-15T13:12:00",
"counts" : 1
}
}
}
},
...
]
}
}
过滤返回
有时候只需要返回部分字段,可以使用下面命令:
curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty -H 'Content-Type: application/json' -d '{"query":{"match_all":{}},"_source":["key"]}'
如果成功返回下面内容
{
"took" : 199,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_record",
"_id" : "pzK5HIQB8xpN1XtdHm7Z",
"_score" : 1.0,
"_source" : {
"key" : "中华人民共和国国歌"
}
},
{
"_index" : "test_record",
"_id" : "qDK5HIQB8xpN1Xtdcm4P",
"_score" : 1.0,
"_source" : {
"key" : "中华人民共和国"
}
},
{
"_index" : "test_record",
"_id" : "qTK5HIQB8xpN1Xtdm245",
"_score" : 1.0,
"_source" : {
"key" : "中华人民共和国万岁"
}
},
{
"_index" : "test_record",
"_id" : "qjK5HIQB8xpN1Xtdy25q",
"_score" : 1.0,
"_source" : {
"key" : "中华人民共和国国旗"
}
}
]
}
}
关键字搜索
通过关键字进行检索:
curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty -H 'Content-Type: application/json' -d '{"query":{"match":{"key":"国歌"}}}'
如果成功返回下面内容
{
"took" : 82,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3260206,
"hits" : [
{
"_index" : "test_record",
"_id" : "pzK5HIQB8xpN1XtdHm7Z",
"_score" : 1.3260206,
"_source" : {
"key" : "中华人民共和国国歌",
"date" : "2021-10-15T13:12:00",
"counts" : 1
}
}
]
}
}
0