龙空技术网

ElasticSearch 综合案例-高亮+区间+精准查询

bobo棒 1652

前言:

今天看官们对“mysql数据驱动包放在哪里”大致比较着重,兄弟们都需要知道一些“mysql数据驱动包放在哪里”的相关知识。那么小编在网上汇集了一些有关“mysql数据驱动包放在哪里””的相关资讯,希望大家能喜欢,我们快快来了解一下吧!

1.关键词高亮查询+日期区间查询+作者精确查询1.0 效果

关键词:去标题和内容中搜索,如果有则颜色高亮

日期区间:按照指定日期区间查询

作者:精确查询,不分词,即“王昌龄”三个字不分开查询。

1.1 实体类

//indexName 索引库 ,type:索引类型(表)@Document(indexName = "index_article",type = "article")public class Article {@Idprivate String id;private String title;//标题private String content;//内容@Field(type=FieldType.Keyword)//不分词精确查询private String username;//作者private Date created;//getsetter}
1.2 查询vo
public class ArticleVO extends Article {@DateTimeFormat(pattern = "yyyy-MM-dd")private Date date1;@DateTimeFormat(pattern = "yyyy-MM-dd")private Date date2;}
1.3 controller
@Controllerpublic class ArticleController {@AutowiredElasticsearchTemplate elasticsearchTemplate;@RequestMapping("list")public String list(String keyword, ArticleVO articleVO, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, Model model) {SearchQuery searchQuery = null;String pre="<span style='color:red'>";String post="</span>";String title="title";String content="content";//高亮的字段HighlightBuilder.Field titleField =new HighlightBuilder.Field("title").preTags(pre).postTags(post);HighlightBuilder.Field contentField =new HighlightBuilder.Field("content").preTags(pre).postTags(post);//boolQuery用来组合查询BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();if(StringUtils.isNoneBlank(keyword)){//关键词模糊查询boolQuery.must( QueryBuilders.multiMatchQuery(keyword,title, content));}RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("created");if(articleVO.getDate1()!=null){//开始日期boolQuery.must(rangeQuery.gte(articleVO.getDate1().getTime()));//封装条件2}if(articleVO.getDate2()!=null){//结束日期boolQuery.must(rangeQuery.lte(articleVO.getDate2().getTime()));//封装条件3}if(StringUtils.isNotBlank(articleVO.getUsername())){//作者精确查询boolQuery.must(QueryBuilders.termQuery("username",articleVO.getUsername()));//封装条件4}searchQuery = new NativeSearchQueryBuilder().withQuery(boolQuery).withHighlightFields(titleField,contentField).withPageable(PageRequest.of(pageNum - 1, pageSize)).withSort(SortBuilders.fieldSort("created").order(SortOrder.ASC)).build();AggregatedPage<Article> articles = elasticsearchTemplate.queryForPage(searchQuery, Article.class,new HighlightResultMapper());//封装pageInfoPage<Article> page = new Page<>(pageNum, pageSize);page.setTotal(articles.getTotalElements());//封装总记录数PageInfo<Article> info = new PageInfo<>(page);info.setList(articles.getContent());//封装当前页数据model.addAttribute("pg", info);model.addAttribute("keyword", keyword);model.addAttribute("articleVO", articleVO);return "list";}
1.4 高亮工具类-封装查询结果
public class HighlightResultMapper implements SearchResultMapper {@Overridepublic <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> clazz, Pageable pageable) {long totalHits = searchResponse.getHits().getTotalHits();//获取击中的总条数List<T> list = new ArrayList<>();SearchHits hits = searchResponse.getHits();//获取高亮击中的数据对象集合if (hits.getHits().length> 0) {for (SearchHit searchHit : hits) {Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();//获取高亮字段T item = JSON.parseObject(searchHit.getSourceAsString(), clazz);Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {field.setAccessible(true);if (highlightFields.containsKey(field.getName())) {//如果高亮的字段和要封装的对象的名字一致则进行重新封装try {field.set(item, highlightFields.get(field.getName()).fragments()[0].toString());} catch (IllegalAccessException e) {e.printStackTrace();}}}list.add(item);}}return new AggregatedPageImpl<>(list, pageable, totalHits);}
1.5 jsp
<form action="/list">关键词: <input type="text" name="keyword" value="${keyword}">日期区间:<input type="date" name="date1" value="<fmt:formatDate value="${articleVO.date1}" pattern="yyyy-MM-dd"></fmt:formatDate>">--<input type="date" name="date2" value="<fmt:formatDate value="${articleVO.date2}" pattern="yyyy-MM-dd"></fmt:formatDate>">作者精确查询: <input type="text" name="username" value="${articleVO.username}"><button type="submit" >查询</button></form><table class="table"><td>id</td><td>标题</td><td>内容</td><td>作者</td><td>日期</td><c:forEach items="${pg.list}" var="a"><tr><td>${a.id}</td><td>${a.title}</td><td>${a.content}</td><td>${a.username}</td><td><fmt:formatDate value="${a.created}" pattern="yyyy-MM-dd HH:mm:ss" ></fmt:formatDate></td></tr></c:forEach></table><jsp:include page="/resource/common/pages.jsp"></jsp:include></body>
1.6 pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns=";xmlns:xsi=";xsi:schemaLocation=" ;><modelVersion>4.0.0</modelVersion><groupId>com.bobo</groupId><artifactId>es-test</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><!-- 定义主要版本号 --><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><spring.version>5.1.5.RELEASE</spring.version><mybatis.version>3.4.2</mybatis.version><!-- aop使用 --><aspectj-version>1.8.0</aspectj-version><log4j.version>1.2.17</log4j.version><log4japi.version>1.7.26</log4japi.version><druid.version>1.0.9</druid.version><mysql.version>5.1.6</mysql.version><mybatis.spring.version>1.3.0</mybatis.spring.version><jackson.version>2.9.8</jackson.version><validator.version>6.0.10.Final</validator.version><poi.version>3.9</poi.version><jstl.version>1.2</jstl.version><servlet-api.version>2.5</servlet-api.version><jsp-api.version>2.0</jsp-api.version><commons-lang3.version>3.3.2</commons-lang3.version><commons-io.version>2.2</commons-io.version><commons-net.version>3.3</commons-net.version><commons-fileupload.version>1.3.1</commons-fileupload.version><junit-version>4.12</junit-version><fast-json-version>1.2.7</fast-json-version><!-- 分页助手 --><pagehelper-version>5.2.0</pagehelper-version><json-simple-version>1.1.1</json-simple-version><common-logging-version>1.2</common-logging-version><jsp-api-version>2.2</jsp-api-version><jedis-version>2.9.0</jedis-version><spring-data-redis-version>2.1.4.RELEASE</spring-data-redis-version><spring-kafka-version>2.2.4.RELEASE</spring-kafka-version><spring-es-version>3.1.5.RELEASE</spring-es-version></properties><!-- jar依赖 --><!--第一部分--><dependencies><!--依赖工具类--><dependency><groupId>com.bobo</groupId><artifactId>bobo-utils</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- es --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-elasticsearch</artifactId><version>${spring-es-version}</version></dependency><!--kafka--><dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>${spring-kafka-version}</version></dependency><!-- 连接redis --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${jedis-version}</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>${spring-data-redis-version}</version></dependency><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><!-- 引用版本 --><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><!-- mybatis核心包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><!-- mybatis-spring 整合jar --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis.spring.version}</version></dependency><!-- druid数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><!-- Mysql数据库驱动包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- 日志文件管理包 --><!-- log start --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><!-- 依赖的公共包 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons-lang3.version}</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>${common-logging-version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectj-version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${aspectj-version}</version></dependency><!--日志--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${log4japi.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${log4japi.version}</version></dependency><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit-version}</version><scope>test</scope></dependency><!--############3###################第二部分####################--><!--web模块--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- 上传组件包 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>${commons-fileupload.version}</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency><!-- JSP相关 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>${jsp-api-version}</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>${servlet-api.version}</version><scope>provided</scope></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>${validator.version}</version></dependency><!-- mybaits pagehelper 分页助手 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>${pagehelper-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><!-- 富文本编辑器使用该文件上传 --><dependency><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId><version>${json-simple-version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fast-json-version}</version></dependency></dependencies><build><plugins><!-- tomcat插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>80</port><path>/SSM</path><uriEncoding>UTF-8</uriEncoding><server>tomcat7</server></configuration></plugin><plugin><!-- 配置jetty --><groupId>org.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.4.15.v20190215</version><configuration><httpConnector><!-- 端口号 --><port>80</port><!-- 访问路径 --><host>localhost</host></httpConnector><!--监听整个项目,如果项目源码改变,1秒后 jetty自动重启 --><scanIntervalSeconds>1</scanIntervalSeconds><!-- 配置jetty虚拟路径 --><contextHandlers><jettyWebAppContext><!--虚拟路径 访问路径 --><contextPath>/pic/</contextPath><!-- 物理路径 --><resourceBase>d:/pic/</resourceBase></jettyWebAppContext></contextHandlers></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.5</version><configuration><skip>true</skip></configuration></plugin></plugins></build></project>

标签: #mysql数据驱动包放在哪里