From 5043f7ed8c9d238c19cfc61d3b5bae25adc795c8 Mon Sep 17 00:00:00 2001
From: zjwave <594980947@qq.com>
Date: Sat, 16 Feb 2019 18:28:50 +0800
Subject: [PATCH 1/6] default add
---
.../module/article/service/search/ArticleSearcherFactory.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/search/ArticleSearcherFactory.java b/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/search/ArticleSearcherFactory.java
index 075dcd2ea..703dc4daa 100644
--- a/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/search/ArticleSearcherFactory.java
+++ b/module-article/module-article-service-provider/src/main/java/io/jpress/module/article/service/search/ArticleSearcherFactory.java
@@ -48,6 +48,7 @@ public class ArticleSearcherFactory {
return Aop.get(ElasticSearcher.class);
case "aliopensearch":
return Aop.get(AliyunOpenSearcher.class);
+ default:
}
ArticleSearcher searcher = JbootSpiLoader.load(ArticleSearcher.class, engine);
--
Gitee
From 2a649b9c8ce6efc9b3dce82ce660e242afdf7a80 Mon Sep 17 00:00:00 2001
From: zjwave <594980947@qq.com>
Date: Sat, 16 Feb 2019 21:54:38 +0800
Subject: [PATCH 2/6] default add
---
.../io/jpress/commons/utils/CommonsUtils.java | 7 +++
.../jpress/module/article/model/Article.java | 20 +++++++++
.../service/search/ArticleSearcher.java | 2 +
.../article/searcher/LuceneSearcher.java | 37 +++++++++++++---
starter/src/main/resources/undertow.txt | 43 +++++++++++++++++++
5 files changed, 103 insertions(+), 6 deletions(-)
create mode 100644 starter/src/main/resources/undertow.txt
diff --git a/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java b/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java
index 30bc41dbd..d2ee64a51 100644
--- a/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java
+++ b/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java
@@ -146,6 +146,13 @@ public class CommonsUtils {
.replace("&", "&");
}
+ public static String removeHtmlTag(String content) {
+ if (StrUtil.isBlank(content)) {
+ return content;
+ }
+ return content.replaceAll("<[^>]*>", "");
+ }
+
public static void main(String[] args) {
String script = "";
diff --git a/module-article/module-article-model/src/main/java/io/jpress/module/article/model/Article.java b/module-article/module-article-model/src/main/java/io/jpress/module/article/model/Article.java
index c2f72f5f6..ae9eef9af 100644
--- a/module-article/module-article-model/src/main/java/io/jpress/module/article/model/Article.java
+++ b/module-article/module-article-model/src/main/java/io/jpress/module/article/model/Article.java
@@ -38,6 +38,10 @@ public class Article extends BaseArticle {
public static final String STATUS_TRASH = "trash";
+ private String highlightContent;
+
+ private String highlightTitle;
+
public boolean isNormal() {
return STATUS_NORMAL.equals(getStatus());
}
@@ -123,6 +127,22 @@ public class Article extends BaseArticle {
return StrUtil.isNotBlank(thumbnail) ? thumbnail : getFirstImage();
}
+ public String getHighlightContent() {
+ return highlightContent;
+ }
+
+ public void setHighlightContent(String highlightContent) {
+ this.highlightContent = highlightContent;
+ }
+
+ public String getHighlightTitle() {
+ return highlightTitle;
+ }
+
+ public void setHighlightTitle(String highlightTitle) {
+ this.highlightTitle = highlightTitle;
+ }
+
@Override
public boolean save() {
CommonsUtils.escapeHtmlForAllAttrs(this, "content");
diff --git a/module-article/module-article-search-api/src/main/java/io/jpress/module/article/service/search/ArticleSearcher.java b/module-article/module-article-search-api/src/main/java/io/jpress/module/article/service/search/ArticleSearcher.java
index fce5b1f82..44c945c10 100644
--- a/module-article/module-article-search-api/src/main/java/io/jpress/module/article/service/search/ArticleSearcher.java
+++ b/module-article/module-article-search-api/src/main/java/io/jpress/module/article/service/search/ArticleSearcher.java
@@ -6,6 +6,8 @@ import io.jpress.module.article.model.Article;
public interface ArticleSearcher {
+ String HIGH_LIGHT_CLASS = "search-highlight";
+
public void addArticle(Article article);
public void deleteArticle(Object id);
diff --git a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
index fa9b34847..6ea1b2370 100644
--- a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
+++ b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
@@ -16,16 +16,19 @@
package io.jpress.module.article.searcher;
import com.jfinal.kit.PathKit;
+import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import io.jpress.commons.utils.CommonsUtils;
import io.jpress.module.article.model.Article;
import io.jpress.module.article.service.search.ArticleSearcher;
import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
+import org.apache.lucene.search.highlight.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NIOFSDirectory;
import org.lionsoul.jcseg.analyzer.JcsegAnalyzer;
@@ -33,12 +36,15 @@ import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import java.io.File;
import java.io.IOException;
+import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class LuceneSearcher implements ArticleSearcher {
+ private static final Log logger = Log.getLog(LuceneSearcher.class);
+
public static String INDEX_PATH = "~/indexes/";
private static Directory directory;
@@ -100,9 +106,12 @@ public class LuceneSearcher implements ArticleSearcher {
ScoreDoc lastScoreDoc = getLastScoreDoc(pageNum, pageSize, query, indexSearcher);
TopDocs topDocs = indexSearcher.searchAfter(lastScoreDoc, query, pageSize);
- List articles = toArticleList(indexSearcher, topDocs);
- int totalRow = getTotalRow(indexSearcher, query);
+ SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("", "");
+ Highlighter highlighter=new Highlighter(formatter, new QueryScorer(query));
+ highlighter.setTextFragmenter(new SimpleFragmenter(100));
+ List articles = toArticleList(indexSearcher, topDocs,highlighter,keyword);
+ int totalRow = getTotalRow(indexSearcher, query);
return newPage(pageNum, pageSize, totalRow, articles);
} catch (IOException e) {
e.printStackTrace();
@@ -115,8 +124,9 @@ public class LuceneSearcher implements ArticleSearcher {
private static ScoreDoc getLastScoreDoc(int pageIndex, int pageSize, Query query,
IndexSearcher indexSearcher) throws IOException {
- if (pageIndex == 1)
+ if (pageIndex == 1){
return null; // 如果是第一页返回空
+ }
int num = pageSize * (pageIndex - 1); // 获取上一页的数量
TopDocs tds = indexSearcher.search(query, num);
return tds.scoreDocs[num - 1];
@@ -188,14 +198,29 @@ public class LuceneSearcher implements ArticleSearcher {
}
- private List toArticleList(IndexSearcher searcher, TopDocs topDocs) throws IOException {
+ private List toArticleList(IndexSearcher searcher, TopDocs topDocs,Highlighter highlighter,String keyword) throws IOException {
List articles = new ArrayList<>();
+ Analyzer luceneAnalyzer = new CJKAnalyzer();
for (ScoreDoc item : topDocs.scoreDocs) {
Document doc = searcher.doc(item.doc);
Article article = new Article();
+ String title = doc.get("title");
+ String content = doc.get("content");
article.setId(Long.valueOf(doc.get("aid")));
- article.setTitle(doc.get("title"));
- article.setContent(doc.get("content"));
+ article.setTitle(title);
+ article.setContent(content);
+ //关键字高亮
+ try {
+ String highlightTitle = highlighter.getBestFragment(luceneAnalyzer.tokenStream(keyword, new StringReader(title)), title);
+ article.setHighlightTitle(highlightTitle);
+
+ String plainContent = CommonsUtils.removeHtmlTag(content);
+ plainContent.replaceAll("\\s*|\t|\r|\n","");
+ String highlightContent = highlighter.getBestFragment(luceneAnalyzer.tokenStream(keyword, new StringReader(plainContent)), plainContent);
+ article.setHighlightContent(highlightContent);
+ } catch (InvalidTokenOffsetsException e) {
+ logger.error(e.getMessage(),e);
+ }
articles.add(article);
}
return articles;
diff --git a/starter/src/main/resources/undertow.txt b/starter/src/main/resources/undertow.txt
new file mode 100644
index 000000000..c6e1f1e7b
--- /dev/null
+++ b/starter/src/main/resources/undertow.txt
@@ -0,0 +1,43 @@
+# 配置 undertow
+
+undertow.devMode=true
+undertow.host=0.0.0.0
+undertow.port=8080
+
+# 开启 gzip 压缩
+undertow.gzip.enable=true
+# 配置压缩级别,默认值 -1。 可配置 1 到 9。 1 拥有最快压缩速度,9 拥有最高压缩率
+undertow.gzip.level=-1
+# 触发压缩的最小内容长度
+undertow.gzip.minLength=1024
+
+
+# 下面两行命令生成密钥库
+# keytool -genkeypair -validity 3650 -alias club -keyalg RSA -keystore club.jks
+# keytool -importkeystore -srckeystore club.jks -destkeystore club.pfx -deststoretype PKCS12
+# 生成过程中提示输入 "名字与姓氏" 时输入 localhost。生产环境从阿里云下载 tomcat 类型的密钥库
+# 是否开启 ssl
+#undertow.ssl.enable=true
+# ssl 监听端口号,部署环境设置为 443
+undertow.ssl.port=443
+# 密钥库类型,建议使用 PKCS12
+undertow.ssl.keyStoreType=PKCS12
+# 密钥库文件
+undertow.ssl.keyStore=club.pfx
+# 密钥库密码
+undertow.ssl.keyStorePassword=111111
+
+
+# ssl 开启时,是否开启 http2。检测该配置是否生效在 chrome 地址栏中输入: chrome://net-internals/#http2
+undertow.http2.enable=true
+
+
+# ssl 开启时,http 请求是否重定向到 https
+# undertow.http.toHttps=false
+# ssl 开启时,http 请求重定向到 https 使用的状态码
+# undertow.http.toHttpsStatusCode=302
+# ssl 开启时,是否关闭 http
+# undertow.http.disable=false
+
+
+
--
Gitee
From 8573aa0df187085f615b0847383cad527bed9432 Mon Sep 17 00:00:00 2001
From: zjwave <594980947@qq.com>
Date: Sat, 16 Feb 2019 22:29:54 +0800
Subject: [PATCH 3/6] rollback
---
.../main/java/io/jpress/commons/utils/CommonsUtils.java | 7 -------
1 file changed, 7 deletions(-)
diff --git a/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java b/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java
index d2ee64a51..30bc41dbd 100644
--- a/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java
+++ b/jpress-commons/src/main/java/io/jpress/commons/utils/CommonsUtils.java
@@ -146,13 +146,6 @@ public class CommonsUtils {
.replace("&", "&");
}
- public static String removeHtmlTag(String content) {
- if (StrUtil.isBlank(content)) {
- return content;
- }
- return content.replaceAll("<[^>]*>", "");
- }
-
public static void main(String[] args) {
String script = "";
--
Gitee
From e7f055a98282729ee94d2eb0d2f51e5b11ab7049 Mon Sep 17 00:00:00 2001
From: zjwave <594980947@qq.com>
Date: Sat, 16 Feb 2019 22:31:38 +0800
Subject: [PATCH 4/6] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E9=AB=98=E4=BA=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../jpress/module/article/searcher/LuceneSearcher.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
index 6ea1b2370..7f522900f 100644
--- a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
+++ b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
@@ -19,6 +19,7 @@ import com.jfinal.kit.PathKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import io.jpress.commons.utils.CommonsUtils;
+import io.jpress.commons.utils.JsoupUtils;
import io.jpress.module.article.model.Article;
import io.jpress.module.article.service.search.ArticleSearcher;
import org.apache.lucene.analysis.Analyzer;
@@ -200,7 +201,7 @@ public class LuceneSearcher implements ArticleSearcher {
private List toArticleList(IndexSearcher searcher, TopDocs topDocs,Highlighter highlighter,String keyword) throws IOException {
List articles = new ArrayList<>();
- Analyzer luceneAnalyzer = new CJKAnalyzer();
+ Analyzer analyzer = new JcsegAnalyzer(JcsegTaskConfig.COMPLEX_MODE);
for (ScoreDoc item : topDocs.scoreDocs) {
Document doc = searcher.doc(item.doc);
Article article = new Article();
@@ -211,12 +212,11 @@ public class LuceneSearcher implements ArticleSearcher {
article.setContent(content);
//关键字高亮
try {
- String highlightTitle = highlighter.getBestFragment(luceneAnalyzer.tokenStream(keyword, new StringReader(title)), title);
+ String highlightTitle = highlighter.getBestFragment(analyzer.tokenStream(keyword, new StringReader(title)), title);
article.setHighlightTitle(highlightTitle);
- String plainContent = CommonsUtils.removeHtmlTag(content);
- plainContent.replaceAll("\\s*|\t|\r|\n","");
- String highlightContent = highlighter.getBestFragment(luceneAnalyzer.tokenStream(keyword, new StringReader(plainContent)), plainContent);
+ String plainContent = JsoupUtils.getText(content);
+ String highlightContent = highlighter.getBestFragment(analyzer.tokenStream(keyword, new StringReader(plainContent)), plainContent);
article.setHighlightContent(highlightContent);
} catch (InvalidTokenOffsetsException e) {
logger.error(e.getMessage(),e);
--
Gitee
From 672fada3f24e5b6d32a6d8098bad97b0ffbc65cd Mon Sep 17 00:00:00 2001
From: zjwave <594980947@qq.com>
Date: Sat, 16 Feb 2019 22:33:20 +0800
Subject: [PATCH 5/6] rollback
---
.../java/io/jpress/module/article/searcher/LuceneSearcher.java | 1 -
1 file changed, 1 deletion(-)
diff --git a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
index 7f522900f..27c73ad0a 100644
--- a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
+++ b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
@@ -23,7 +23,6 @@ import io.jpress.commons.utils.JsoupUtils;
import io.jpress.module.article.model.Article;
import io.jpress.module.article.service.search.ArticleSearcher;
import org.apache.lucene.analysis.Analyzer;
-import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
--
Gitee
From f7c16423ba10c28373a7074d5657c034e6c8f325 Mon Sep 17 00:00:00 2001
From: zjwave <594980947@qq.com>
Date: Sat, 16 Feb 2019 22:54:22 +0800
Subject: [PATCH 6/6] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E9=AB=98=E4=BA=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../jpress/module/article/searcher/LuceneSearcher.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
index 27c73ad0a..19552352b 100644
--- a/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
+++ b/module-article/module-article-search-lucene/src/main/java/io/jpress/module/article/searcher/LuceneSearcher.java
@@ -161,6 +161,7 @@ public class LuceneSearcher implements ArticleSearcher {
Document doc = new Document();
doc.add(new StringField("aid", article.getId().toString(), Field.Store.YES));
doc.add(new TextField("content", article.getContent(), Field.Store.YES));
+ doc.add(new TextField("text", article.getText(), Field.Store.YES));
doc.add(new TextField("title", article.getTitle(), Field.Store.YES));
doc.add(new StringField("created", DateTools.dateToString(article.getCreated() == null ? new Date() : article.getCreated(), DateTools.Resolution.YEAR), Field.Store.NO));
return doc;
@@ -177,8 +178,8 @@ public class LuceneSearcher implements ArticleSearcher {
try {
Analyzer analyzer = new JcsegAnalyzer(JcsegTaskConfig.COMPLEX_MODE);
-
- QueryParser queryParser1 = new QueryParser("content", analyzer);
+ //这里使用text,防止搜索出html的tag或者tag中属性
+ QueryParser queryParser1 = new QueryParser("text", analyzer);
Query termQuery1 = queryParser1.parse(keyword);
BooleanClause booleanClause1 = new BooleanClause(termQuery1, BooleanClause.Occur.SHOULD);
@@ -213,9 +214,8 @@ public class LuceneSearcher implements ArticleSearcher {
try {
String highlightTitle = highlighter.getBestFragment(analyzer.tokenStream(keyword, new StringReader(title)), title);
article.setHighlightTitle(highlightTitle);
-
- String plainContent = JsoupUtils.getText(content);
- String highlightContent = highlighter.getBestFragment(analyzer.tokenStream(keyword, new StringReader(plainContent)), plainContent);
+ String text = article.getText();
+ String highlightContent = highlighter.getBestFragment(analyzer.tokenStream(keyword, new StringReader(text)), text);
article.setHighlightContent(highlightContent);
} catch (InvalidTokenOffsetsException e) {
logger.error(e.getMessage(),e);
--
Gitee