Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎,可以用于搜索各种文档,并且这种搜索引擎也被很多企业所使用,那elasticsearch的数据库是什么类型?下面来我们就来给大家讲解一下。
Elasticsearch 是 面向文档型数据库,这意味着它存储的是整个对象或者 文档,它不但会存储它们,还会为他们建立索引,这样你就可以搜索他们了。你可以在 Elasticsearch 中索引、搜索、排序和过滤这些文档。不需要成行成列的数据。这将会是完全不同的一种面对数据的思考方式,这也是为什么 Elasticsearch 可以执行复杂的全文搜索的原因。
elasticsearch如何进行基本查询?
1.语法:
GET / 索引库名 / _search { "query": { "查询类型": { "查询条件": "查询条件值" } } }
查询类型:match_all,match,term,range,fuzzy,bool 等等
查询条件:查询条件会根据类型的不同,写法也有差异
1.1 查询所有(match_all)
查询指令:
GET / shop / _search { "query": { "match_all": {} } }
查询结果:
{ "took": 13 , "timed_out": false , "_shards": { "total": 3 , "successful": 3 , "skipped": 0 , "failed": 0 } , "hits": { "total": 4 , "max_score": 1 , "hits": [ { "_index": "shop" , "_type": "goods" , "_id": "P4EGtmgBBdkQnU_d8b7I" , "_score": 1 , "_source": { "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)" , "price": 8599 , "stock": 1900 } }, { "_index": "shop" , "_type": "goods" , "_id": "QIEGtmgBBdkQnU_d-r6T" , "_score": 1 , "_source": { "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器" , "price": 109 , "stock": 9970 } }, { "_index": "shop" , "_type": "goods" , "_id": "PYEGtmgBBdkQnU_d4b4m" , "_score": 1 , "_source": { "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待" , "price": 2299 , "stock": 8800 } }, { "_index": "shop" , "_type": "goods" , "_id": "PoEGtmgBBdkQnU_d6b4Q" , "_score": 1 , "_source": { "title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机" , "price": 4999 , "stock": 5600 } } ] } } 对查询结果进行分页 GET / shop / _search { "query": { "match_all": {} } , "from": 1 , "size": 2 }
1.2 匹配查询(match)
or 关系:会把查询条件进行分词,然后进行查询,多个词条之间是or的关系
查询指令:
GET / shop / _search { "query": { "match": { "title": { "query": "小米手机" , "operator": "or" } } } }
查询结果:
{ "took": 3 , "timed_out": false , "_shards": { "total": 3 , "successful": 3 , "skipped": 0 , "failed": 0 } , "hits": { "total": 2 , "max_score": 1.6051829 , "hits": [ { "_index": "demo" , "_type": "goods" , "_id": "MIHNs2gBBdkQnU_d_r6o" , "_score": 1.6051829 , "_source": { "title": "华为手机" , "price": "2199.00" } }, { "_index": "demo" , "_type": "goods" , "_id": "LYHJs2gBBdkQnU_dt75n" , "_score": 0.2876821 , "_source": { "title": "小米手机" , "price": "1699.00" } } ] } }
and关系:会把查询条件进行分词,然后进行查询,多个词条之间是and的关系
查询指令:
GET / shop / _search { "query": { "match": { "title": { "query": "小米手机" , "operator": "and" } } } }
查询结果:
{ "took": 10 , "timed_out": false , "_shards": { "total": 3 , "successful": 3 , "skipped": 0 , "failed": 0 } , "hits": { "total": 1 , "max_score": 0.92355466 , "hits": [ { "_index": "shop" , "_type": "goods" , "_id": "PYEGtmgBBdkQnU_d4b4m" , "_score": 0.92355466 , "_source": { "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待" , "price": 2299 , "stock": 8800 } } ] } }
1.3 词条查询
单值查询(term)
查询指令:
GET / shop / _search { "query": { "term": { "price": 109.00 } } }
查询结果:
{ "took": 3 , "timed_out": false , "_shards": { "total": 3 , "successful": 3 , "skipped": 0 , "failed": 0 } , "hits": { "total": 1 , "max_score": 1 , "hits": [ { "_index": "shop" , "_type": "goods" , "_id": "QIEGtmgBBdkQnU_d-r6T" , "_score": 1 , "_source": { "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器" , "price": 109 , "stock": 9970 } } ] } }
多值查询(terms)
查询指令:
GET / shop / _search { "query": { "terms": { "price": [8599.00, 109.00] } } }
查询结果:
{ "took": 15 , "timed_out": false , "_shards": { "total": 3 , "successful": 3 , "skipped": 0 , "failed": 0 } , "hits": { "total": 2 , "max_score": 1 , "hits": [ { "_index": "shop" , "_type": "goods" , "_id": "P4EGtmgBBdkQnU_d8b7I" , "_score": 1 , "_source": { "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)" , "price": 8599 , "stock": 1900 } }, { "_index": "shop" , "_type": "goods" , "_id": "QIEGtmgBBdkQnU_d-r6T" , "_score": 1 , "_source": { "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器" , "price": 109 , "stock": 9970 } } ] } }
elasticsearch本就是一个分布式多用户能力的全文搜索引擎,因此对于elasticsearch的基本搜索查询我们一定要会,并且elasticsearch提供可扩展的搜索,具有接近实时的搜索,并支持多租户。最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。
推荐阅读: