您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
elasticSearch搜索与_search端点
发布时间:2021-08-17 12:34:24编辑:雪饮阅读()
搜索
在前面,已经介绍了在ElasticSearch索引中处理数据的基础知识,现在是时候进行核心功能的学习了。考虑到之前我们删除索引中的所有文档,所以,在进行搜索学习之前,需要一些添加一些示例数据。使用以下这些请求和数据对象来创建索引。
curl -XPUT "http://localhost:9200/movies/movie/1" -H "Content-Type: application/json" -d "{\"title\":\"The Godfather\",\"director\":\"Francis Ford Coppola\",\"year\":1972,\"genres\":[\"Crime\",\"Drama\"]}"
curl -XPUT "http://localhost:9200/movies/movie/2" -H "Content-Type: application/json" -d "{\"title\":\"Lawrence of Arabia\",\"director\":\"David Lean\",\"year\":1962,\"genres\":[\"Adventure\",\"Biography\",\"Drama\"]}"
curl -XPUT "http://localhost:9200/movies/movie/3" -H "Content-Type: application/json" -d "{\"title\":\"To Kill a Mockingbird\",\"director\":\"Robert Mulligan\",\"year\":1962,\"genres\":[\"Crime\",\"Drama\",\"Mystery\"]}"
curl -XPUT "http://localhost:9200/movies/movie/4" -H "Content-Type: application/json" -d "{\"title\":\"Apocalypse Now\",\"director\":\"Francis Ford Coppola\",\"year\":1979,\"genres\":[\"Drama\",\"War\"]}"
curl -XPUT "http://localhost:9200/movies/movie/5" -H "Content-Type: application/json" -d "{\"title\":\"Kill Bill: Vol. 1\",\"director\":\"Quentin Tarantino\",\"year\":2003,\"genres\":[\"Action\",\"Crime\",\"Thriller\"]}"
curl -XPUT "http://localhost:9200/movies/movie/6" -H "Content-Type: application/json" -d "{\"title\":\"The Assassination of Jesse James by the Coward Robert Ford\",\"director\":\"Andrew Dominik\",\"year\":2007,\"genres\":[\"Biography\",\"Crime\",\"Drama\"]}"
值得指出的是,ElasticSearch具有和端点(_bulk)用于用单个请求索引多个文档,但是这超出了本教程的范围,这里只保持简单,使用六个单独的请求学习。
_search端点
现在已经把一些电影信息放入了索引,可以通过搜索看看是否可找到它们。 为了使用ElasticSearch进行搜索,我们使用_search端点,可选择使用索引和类型。也就是说,按照以下模式向URL发出请求:<index>/<type>/_search。其中,index和type都是可选的。
换句话说,为了搜索电影,可以对以下任一URL进行POST请求:
http://localhost:9200/_search - 搜索所有索引和所有类型。
http://localhost:9200/movies/_search - 在电影索引中搜索所有类型
http://localhost:9200/movies/movie/_search - 在电影索引中显式搜索电影类型的文档。
因为我们只有一个单一的索引和单一的类型,所以怎么使用都不会有什么问题。为了简洁起见使用第一个URL。
关键字词:elasticSearch,搜索,_search,_search端点