九、文档的查询
Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查询。常见的查询类型包括。
- 查询所有:查询出所有数据,一般测试用。例如:match_all
- 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:
match_query
multi_match_query
- 精确查询:根据精确词条值查找数据,一般是查找keyword、数值、日期、boolean等类型字段。例如:
ids
range
term
- 地理(geo)查询:根据经纬度查询。例如:
geo_distance
geo_bounding_box
- 复合(compound)查询:复合查询可以将上述各种查询条件组合起来,合并查询条件。例如:
bool
function_score
9.1、term查询
Elasticsearch默认采用的是standard,这个分词器是一个字一个词,例如我们在person这个索引里面有以下几条数据- {
- "took" : 49,
- "timed_out" : false,
- "_shards" : {
- "total" : 1,
- "successful" : 1,
- "skipped" : 0,
- "failed" : 0
- },
- "hits" : {
- "total" : {
- "value" : 4,
- "relation" : "eq"
- },
- "max_score" : 0.105360515,
- "hits" : [
- {
- "_index" : "person",
- "_type" : "_doc",
- "_id" : "1",
- "_score" : 0.105360515,
- "_source" : {
- "name" : "张三",
- "age" : 30,
- "address" : "杭州市滨江区"
- }
- },
- {
- "_index" : "person",
- "_type" : "_doc",
- "_id" : "e0DxjYABtRuVRBuRfJYL",
- "_score" : 0.105360515,
- "_source" : {
- "name" : "李四",
- "age" : 30,
- "address" : "杭州市滨江区"
- }
- },
- {
- "_index" : "person",
- "_type" : "_doc",
- "_id" : "fEDxjYABtRuVRBuRg5aL",
- "_score" : 0.105360515,
- "_source" : {
- "name" : "李四",
- "age" : 30,
- "address" : "杭州市滨江区"
- }
- },
- {
- "_index" : "person",
- "_type" : "_doc",
- "_id" : "4",
- "_score" : 0.105360515,
- "_source" : {
- "name" : "李四",
- "age" : 30,
- "address" : "杭州市滨江区"
- }
- }
- ]
- }
- }
-
复制代码
采用term进行查询- GET person/_search
- {
- "query": {
- "term": {
- "address": {
- "value": "杭州"
- }
- }
- }
- }
复制代码
返回的结果- {
- "took" : 47,
- "timed_out" : false,
- "_shards" : {
- "total" : 1,
- "successful" : 1,
- "skipped" : 0,
- "failed" : 0
- },
- "hits" : {
- "total" : {
- "value" : 0,
- "relation" : "eq"
- },
- "max_score" : null,
- "hits" : [ ]
- }
- }
-
复制代码
任何词条都没有查询到。
9.2、创建指定分词器的索引
创建索引
- put http://192.168.2.128:9200/person
-
- //入参
- {
- "mappings":{
- "properties":{
- "name":{
- "type":"keyword"
- },
- "address":{
- "type":"text",
- "analyzer":"ik_max_word"
- }
- }
- }
- }
-
- //结果
- {
- "acknowledged": true,
- "shards_acknowledged": true,
- "index": "person"
- }
复制代码
查询索引
查询指定分词器的索引- get http://192.168.2.128:9200/person
-
- // 运行结果
- {
- "person": {
- "aliases": {},
- "mappings": {
- "properties": {
- "address": {
- "type": "text",
- "analyzer": "ik_max_word" //address字段使用ik分词器进行分词
- },
- "name": {
- "type": "keyword"
- }
- }
- },
- "settings": {
- "index": {
- "creation_date": "1651665740384",
- "number_of_shards": "1",
- "number_of_replicas": "1",
- "uuid": "nYelz6sgQMWpcNEt-hrGUA",
- "version": {
- "created": "7040299"
- },
- "provided_name": "person"
- }
- }
- }
- }
复制代码
添加文档
- POST http://192.168.2.128:9200/person/_doc/1
- {
- "name" : "李四",
- "address" : "杭州市滨江区"
- }
-
-
- POST http://192.168.2.128:9200/person/_doc/2
- {
- "name" : "李四",
- "address" : "北京市朝阳区"
- }
-
复制代码
分词查询
- GET http://192.168.2.128:9200/person/_search
-
- //入参
- {
- "query": {
- "term": {
- "address": {
- "value": "北京"
- }
- }
- }
- }
-
- //结果
- {
- "took" : 758,
- "timed_out" : false,
- "_shards" : {
- "total" : 1,
- "successful" : 1,
- "skipped" : 0,
- "failed" : 0
- },
- "hits" : {
- "total" : {
- "value" : 1,
- "relation" : "eq"
- },
- "max_score" : 0.6931472,
- "hits" : [
- {
- "_index" : "person",
- "_type" : "_doc",
- "_id" : "2",
- "_score" : 0.6931472,
- "_source" : {
- "name" : "李四",
- "address" : "北京市朝阳区"
- }
- }
- ]
- }
- }
-
复制代码
9.3、match查询
match会对查询条件进行分词,然后匹配的结果的并集返回。- GET person/_search
- {
- "query": {
- "match": {
- "address": "北京杭州"
- }
- }
- }
-
- //查询结果
- {
- "took" : 736,
- "timed_out" : false,
- "_shards" : {
- "total" : 1,
- "successful" : 1,
- "skipped" : 0,
- "failed" : 0
- },
- "hits" : {
- "total" : {
- "value" : 2,
- "relation" : "eq"
- },
- "max_score" : 0.6931472,
- "hits" : [
- {
- "_index" : "person",
- "_type" : "_doc",
- "_id" : "1",
- "_score" : 0.6931472,
- "_source" : {
- "name" : "李四",
- "address" : "杭州市滨江区"
- }
- },
- {
- "_index" : "person",
- "_type" : "_doc",
- "_id" : "2",
- "_score" : 0.6931472,
- "_source" : {
- "name" : "李四",
- "address" : "北京市朝阳区"
- }
- }
- ]
- }
- }
-
-
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |