ElasticSearch学习九 文档查询
九、文档的查询
Elasticsearch提供了基于JSON的DSL()来定义查询。常见的查询类型包括。
-
查询所有:查询出所有数据,一般测试用。例如: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" : "北京市朝阳区"
}
}
]
}
}
标签:
ElasticSearch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
2016-06-25 构造函数constructor 与析构函数destructor(三)
2016-06-25 构造函数constructor 与析构函数destructor(一)
2016-06-25 构造函数constructor 与析构函数destructor(二)