Elasticsearch搜索文档

网友投稿 241 2022-11-17

Elasticsearch搜索文档

请求路径:/索引/_search 请求方式:GET

{ "query":{ 搜索方式:搜索参数 } }

1. 搜索方式

match_all:查询所有数据搜索参数: {}match:全文检索。将查询条件分词后再进行搜索。搜索参数: { 搜索字段:搜索条件 }match_phrase:短语检索。搜索条件不做任何分词解析,在搜索字对应的倒排索引中精确匹配。搜索参数: { 搜索字段:搜索条件 }range:范围搜索。对数字类型的字段进行范围搜索搜索参数: { 搜索字段:{ “gte”:最小值, “lte”:最大值 } }gt/lt:大于/小于 gte/lte:大于等于/小于等于term/terms:单词/词组搜索。搜索条件不做任何分词解析,在搜索字对应的倒排索引中精确匹配term参数: { 搜索字段: 搜索条件 }terms参数: { 搜索字段: [搜索条件1,搜索条件2] }

补充:在搜索时关键词有可能会输入错误,ES搜索提供了自动纠错功能,即ES的模糊查询。使用 match方式可以实现模糊查询。模糊查询对中文的支持效果一般。

{ "query": { "match": { "域名": { "query": 搜索条件, "fuzziness": 最多错误字符数,不能超过2 } } } }

2.复合搜索

路径: /索引/_search 请求方式:GET 请求体:

{ "query": { "bool": { // 必须满足的条件 "must": [ 搜索方式:搜索参数, 搜索方式:搜索参数 ], // 多个条件有任意一个满足即可 "should": [ 搜索方式:搜索参数, 搜索方式:搜索参数 ], // 必须不满足的条件 "must_not":[ 搜索方式:搜索参数, 搜索方式:搜索参数 ] } } }

3.结果排序

ES中默认使用相关度分数实现排序,可以通过搜索语法定制化排序。 请求体:

{ "query": 搜索条件, "sort": [ { "字段1":{ "order":"asc" } }, { "字段2":{ "order":"desc" } } ] }例如:GET /people/_search{ "query": { "bool": { "must": [ { "match_phrase": { "desc": "NBA" } }, { "match_phrase": { "desc": "运动员" } } ] } }, "sort": [ { "id": { "order": "asc" } } ]}//排序结果{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "people", "_type" : "_doc", "_id" : "1", "_score" : null, "_source" : { "id" : 1001, "name" : "湖人俱乐部的戴维斯", "desc" : "戴维斯是NBA最伟大的运动员之一" }, "sort" : [ 1001 ] }, { "_index" : "people", "_type" : "_doc", "_id" : "2", "_score" : null, "_source" : { "id" : 1002, "name" : "湖人俱乐部的科比", "desc" : "科比是NBA最伟大的运动员之一" }, "sort" : [ 1002 ] }, { "_index" : "people", "_type" : "_doc", "_id" : "3", "_score" : null, "_source" : { "id" : 1003, "name" : "湖人俱乐部的詹姆斯", "desc" : "詹姆斯是NBA最伟大的运动员之一" }, "sort" : [ 1003 ] }, { "_index" : "people", "_type" : "_doc", "_id" : "4", "_score" : null, "_source" : { "id" : 1004, "name" : "湖人俱乐部的奥尼尔", "desc" : "奥尼尔是NBA最伟大的运动员之一" }, "sort" : [ 1004 ] } ] }}

注意: 由于ES对text 类型字段数据会做分词处理,使用哪一个单词做排序都是不合理的,所以 ES中默认 不允许对text 类型的字段做排序。如果需要使用字符串做结果排序,可以使用 keyword 类型的字 段作为排序依据,因为 keyword 字段不做分词处理。

4.分页查询

请求体:

{ "query": 搜索条件, "from": 起始下标, "size": 查询记录数 }//例如GET /people/_search{ "query": { "match_all": {} }, "sort": [ { "id": { "order": "desc" } } ], "from": 0, "size": 3}//分页结果{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 5, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "people", "_type" : "_doc", "_id" : "4", "_score" : null, "_source" : { "id" : 1004, "name" : "湖人俱乐部的奥尼尔", "desc" : "奥尼尔是NBA最伟大的运动员之一" }, "sort" : [ 1004 ] }, { "_index" : "people", "_type" : "_doc", "_id" : "5", "_score" : null, "_source" : { "id" : 1003, "name" : "tom", "desc" : "tom is the best player" }, "sort" : [ 1003 ] }, { "_index" : "people", "_type" : "_doc", "_id" : "3", "_score" : null, "_source" : { "id" : 1003, "name" : "湖人俱乐部的詹姆斯", "desc" : "詹姆斯是NBA最伟大的运动员之一" }, "sort" : [ 1003 ] } ] }}

5.高亮查询

{ "query":搜索条件, "highlight":{ "fields": { "高亮显示的字段名": { // 返回高亮数据的最大长度 "fragment_size":100, // 返回结果最多可以包含几段不连续的文字 "number_of_fragments":5 } }, "pre_tags":["前缀"], "post_tags":["后缀"] } }//例如GET /people/_search{ "query": { "match": { "desc": "运动员" } }, "highlight": { "fields": { "desc":{ "fragment_size": 20, "number_of_fragments": 5 } }, "pre_tags": [""], "post_tags": [""] }}//高亮测试结果{ "took" : 184, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 0.8559005, "hits" : [ { "_index" : "people", "_type" : "_doc", "_id" : "2", "_score" : 0.8559005, "_source" : { "id" : 1002, "name" : "湖人俱乐部的科比", "desc" : "科比是NBA最伟大的运动员之一" }, "highlight" : { "desc" : [ "科比是NBA最伟大的运动员之一" ] } }, { "_index" : "people", "_type" : "_doc", "_id" : "3", "_score" : 0.8218762, "_source" : { "id" : 1003, "name" : "湖人俱乐部的詹姆斯", "desc" : "詹姆斯是NBA最伟大的运动员之一" }, "highlight" : { "desc" : [ "詹姆斯是NBA最伟大的运动员之一" ] } }, { "_index" : "people", "_type" : "_doc", "_id" : "1", "_score" : 0.79045355, "_source" : { "id" : 1001, "name" : "湖人俱乐部的戴维斯", "desc" : "戴维斯是NBA最伟大的运动员之一" }, "highlight" : { "desc" : [ "戴维斯是NBA最伟大的运动员之一" ] } }, { "_index" : "people", "_type" : "_doc", "_id" : "4", "_score" : 0.76134515, "_source" : { "id" : 1004, "name" : "湖人俱乐部的奥尼尔", "desc" : "奥尼尔是NBA最伟大的运动员之一" }, "highlight" : { "desc" : [ "奥尼尔是NBA最伟大的运动员之一" ] } } ] }}

6.SQL查询

在ES7之后,支持SQL语句查询文档:

GET /_sql?format=txt { "query": SQL语句 }例如GET /_sql?format=txt{ "query":"select *from people"}//查询结果 desc | id | name ----------------------+---------------+---------------tom is the best player|1003 |tom 戴维斯是NBA最伟大的运动员之一 |1001 |湖人俱乐部的戴维斯 科比是NBA最伟大的运动员之一 |1002 |湖人俱乐部的科比 詹姆斯是NBA最伟大的运动员之一 |1003 |湖人俱乐部的詹姆斯 奥尼尔是NBA最伟大的运动员之一 |1004 |湖人俱乐部的奥尼尔 //又例如GET /_sql?format=txt{ "query":"select *from people", "filter":{ "match":{ "name":"tom" } }}//查询结果 desc | id | name ----------------------+---------------+---------------tom is the best player|1003 |tom

开源版本的ES并不支持通过Java操作SQL进行查询,如果需要操作 SQL查询,则需花钱买版

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringDataJpa like查询无效的解决
下一篇:Elasticsearch分词器
相关文章

 发表评论

暂时没有评论,来抢沙发吧~