提交 962e3f7e authored 作者: Andrei Tokar's avatar Andrei Tokar

bring lucene support up to 5.5.5 - last java7-compatible version

上级 da2fcc08
......@@ -56,7 +56,17 @@
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.2</version>
<version>5.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>5.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>5.5.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
......
......@@ -5,8 +5,8 @@
*/
package org.h2.fulltext;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
......@@ -16,18 +16,20 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
......@@ -35,7 +37,6 @@ import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.h2.api.Trigger;
import org.h2.command.Parser;
import org.h2.engine.Session;
......@@ -296,9 +297,9 @@ public class FullTextLucene extends FullText {
if (access == null) {
try {
Directory indexDir = path.startsWith(IN_MEMORY_PREFIX) ?
new RAMDirectory() : FSDirectory.open(new File(path));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_30, analyzer);
new RAMDirectory() : FSDirectory.open(Paths.get(path));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter writer = new IndexWriter(indexDir, conf);
//see http://wiki.apache.org/lucene-java/NearRealtimeSearch
......@@ -416,17 +417,18 @@ public class FullTextLucene extends FullText {
// reuse the same analyzer; it's thread-safe;
// also allows subclasses to control the analyzer used.
Analyzer analyzer = access.writer.getAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_30,
LUCENE_FIELD_DATA, analyzer);
Query query = parser.parse(text);
// Lucene 3 insists on a hard limit and will not provide
StandardQueryParser parser = new StandardQueryParser(analyzer);
Query query = parser.parse(text, LUCENE_FIELD_DATA);
// Lucene insists on a hard limit and will not provide
// a total hits value. Take at least 100 which is
// an optimal limit for Lucene as any more
// will trigger writing results to disk.
int maxResults = (limit == 0 ? 100 : limit) + offset;
TopDocs docs = searcher.search(query, maxResults);
if (limit == 0) {
limit = docs.totalHits;
// TopDocs.totalHits is long now (https://issues.apache.org/jira/browse/LUCENE-7872)
// but in this context it's save to cast
limit = (int)docs.totalHits;
}
for (int i = 0, len = docs.scoreDocs.length; i < limit
&& i + offset < docs.totalHits
......@@ -476,6 +478,14 @@ public class FullTextLucene extends FullText {
private String indexPath;
private IndexAccess indexAccess;
private final FieldType DOC_ID_FIELD_TYPE;
public FullTextTrigger() {
DOC_ID_FIELD_TYPE = new FieldType(TextField.TYPE_STORED);
DOC_ID_FIELD_TYPE.setTokenized(false);
DOC_ID_FIELD_TYPE.freeze();
}
/**
* INTERNAL
*/
......@@ -598,12 +608,11 @@ public class FullTextLucene extends FullText {
protected void insert(Object[] row, boolean commitIndex) throws SQLException {
String query = getQuery(row);
Document doc = new Document();
doc.add(new Field(LUCENE_FIELD_QUERY, query,
Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field(LUCENE_FIELD_QUERY, query, DOC_ID_FIELD_TYPE));
long time = System.currentTimeMillis();
doc.add(new Field(LUCENE_FIELD_MODIFIED,
DateTools.timeToString(time, DateTools.Resolution.SECOND),
Field.Store.YES, Field.Index.NOT_ANALYZED));
TextField.TYPE_STORED));
StatementBuilder buff = new StatementBuilder();
for (int index : indexColumns) {
String columnName = columns[index];
......@@ -614,15 +623,13 @@ public class FullTextLucene extends FullText {
if (columnName.startsWith(LUCENE_FIELD_COLUMN_PREFIX)) {
columnName = LUCENE_FIELD_COLUMN_PREFIX + columnName;
}
doc.add(new Field(columnName, data,
Field.Store.NO, Field.Index.ANALYZED));
doc.add(new Field(columnName, data, TextField.TYPE_NOT_STORED));
buff.appendExceptFirst(" ");
buff.append(data);
}
Field.Store storeText = STORE_DOCUMENT_TEXT_IN_INDEX ?
Field.Store.YES : Field.Store.NO;
doc.add(new Field(LUCENE_FIELD_DATA, buff.toString(), storeText,
Field.Index.ANALYZED));
FieldType dataFieldType = STORE_DOCUMENT_TEXT_IN_INDEX ?
TextField.TYPE_STORED : TextField.TYPE_NOT_STORED;
doc.add(new Field(LUCENE_FIELD_DATA, buff.toString(), dataFieldType));
try {
indexAccess.writer.addDocument(doc);
if (commitIndex) {
......@@ -675,23 +682,13 @@ public class FullTextLucene extends FullText {
/**
* A wrapper for the Lucene writer and searcher.
*/
static final class IndexAccess {
private static final class IndexAccess {
/**
* The index writer.
*/
final IndexWriter writer;
/**
* Map of usage counters for outstanding searchers.
*/
private final Map<IndexSearcher,Integer> counters = new HashMap<>();
/**
* Usage counter for current searcher.
*/
private int counter;
/**
* The index searcher.
*/
......@@ -699,8 +696,7 @@ public class FullTextLucene extends FullText {
IndexAccess(IndexWriter writer) throws IOException {
this.writer = writer;
IndexReader reader = IndexReader.open(writer, true);
searcher = new IndexSearcher(reader);
initializeSearcher();
}
/**
......@@ -708,29 +704,25 @@ public class FullTextLucene extends FullText {
*
* @return the searcher
*/
synchronized IndexSearcher getSearcher() {
++counter;
synchronized IndexSearcher getSearcher() throws IOException {
if (!searcher.getIndexReader().tryIncRef()) {
initializeSearcher();
}
return searcher;
}
private void initializeSearcher() throws IOException {
IndexReader reader = DirectoryReader.open(writer);
searcher = new IndexSearcher(reader);
}
/**
* Stop using the searcher.
*
* @param searcher the searcher
*/
synchronized void returnSearcher(IndexSearcher searcher) {
if (this.searcher == searcher) {
--counter;
assert counter >= 0;
} else {
Integer cnt = counters.remove(searcher);
assert cnt != null;
if(--cnt == 0) {
closeSearcher(searcher);
} else {
counters.put(searcher, cnt);
}
}
synchronized void returnSearcher(IndexSearcher searcher) throws IOException {
searcher.getIndexReader().decRef();
}
/**
......@@ -738,33 +730,16 @@ public class FullTextLucene extends FullText {
*/
public synchronized void commit() throws IOException {
writer.commit();
if (counter != 0) {
counters.put(searcher, counter);
counter = 0;
} else {
closeSearcher(searcher);
}
// recreate Searcher with the IndexWriter's reader.
searcher = new IndexSearcher(IndexReader.open(writer, true));
returnSearcher(searcher);
searcher = new IndexSearcher(DirectoryReader.open(writer));
}
/**
* Close the index.
*/
public synchronized void close() throws IOException {
for (IndexSearcher searcher : counters.keySet()) {
closeSearcher(searcher);
}
counters.clear();
closeSearcher(searcher);
searcher = null;
writer.close();
}
private static void closeSearcher(IndexSearcher searcher) {
IndexReader indexReader = searcher.getIndexReader();
try { searcher.close(); } catch(IOException ignore) {/**/}
try { indexReader.close(); } catch(IOException ignore) {/**/}
}
}
}
......@@ -184,7 +184,11 @@ public class Build extends BuildBase {
String cp = "coverage/bin" +
File.pathSeparator + "ext/postgresql-42.2.1.jre7" +
File.pathSeparator + "ext/servlet-api-3.1.0.jar" +
File.pathSeparator + "ext/lucene-core-3.6.2.jar" +
File.pathSeparator + "ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queries-5.5.5.jar" +
File.pathSeparator + "ext/lucene-sandbox-5.5.5.jar" +
File.pathSeparator + "ext/h2mig_pagestore_addon.jar" +
File.pathSeparator + "ext/org.osgi.core-4.2.0.jar" +
File.pathSeparator + "ext/org.osgi.enterprise-4.2.0.jar" +
......@@ -263,7 +267,11 @@ public class Build extends BuildBase {
download();
String classpath = "temp" +
File.pathSeparator + "ext/servlet-api-3.1.0.jar" +
File.pathSeparator + "ext/lucene-core-3.6.2.jar" +
File.pathSeparator + "ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queries-5.5.5.jar" +
File.pathSeparator + "ext/lucene-sandbox-5.5.5.jar" +
File.pathSeparator + "ext/slf4j-api-1.6.0.jar" +
File.pathSeparator + "ext/org.osgi.core-4.2.0.jar" +
File.pathSeparator + "ext/org.osgi.enterprise-4.2.0.jar" +
......@@ -358,9 +366,21 @@ public class Build extends BuildBase {
downloadOrVerify("ext/servlet-api-3.1.0.jar",
"javax/servlet", "javax.servlet-api", "3.1.0",
"3cd63d075497751784b2fa84be59432f4905bf7c", offline);
downloadOrVerify("ext/lucene-core-3.6.2.jar",
"org/apache/lucene", "lucene-core", "3.6.2",
"9ec77e2507f9cc01756964c71d91efd8154a8c47", offline);
downloadOrVerify("ext/lucene-core-5.5.5.jar",
"org/apache/lucene", "lucene-core", "5.5.5",
"d9e58b36578571bb85e55e26ea5834036915dfdf", offline);
downloadOrVerify("ext/lucene-analyzers-common-5.5.5.jar",
"org/apache/lucene", "lucene-analyzers-common", "5.5.5",
"e6b3f5d1b33ed24da7eef0a72f8062bd4652700c", offline);
downloadOrVerify("ext/lucene-queryparser-5.5.5.jar",
"org/apache/lucene", "lucene-queryparser", "5.5.5",
"f32d05577e57e413e4731f8b2aabbce00a1e1cb8", offline);
downloadOrVerify("ext/lucene-queries-5.5.5.jar",
"org/apache/lucene", "lucene-queries", "5.5.5",
"d99719e7c58c149113f897bca301f1d68cbf3241", offline);
downloadOrVerify("ext/lucene-sandbox-5.5.5.jar",
"org/apache/lucene", "lucene-sandbox", "5.5.5",
"d145d959109257c47151be43b211213dff455f47", offline);
downloadOrVerify("ext/slf4j-api-1.6.0.jar",
"org/slf4j", "slf4j-api", "1.6.0",
"b353147a7d51fcfcd818d8aa6784839783db0915", offline);
......@@ -626,7 +646,11 @@ public class Build extends BuildBase {
javadoc("-sourcepath", "src/main", "org.h2.jdbc", "org.h2.jdbcx",
"org.h2.tools", "org.h2.api", "org.h2.engine", "org.h2.fulltext",
"-classpath",
"ext/lucene-core-3.6.2.jar" +
"ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queries-5.5.5.jar" +
File.pathSeparator + "ext/lucene-sandbox-5.5.5.jar" +
File.pathSeparator + "ext/jts-core-1.15.0.jar",
"-docletpath", "bin" + File.pathSeparator + "temp",
"-doclet", "org.h2.build.doclet.Doclet");
......@@ -650,7 +674,11 @@ public class Build extends BuildBase {
"-classpath", javaToolsJar +
File.pathSeparator + "ext/slf4j-api-1.6.0.jar" +
File.pathSeparator + "ext/servlet-api-3.1.0.jar" +
File.pathSeparator + "ext/lucene-core-3.6.2.jar" +
File.pathSeparator + "ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queries-5.5.5.jar" +
File.pathSeparator + "ext/lucene-sandbox-5.5.5.jar" +
File.pathSeparator + "ext/org.osgi.core-4.2.0.jar" +
File.pathSeparator + "ext/org.osgi.enterprise-4.2.0.jar" +
File.pathSeparator + "ext/jts-core-1.15.0.jar",
......@@ -664,7 +692,11 @@ public class Build extends BuildBase {
"-classpath", javaToolsJar +
File.pathSeparator + "ext/slf4j-api-1.6.0.jar" +
File.pathSeparator + "ext/servlet-api-3.1.0.jar" +
File.pathSeparator + "ext/lucene-core-3.6.2.jar" +
File.pathSeparator + "ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queries-5.5.5.jar" +
File.pathSeparator + "ext/lucene-sandbox-5.5.5.jar" +
File.pathSeparator + "ext/org.osgi.core-4.2.0.jar" +
File.pathSeparator + "ext/org.osgi.enterprise-4.2.0.jar" +
File.pathSeparator + "ext/jts-core-1.15.0.jar",
......@@ -679,7 +711,11 @@ public class Build extends BuildBase {
"-classpath", javaToolsJar +
File.pathSeparator + "ext/slf4j-api-1.6.0.jar" +
File.pathSeparator + "ext/servlet-api-3.1.0.jar" +
File.pathSeparator + "ext/lucene-core-3.6.2.jar" +
File.pathSeparator + "ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queries-5.5.5.jar" +
File.pathSeparator + "ext/lucene-sandbox-5.5.5.jar" +
File.pathSeparator + "ext/org.osgi.core-4.2.0.jar" +
File.pathSeparator + "ext/org.osgi.enterprise-4.2.0.jar" +
File.pathSeparator + "ext/jts-core-1.15.0.jar",
......@@ -882,7 +918,9 @@ public class Build extends BuildBase {
java("org.h2.build.doc.GenerateHelp", null);
javadoc("-sourcepath", "src/main", "org.h2.tools", "org.h2.jmx",
"-classpath",
"ext/lucene-core-3.6.2.jar" +
"ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/jts-core-1.15.0.jar",
"-docletpath", "bin" + File.pathSeparator + "temp",
"-doclet", "org.h2.build.doclet.ResourceDoclet");
......@@ -934,7 +972,11 @@ public class Build extends BuildBase {
String cp = "temp" + File.pathSeparator + "bin" +
File.pathSeparator + "ext/postgresql-42.2.1.jre7.jar" +
File.pathSeparator + "ext/servlet-api-3.1.0.jar" +
File.pathSeparator + "ext/lucene-core-3.6.2.jar" +
File.pathSeparator + "ext/lucene-core-5.5.5.jar" +
File.pathSeparator + "ext/lucene-analyzers-common-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queryparser-5.5.5.jar" +
File.pathSeparator + "ext/lucene-queries-5.5.5.jar" +
File.pathSeparator + "ext/lucene-sandbox-5.5.5.jar" +
File.pathSeparator + "ext/h2mig_pagestore_addon.jar" +
File.pathSeparator + "ext/org.osgi.core-4.2.0.jar" +
File.pathSeparator + "ext/org.osgi.enterprise-4.2.0.jar" +
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论