千家信息网

Elasticsearch中查询的多种方式

发表于:2024-11-19 作者:千家信息网编辑
千家信息网最后更新 2024年11月19日,本篇内容介绍了"Elasticsearch中查询的多种方式"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所
千家信息网最后更新 2024年11月19日Elasticsearch中查询的多种方式

本篇内容介绍了"Elasticsearch中查询的多种方式"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、DSL查询

query DSL:Domain Specified Language,特定领域的语言

1.查询某个索引下所有的数据

语法

GET /{index}/_search{  "query": {"match_all": {}}}

示例

输入:GET /staffs/_search{  "query": {"match_all": {}}}
输出{  "took" : 1, 消耗的时间  "timed_out" : false, 是否超时  "_shards" : {    "total" : 1, 一共请求了几个shared    "successful" : 1, 成功了几个    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 4, 查询的结果的总数量      "relation" : "eq"    },    "max_score" : 1.0,     "hits" : [      {        "_index" : "staffs", 索引的名称        "_type" : "_doc",        "_id" : "1a", 数据对应的主键        "_score" : 1.0,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高        "_source" : { 数据的详情          "name" : "shu xian sheng",          "age" : 28,          "phone" : "15711111111",          "posittion" : "java kaifa",          "hobby" : [            "lanqiu",            "zuqiu",            "tubu"          ]        }      },      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "Nq_96G0Bs8sg-pU7kn0S",        "_score" : 1.0,        "_source" : {          "name" : "wang xiao san",          "age" : 21,          "phone" : "15722222222",          "posittion" : "web kaifa",          "hobby" : [            "yumaoqiu",            "zuqiu",            "taiqiu"          ]        }      },      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "name" : "lixiansheng",          "age" : 25,          "phone" : "15733333333",          "posittion" : "android",          "hobby" : [            "paobu",            "zuqiu",            "tubu"          ]        }      },      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "3",        "_score" : 1.0,        "_source" : {          "name" : "maxiaoshuan",          "age" : 23,          "phone" : "15744444444",          "posittion" : "ios",          "hobby" : [            "paobu",            "yumaoqiu",            "lanqiu"          ]        }      }    ]  }}
2.查询名字中含有xiao 按照年龄倒叙排序的

语法

GET {index}/_search{  "query": {    "match": {      "FIELD": "TEXT"  FIELD:字段的名称  TEXT:条件    }  },  "sort": [    {      "FIELD": {       FIELD: 字段的名称        "order": "desc"      }    }  ]}

示例

GET staffs/_search{  "query": {    "match": {      "name": "xiao"    }  },  "sort": [    {      "age": {        "order": "desc"      }    }  ]}
{  "took" : 1,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : null,    "hits" : [      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "3",        "_score" : null,        "_source" : {          "name" : "ma xiao shuai",          "age" : 23,          "phone" : "15744444444",          "posittion" : "ios",          "hobby" : [            "paobu",            "yumaoqiu",            "lanqiu"          ]        },        "sort" : [          23        ]      },      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "Nq_96G0Bs8sg-pU7kn0S",        "_score" : null,        "_source" : {          "name" : "wang xiao san",          "age" : 21,          "phone" : "15722222222",          "posittion" : "web kaifa",          "hobby" : [            "yumaoqiu",            "zuqiu",            "taiqiu"          ]        },        "sort" : [          21        ]      }    ]  }}
3.分页查询数据

语法

GET {index}/_search{  "query": {"match_all": {}},  "from": 1,  从第几页开始查询,0:代表第一页,1:代表第二页  "size": 1  每页显示的条数}

示例

GET staffs/_search{  "query": {"match_all": {}},  "from": 1,  "size": 1}
{  "took" : 1,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 4,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "Nq_96G0Bs8sg-pU7kn0S",        "_score" : 1.0,        "_source" : {          "name" : "wang xiao san",          "age" : 21,          "phone" : "15722222222",          "posittion" : "web kaifa",          "hobby" : [            "yumaoqiu",            "zuqiu",            "taiqiu"          ]        }      }    ]  }}
4.搜索名称含有xian sheng 且年龄大于25的

语法

GET {index}}/_search{  "query": {    "bool": {      "must": [        {"match": {          "FIELD": "TEXT"        }}      ]    }  },  "post_filter": {    "range": {      "FIELD": {        "gte": 10      }    }  }}

示例

