本文共 11321 字,大约阅读时间需要 37 分钟。
本文基于Elasticsearch6.4.0版本
DSL是Elasticsearch提供的一种基于JSON格式的查询方式。
mapping映射
{ "emp": { "mappings": { "base_info": { "properties": { "age": { "type": "integer" }, "dept": { "type": "long" }, "detail": { "type": "text" }, "name": { "type": "keyword" }, "salary": { "type": "double" }, "sex": { "type": "integer" } } } } }}
GET /emp/base_info/_search
已有数据
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 6, "max_score": 1, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "5", "_score": 1, "_source": { "name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": "普通人" } }, { "_index": "emp", "_type": "base_info", "_id": "10", "_score": 1, "_source": { "name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, { "_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, { "_index": "emp", "_type": "base_info", "_id": "1", "_score": 1, "_source": { "name": "张三", "age": 18, "sex": 1, "salary": 6000, "detail": "普通人" } }, { "_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": { "name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } }, { "_index": "emp", "_type": "base_info", "_id": "11", "_score": 1, "_source": { "name": "小王", "age": 26, "sez": 1, "salary": 1234, "detail": "隔壁老王" } } ] }}
GET /_search{ "query":{ "match_all": { } } }
GET /emp/base_info/_search{ "query": { "match_all": { } }}
GET /emp/base_info/_search{ "query": { "match": { "name": "赵六" } }}
查询结果
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "5", "_score": 0.2876821, "_source": { "name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": "普通人" } } ] }}
GET /emp/base_info/_search{ "query": { "match_all": { } }, "size": 2}
查询结果
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 1, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "5", "_score": 1, "_source": { "name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": "普通人" } }, { "_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } } ] }}
GET /emp/base_info/_search{ "query": { "match_all": { } }, "size": 2, "from": 1}
查询结果
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 1, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, { "_index": "emp", "_type": "base_info", "_id": "1", "_score": 1, "_source": { "name": "张三", "age": 18, "sex": 1, "salary": 6000, "detail": "普通人" } } ] }}
GET /emp/base_info/_search{ "query":{ "match": { "name": "赵六" } }, "_source": ["name","salary"]}
查询结果
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "5", "_score": 0.2876821, "_source": { "name": "赵六", "salary": 8888 } } ] }}
GET /emp/base_info/_search{ "query":{ "multi_match": { "query": "王者", "fields": ["name","detail"] #在指定字段中按条件查询 } }}
查询结果
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 0.87546873, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "3", "_score": 0.87546873, "_source": { "name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } }, { "_index": "emp", "_type": "base_info", "_id": "10", "_score": 0.6931472, "_source": { "name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, { "_index": "emp", "_type": "base_info", "_id": "11", "_score": 0.18232156, "_source": { "name": "小王", "age": 26, "sez": 1, "salary": 1234, "detail": "隔壁老王" } } ] }}
GET /emp/base_info/_search{ "query":{ "term": { "detail": { "value": "王" } } }}
查询结果
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.18232156, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "3", "_score": 0.18232156, "_source": { "name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } }, { "_index": "emp", "_type": "base_info", "_id": "11", "_score": 0.18232156, "_source": { "name": "小王", "age": 26, "sez": 1, "salary": 1234, "detail": "隔壁老王" } } ] }}
如果查“老王”
GET /emp/base_info/_search{ "query":{ "term": { "detail": { "value": "老王" } } }}
查询不到数据,说明默认的分词器对中文会按照一个字一个字的进行分词
{ "took": 0, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] }}
GET /emp/base_info/_search{ "query": { "range": { "salary": { "gte": 10000, "lte": 20000 } } }}
查询结果
{ "took": 0, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, { "_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": { "name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } } ] }}
GET /emp/base_info/_search{ "query": { "prefix": { "name": { "value": "王" } } }}
查询结果如下,name字段的type是keyword,之前使用match是查询不到的。
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "10", "_score": 1, "_source": { "name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, { "_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": { "name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } } ] }}
GET /emp/base_info/_search{ "query": { "wildcard": { "name": { "value": "王*" } } }}
查询结果
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "10", "_score": 1, "_source": { "name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, { "_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": { "name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } } ] }}
GET /emp/base_info/_search{ "query": { "fuzzy": { "detail": "王" } }}
GET /emp/base_info/_search{ "query": { "ids": { "values": ["1","2"] } }}
查询结果
{ "took": 0, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": { "name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, { "_index": "emp", "_type": "base_info", "_id": "1", "_score": 1, "_source": { "name": "张三", "age": 18, "sex": 1, "salary": 6000, "detail": "普通人" } } ] }}
转载地址:http://ehlrb.baihongyu.com/