数据查询
返回所有记录
请求方法:GET,路径:/index/type/_search
1
| curl --location --request GET 'localhost:9200/blog/doc/_search'
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| { "took": 6, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "blog", "_type": "doc", "_id": "jV3J-HIBNIzRDJgGMzQj", "_score": 1.0, "_source": { "title": "java编程思想", "price": 99.99, "description": "本书赢得了全球程序员的广泛赞誉,即使是最晦涩的概念,在Bruce Eckel的文字亲和力和小而直接的编程示例面前也会化解于无形。从Java的基础语法到最高级特性(深入的面向对象概念、多线程、自动项目构建、单元测试和调试等),本书都能逐步指导你轻松掌握。", "author": "埃克尔" } } ] } }
|
其中字段含义:
- total:返回记录的条数
- hits:返回记录的数组
- max_score:最高匹配的程度
全文本查询
1 2 3 4 5 6 7 8 9
| curl --location --request POST 'http://localhost:9200/blog/doc/_search' \ --header 'Content-Type: application/json' \ --data-raw '{ "query":{ "match":{ "title":"java" } } }'
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| { "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 0.40388194, "hits": [ { "_index": "blog", "_type": "doc", "_id": "jV3J-HIBNIzRDJgGMzQj", "_score": 0.40388194, "_source": { "title": "java编程思想", "price": 99.99, "author": "埃克尔" } }, { "_index": "blog", "_type": "doc", "_id": "Jsrp_XIBKoNLJaiRi4mV", "_score": 0.35667494, "_source": { "title": "Java并发编程实战", "price": 69, "author": " Brian Goetz" } }, { "_index": "blog", "_type": "doc", "_id": "J8rq_XIBKoNLJaiRW4n4", "_score": 0.33698124, "_source": { "title": "Effective java 中文版(第2版)", "price": 52.00, "author": " [美] Joshua Bloch" } } ] } }
|
由返回的结果可以看出,elasticsearch返回了标题为java的文档,但match是模糊匹配,他会把查询的词先进行分词然后再去查询。例如下图的示例
1 2 3 4 5 6 7 8 9
| curl --location --request POST 'http://localhost:9200/blog/doc/_search' \ --header 'Content-Type: application/json' \ --data-raw '{ "query":{ "match":{ "title":"编程思想" } } }'
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| { "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 4.296419, "hits": [ { "_index": "blog", "_type": "doc", "_id": "jV3J-HIBNIzRDJgGMzQj", "_score": 4.296419, "_source": { "title": "java编程思想", "price": 99.99, "author": "埃克尔" } }, { "_index": "blog", "_type": "doc", "_id": "Jsrp_XIBKoNLJaiRi4mV", "_score": 1.3862944, "_source": { "title": "Java并发编程实战", "price": 69, "author": " Brian Goetz" } } ] } }
|
由上图结果可以看到的确是进行了分词,那如果我们有时候不想要这样查询呢?我们可以换一个关键字match_phrase
1 2 3 4 5 6 7 8 9
| curl --location --request POST 'http://localhost:9200/blog/doc/_search' \ --header 'Content-Type: application/json' \ --data-raw '{ "query":{ "match_phrase":{ "title":"编程思想" } } }'
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| { "took": 100, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 4.296419, "hits": [ { "_index": "blog", "_type": "doc", "_id": "jV3J-HIBNIzRDJgGMzQj", "_score": 4.296419, "_source": { "title": "java编程思想", "price": 99.99, "author": "埃克尔" } } ] } }
|
多字段查询
1 2 3 4 5 6 7 8 9 10 11
| { "query":{ "multi_match":{ "query":"java", "fields":[ "title", "description" ] } } }
|
语法查询
1 2 3 4 5 6 7
| { "query":{ "query_string":{ "query":"编程思想 and c++" } } }
|
字段查询