GET staffs/_search{  "query": {    "bool": {    可以拼接多个条件的查询      "must": [        {"match": {          "name": "xian sheng"        }}      ]    }  },  "post_filter": {    "range": {      "age": {        "gt": 25      }    }  }}
其实有两条 但是有一个年龄为25  所以这里只显示一条{  "took" : 1,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.3862944,    "hits" : [      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "1a",        "_score" : 1.3862944,        "_source" : {          "name" : "shu xian sheng",          "age" : 28,          "phone" : "15711111111",          "posittion" : "java kaifa",          "hobby" : [            "lanqiu",            "zuqiu",            "tubu"          ]        }      }    ]  }}
5.full-text search(全文检索)

示例

GET staffs/_search{  "query": {"match": {    "posittion": "java kaifa"  }}}
{  "took" : 1,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 1.6694658,    "hits" : [      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "1a",        "_score" : 1.6694658,        "_source" : {          "name" : "shu xian sheng",          "age" : 28,          "phone" : "15711111111",          "posittion" : "java kaifa",          "hobby" : [            "lanqiu",            "zuqiu",            "tubu"          ]        }      },      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "Nq_96G0Bs8sg-pU7kn0S",        "_score" : 0.60996956,        "_source" : {          "name" : "wang xiao san",          "age" : 21,          "phone" : "15722222222",          "posittion" : "web kaifa",          "hobby" : [            "yumaoqiu",            "zuqiu",            "taiqiu"          ]        }      }    ]  }}
6.phrase search(短语搜索)
  • 和全文检索相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面意义匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回

  • 短语搜索,要求输入的搜索串,必须在制定的字段文本中,完全包含一模一样的,擦可以算匹配,才能作为结果返回

语法

GET staffs/_search{  "query": {    "match_phrase": {      "FIELD": "PHRASE"    }  }}

示例

GET staffs/_search{  "query": {    "match_phrase": {      "posittion": "java kaifa"    }  }}
{  "took" : 30,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.6694657,    "hits" : [      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "1a",        "_score" : 1.6694657,        "_source" : {          "name" : "shu xian sheng",          "age" : 28,          "phone" : "15711111111",          "posittion" : "java kaifa",          "hobby" : [            "lanqiu",            "zuqiu",            "tubu"          ]        }      }    ]  }}
7.highlight search(高亮搜索结果)

语法

GET {index}/_search{  "query": {    "match": {      "FIELD": "TEXT"    }  },  "highlight": {    "fields": {      "FIELD": {}    }  }}

示例

GET staffs/_search{  "query": {    "match": {      "posittion": "kaifa"    }  },  "highlight": {    "fields": {      "posittion": {}    }  }}
{  "took" : 54,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 0.60996956,    "hits" : [      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "1a",        "_score" : 0.60996956,        "_source" : {          "name" : "shu xian sheng",          "age" : 28,          "phone" : "15711111111",          "posittion" : "java kaifa",          "hobby" : [            "lanqiu",            "zuqiu",            "tubu"          ]        },        "highlight" : {          "posittion" : [            "java kaifa"          ]        }      },      {        "_index" : "staffs",        "_type" : "_doc",        "_id" : "Nq_96G0Bs8sg-pU7kn0S",        "_score" : 0.60996956,        "_source" : {          "name" : "wang xiao san",          "age" : 21,          "phone" : "15722222222",          "posittion" : "web kaifa",          "hobby" : [            "yumaoqiu",            "zuqiu",            "taiqiu"          ]        },        "highlight" : {          "posittion" : [            "web kaifa"          ]        }      }    ]  }}
倒排索引
  • 其实保存数据的时候,其查询索引就已经创建了,使用的倒排索引

  • 例如:posittion字段,先被拆解,然后创建倒排索引

  • 拆解是根据选择的分词器构成的

拆解的单词对应的数据主键
java1a
kaifa1a,Nq_96G0Bs8sg-pU7kn0S
web1a,Nq_96G0Bs8sg-pU7kn0S
ios3
android4
查询所有的索引
GET _cat/indices?v
health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.sizegreen  open   .kibana_task_manager_1   Qyl1MousQLq5FyMOCBO4nw   1   0          2            0     30.5kb         30.5kbgreen  open   .apm-agent-configuration qPsz40bsQxW_Zcd_4pkJJg   1   0          0            0       283b           283bgreen  open   .kibana_1                yYdsQgxWQ0utXN-Ulhm5ew   1   0          9            0     35.4kb         35.4kbyellow open   staffs                   uoo38LYwRB2PzxupYjJ66Q   1   1          4            0     17.9kb         17.9kb

"Elasticsearch中查询的多种方式"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0