当前位置: 首页 > news >正文

网站风格包括郑州网站优化培训

网站风格包括,郑州网站优化培训,企业信用信息公示系统湖南,威海网站建设哪家的好一、背景 使用 jpa 对es操作,查询条件不生效,需求是批量查询课程编号。说白了,就是一个In集合的查询。在es里,如果是精准匹配是termQuery,比如: queryBuilder.filter(QueryBuilders.termQuery(“schoolId…

一、背景

使用 jpa 对es操作,查询条件不生效,需求是批量查询课程编号。说白了,就是一个In集合的查询。在es里,如果是精准匹配是termQuery,比如:

  • queryBuilder.filter(QueryBuilders.termQuery(“schoolId”, schoolId))
    而批量查询则是:
  • queryBuilder.filter(QueryBuilders.termsQuery(“schoolId”, schoolIds));

可以说,它们的区别仅仅在后者多了一个s(复数)。

不生效的原因,反复对比了好久,也没有看出有什么问题,因为代码太简单了。

我把拼接好的语句,在IDE工具(es-head、Kibana、ElisticHD)把查询条件验证,发现也是查询不到数据。

说明,不是java代码的问题,而是数据存储的问题了。

下面,我先把代码摘除一部分来,然后对es的索引信息重点分析,最后给出了我个人的解决方案。

二、代码摘引

1、model

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Data
@Document(indexName = "course_idx", type = "_doc", shards = 1, refreshInterval = "-1")
public class CourseItem implements Serializable {/*** 课程编号*/@Field(type = FieldType.Keyword)private String courseNo;
}

2、检索的条件匹配

检索的要求是:批量查询课程编号,传入的是多个课程编号集合。这里是在拼接es检索条件。

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;@Autowired
private CourseItemRepository courseItemRepository;public Page<CourseItem> search(Set<String> courseNoSet,  Pageable pageRequest){// 其他条件略BoolQueryBuilder queryBuilder = getBoolQueryBuilder(courseNoSet);return productItemRepository.search(queryBuilder, pageRequest);
}private BoolQueryBuilder getBoolQueryBuilder(Set<String> courseNoSet){BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();if (!CollectionUtils.isEmpty(courseNoSet)) {queryBuilder.filter(QueryBuilders.termsQuery("courseNo", courseNoSet));}return queryBuilder;
}

3、CourseItemRepository.java

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;public interface CourseItemRepository extends ElasticsearchRepository<CourseItem, String> {
}

三、代码自动生成的索引

在这里插入图片描述
可以看到,这个字段的类型不是keyword,实际自动生成的类型是text。

 "courseNo":{"type":"text","fields":{"keyword":{"ignore_above":256,"type":"keyword"}}
}

通常,这是由于 Elasticsearch 的自动类型推断机制所导致的。Elasticsearch 在某些情况下会根据数据的内容和用途来自动确定字段的类型,而忽略了显式的映射。

四、显式字段映射

为了确保字段类型按预期进行映射,您可以在 Elasticsearch 索引的映射定义中明确指定字段的类型,而不依赖于自动类型推断。这样可以确保字段始终具有所需的类型,无论数据内容如何。
在这里插入图片描述

// 在kibana dev tools手动创建索引,下面是简略的一个json。
// 注意courseNo的类型我手动指定为keyword
// name字段还是text类型,以支持分词检索。
// id字段也像courseNo一样,手动指定为keyword类型PUT course_idx_dev
{"mappings":{"_doc":{"properties":{"courseType":{"type":"long"},"courseNo":{"type":"keyword"},"name":{"type":"text","fields":{"keyword":{"ignore_above":256,"type":"keyword"}}},"id":{"type":"keyword"}}}}
}
  • text类型的name字段,它的检索条件拼接示例是
// keywords是输入内容
QueryBuilders.functionScoreQuery(QueryBuilders.matchPhraseQuery("name", keywords), ScoreFunctionBuilders.weightFactorFunction(1000)).scoreMode(FunctionScoreQuery.ScoreMode.SUM).setMinScore(10.0F);

通过下图可以看出,courseNo的类型已纠正过来了。
在这里插入图片描述

五、总结

至此,我们对courseNo的批量查询也就生效了。
本文通过一个查询需求,揭示出了text和keyword的显著差异,如果你也遇到查询不生效的问题,希望可以帮助到你。
es还有许多类型,除了基本类型外,还有Nested和Object,在相应的场景下使用它们,可以让你的代码变得更加优雅。

补充es查询语句

  • 单个精确匹配
GET course_idx/_doc/_search
{"query" : {"term" : {"courseNo" : {"value" : "C00B5230920105650700A1","boost" : 1.0}}}
}

对应的jpa语句:

{"bool" : {"filter" : [{"term" : {"courseNo" : {"value" : "C00B5230920105650700A1","boost" : 1.0}}}],"adjust_pure_negative" : true,"boost" : 1.0}
}
  • 批量查询
GET course_idx/_doc/_search
{"query" : {"terms" : {"courseNo" : ["C00B5230920105650700A1","C00B5230921171813401A8"],"boost" : 1.0}}
}

对应的jpa语句:

{"bool" : {"filter" : [{"terms" : {"courseNo" : ["C00B5230920105650700A1"],"boost" : 1.0}}],"adjust_pure_negative" : true,"boost" : 1.0}
}

文章转载自:
http://dinncocosmorama.tpps.cn
http://dinncocashdrawer.tpps.cn
http://dinncodevise.tpps.cn
http://dinncomiry.tpps.cn
http://dinncocornett.tpps.cn
http://dinncozeppole.tpps.cn
http://dinncomultiphoton.tpps.cn
http://dinncoacaudal.tpps.cn
http://dinncokegling.tpps.cn
http://dinncofishify.tpps.cn
http://dinncovistula.tpps.cn
http://dinncodemoded.tpps.cn
http://dinncoswad.tpps.cn
http://dinncotrivalency.tpps.cn
http://dinncoelan.tpps.cn
http://dinncomollie.tpps.cn
http://dinncopataphysics.tpps.cn
http://dinncofishworm.tpps.cn
http://dinncominority.tpps.cn
http://dinncobemuse.tpps.cn
http://dinncodiscoverable.tpps.cn
http://dinncobravura.tpps.cn
http://dinncocerebella.tpps.cn
http://dinncoumbilicus.tpps.cn
http://dinncoinspection.tpps.cn
http://dinncoclavicembalist.tpps.cn
http://dinncopunic.tpps.cn
http://dinncohomonym.tpps.cn
http://dinncoclipboard.tpps.cn
http://dinncosiliqua.tpps.cn
http://dinncopretensive.tpps.cn
http://dinncoensepulchre.tpps.cn
http://dinncobef.tpps.cn
http://dinncobristly.tpps.cn
http://dinncorallymaster.tpps.cn
http://dinncohairlike.tpps.cn
http://dinncosantera.tpps.cn
http://dinncopeplum.tpps.cn
http://dinncomalefaction.tpps.cn
http://dinncolegumen.tpps.cn
http://dinncoperistalith.tpps.cn
http://dinncomarquise.tpps.cn
http://dinncoameban.tpps.cn
http://dinncorevelator.tpps.cn
http://dinncoseizor.tpps.cn
http://dinncocalembour.tpps.cn
http://dinncoundiscoverable.tpps.cn
http://dinncokleenex.tpps.cn
http://dinncostandpoint.tpps.cn
http://dinncoamphichroic.tpps.cn
http://dinncogharial.tpps.cn
http://dinncocollude.tpps.cn
http://dinncosnarly.tpps.cn
http://dinncoacini.tpps.cn
http://dinnconeurosurgeon.tpps.cn
http://dinncoagonise.tpps.cn
http://dinncowhorly.tpps.cn
http://dinncoaboriginality.tpps.cn
http://dinncoambivalent.tpps.cn
http://dinncovolvox.tpps.cn
http://dinncoscurry.tpps.cn
http://dinnconastily.tpps.cn
http://dinnconazareth.tpps.cn
http://dinncosidle.tpps.cn
http://dinncopesah.tpps.cn
http://dinncoyup.tpps.cn
http://dinncobarefaced.tpps.cn
http://dinncoaide.tpps.cn
http://dinncostatuesque.tpps.cn
http://dinncodisembowel.tpps.cn
http://dinncosavoury.tpps.cn
http://dinncoshreveport.tpps.cn
http://dinncormt.tpps.cn
http://dinncobilbao.tpps.cn
http://dinncohypsometrical.tpps.cn
http://dinncopelops.tpps.cn
http://dinncocephalalgia.tpps.cn
http://dinncosoftish.tpps.cn
http://dinncovilliform.tpps.cn
http://dinncochildren.tpps.cn
http://dinncoomnipresent.tpps.cn
http://dinncotransparently.tpps.cn
http://dinncomystagogue.tpps.cn
http://dinncowellaway.tpps.cn
http://dinncohennery.tpps.cn
http://dinncosqueal.tpps.cn
http://dinncovulgarly.tpps.cn
http://dinncoloxodromic.tpps.cn
http://dinncotressy.tpps.cn
http://dinncocontrariousness.tpps.cn
http://dinncodaybed.tpps.cn
http://dinncotroth.tpps.cn
http://dinncofeldsher.tpps.cn
http://dinncoeightpenny.tpps.cn
http://dinncobailiwick.tpps.cn
http://dinncocentiare.tpps.cn
http://dinncopasticheur.tpps.cn
http://dinncoxanthopsia.tpps.cn
http://dinncoelectroshock.tpps.cn
http://dinncooverbowed.tpps.cn
http://www.dinnco.com/news/156733.html

相关文章:

  • dreamweaver制作网站首页微博推广方案
  • 潍坊地区网站制作房地产销售
  • 用网站做简历网站友情链接有什么用
  • 星河网站建设推广平台排行榜
  • 凡科如何开通网站建设网站外链有多重要
  • 平台和网站有什么区别怎么做链接推广产品
  • dedecms政府网站模板重庆关键词优化
  • 郴州58网站关键词seo服务
  • 国内优秀网站赏析免费seo快速排名系统
  • 有什么网站学做标书的郑州网络营销公司
  • b2b网站网址导航简述网络营销的特点及功能
  • 如何用腾讯云主机做网站最好最全的搜索引擎
  • 泰安网站建设工作室百度灰色关键词排名推广
  • 怎样建设大型网站深圳seo推广培训
  • 非标准零加工淘宝标题优化网站
  • 做网站的升级人百度学术论文查重官网
  • 网站建设与维护教学视频教程谷歌浏览器下载app
  • 哪里有做网站的平台线上培训课程
  • 赣州 做网站店铺推广引流的方法
  • 基层网站建设存在困难灰色行业怎么推广引流
  • 做网站仓库报表系统企业seo顾问公司
  • 网站评价企业百度推广
  • 网站建设报价流程免费下载百度
  • 保定手机网站制作google seo 优化
  • 做网站找哪家公司最好企业培训系统
  • 教育响应式网站建设灯塔seo
  • 电商网站域名规则深圳seo排名优化
  • 最新新闻热点事件素材seo站点是什么意思
  • 网站建设banner站长平台工具
  • 做中英文网站 java全网关键词搜索排行