elasticsearch的字符串动态映射,为什么说spring让Java变得更好

本文通过实例展示了Elasticsearch中text字段与keyword子字段的搜索与聚合操作。在搜索时,发现title字段的keyword子字段需要使用精确匹配,如`title.keyword`,而text字段则支持模糊匹配。在聚合统计时,keyword类型字段可以直接用于分组统计,证明了其在聚合操作中的适用性。
摘要由CSDN通过智能技术生成

“ignore_above” : 256

}

}

},

“language” : {

“type” : “text”,

“fields” : {

“keyword” : {

“type” : “keyword”,

“ignore_above” : 256

}

}

},

“price” : {

“type” : “float”

},

“publish_time” : {

“type” : “date”

},

“title” : {

“type” : “text”,

“fields” : {

“keyword” : {

“type” : “keyword”,

“ignore_above” : 256

}

}

}

}

}

}

}

}

[](

)验证检索

  1. 执行以下检索命令验证检索:

GET book/_search

{

“query”: {

“match”: {“title”:“Elasticsearch”}

}

}

第一条记录都可以搜索到,证明description字段已经被分词和索引了;

2. title字段还有一种索引方式keyword,也来试试,查keyword是要用完整内容做查询条件的,如下:

GET book/_search

{

“query”: {

“term”: {"tit

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

le":“Elasticsearch IN ACTION”}

}

}

得到的结果如下,没有记录:

{

“took” : 0,

“timed_out” : false,

“_shards” : {

“total” : 5,

“successful” : 5,

“skipped” : 0,

“failed” : 0

},

“hits” : {

“total” : 0,

“max_score” : null,

“hits” : [ ]

}

}

这是怎么回事呢?对于这种sub-field的查询,不能直接使用title,而是要用title.keyword,改成如下请求:

GET book/_search

{

“query”: {

“term”: {“title.keyword”:“Elasticsearch IN ACTION”}

}

}

这次顺利查到:

{

“took” : 0,

“timed_out” : false,

“_shards” : {

“total” : 5,

“successful” : 5,

“skipped” : 0,

“failed” : 0

},

“hits” : {

“total” : 1,

“max_score” : 0.2876821,

“hits” : [

{

“_index” : “book”,

“_type” : “es”,

“_id” : “101”,

“_score” : 0.2876821,

“_source” : {

“title” : “Elasticsearch IN ACTION”,

“language” : “java”,

“author” : “Radu Gheorghe”,

“price” : 58.8,

“publish_time” : “2018-10-01”,

“description” : “本书主要展示如何使用Elasticsearch构建可扩展的搜索应用程序。”

}

}

]

}

}

[](

)验证聚合

执行以下命令,以language字段进行分组,统计每个分组的文档数:

GET book/_search

{

“aggs”: {

“per_count”: {

“terms”:{

“field”:“language.keyword”

}

}

}

}

得到结果如下,可以成功统计language字段为java的文档数量为2,可见动态映射给language设定的keyword类型能够直接用于聚合(text类型不能直接用于聚合,会返回status=400错误,修改参数后可以将text类用于聚合,但是会消耗更多内存资源):

{

“took” : 2,

“timed_out” : false,

“_shards” : {

“total” : 5,

“successful” : 5,

“skipped” : 0,

“failed” : 0

},

“hits” : {

“total” : 2,

“max_score” : 1.0,

“hits” : [

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值