According to document Jena Full Text Search, it is possible that the indexed text is content external to the RDF store with only additional triples in the RDF store. The subject URI returned as a search result may then be considered to refer via the indexed property to the external content.
To work with external content, the maintenance of the index is external to the RDF data store too. The key of the index is: building an URI field from indexed document's path.
When the text search is performed, the URIs returned as the search result will be used for matching the subject URI in the SPARQL query.
In this tutorial, we create lucene index for external text documents, perform a full text search in SPARQL using jena-text, then return the highlighting results.
The text files under directory to_index are the external files which will be used to create a Lucene index. File name of each text file is the email id, corresponding to the email id in the turtle file.
$ ls -la to_index/
total 16
drwxrwxr-x 2 hy hy 4096 12月 6 08:44 .
drwxrwxr-x 7 hy hy 4096 12月 13 15:06 ..
-rw-rw-r-- 1 hy hy 30 12月 6 08:44 id1
-rw-rw-r-- 1 hy hy 28 12月 6 08:44 id2
$ cat to_index/id1
context
good luck
background
$ cat to_index/id2
context
bad luck
background
// in file IndexFiles.java
/** Indexes a single document */
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
try (InputStream stream = Files.newInputStream(file)) {
// make a new, empty document
Document doc = new Document();
// Add the path of the file as a field named "path". Use a
// field that is indexed (i.e. searchable), but don't tokenize
// the field into separate words and don't index term frequency
// or positional information:
Field pathField = new StringField("uri", App.EMAIL_URI_PREFIX + "id/" + file.getFileName().toString(), Field.Store.YES);
doc.add(pathField);
// Add the last modified date of the file a field named "modified".
// Use a LongPoint that is indexed (i.e. efficiently filterable with
// PointRangeQuery). This indexes to milli-second resolution, which
// is often too fine. You could instead create a number based on
// year/month/day/hour/minutes/seconds, down the resolution you require.
// For example the long value 2011021714 would mean
// February 17, 2011, 2-3 PM.
doc.add(new LongPoint("modified", lastModified));
// Add the contents of the file to a field named "contents". Specify a Reader,
// so that the text of the file is tokenized and indexed, but not stored.
// Note that FileReader expects the file to be in UTF-8 encoding.
// If that's not the case searching for special characters will fail.
//doc.add(new TextField("text", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
doc.add(new TextField("text", new String(Files.readAllBytes(file)), Field.Store.YES));
if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
// New index, so we just add the document (no old document can be there):
System.out.println("adding " + file);
writer.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been indexed) so
// we use updateDocument instead to replace the old one matching the exact
// path, if present:
System.out.println("updating " + file);
writer.updateDocument(new Term("path", file.toString()), doc);
}
}
}
}
With in line Field pathField = new StringField("uri", App.EMAIL_URI_PREFIX + "id/" + file.getFileName().toString(), Field.Store.YES);, we create a StringField uri from the external text file's name. And Field.Store.YES indicates the text file's content will be stored in the index, which is used for working with jena-text's full text search highlighting feature.
Load dataset and define the index mapping
// in file JenaTextSearch.java
public static Dataset createCode()
{
// Base data
Dataset ds1 = DatasetFactory.create() ;
Model defaultModel = ModelFactory.createDefaultModel();
defaultModel.read("data.ttl", "N-TRIPLES");
ds1.setDefaultModel(defaultModel);
// Define the index mapping
EntityDefinition entDef = new EntityDefinition( "uri", "text", ResourceFactory.createProperty( App.EMAIL_URI_PREFIX, "content" ) );
Directory dir = null;
try {
dir = new SimpleFSDirectory(Paths.get("index")); // lucene index directory
}
catch( IOException e){
e.printStackTrace();
}
// Join together into a dataset
Dataset ds = TextDatasetFactory.createLucene( ds1, dir, new TextIndexConfig(entDef) ) ;
return ds ;
}
We define the index mapping according to the index we built. The index itself is maintained by the main app, see code in below.
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return&
在JDK1.5之前的单例实现方式有两种(懒汉式和饿汉式并无设计上的区别故看做一种),两者同是私有构
造器,导出静态成员变量,以便调用者访问。
第一种
package singleton;
public class Singleton {
//导出全局成员
public final static Singleton INSTANCE = new S