学习LuceneSail(论文+代码举例)

本文介绍如何将Lucene集成到Sesame中形成LuceneSail,以实现RDF数据的结构化查询与全文搜索。具体展示了基于NativeStore生成索引的过程,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 论文

1,论文出处 

         The Sesame LuceneSail: RDF queries with full-text search  (2008年发表)

2,论文笔记

      简单说,LuceneSail就是集成lucene到sesame。它是结构化RDF查询和全文搜索的一个结合,sesame (RDF store)和lucene(文本搜索库)。

        Motivation :  SPARQL只能字符串全匹配或过滤,操作慢。

luceneSail 文档:此处

下面分别简单介绍sesame和lucene.

sesame

sesame是一个RDF store,可以存储RDF文件并且对RDF进行查询等操作。它可以使用不同的后端,比如关系数据库、或者原生RDF文件。像JAVA的JDBC连接一样,sesame也需要连接,sesame连接使用SALL Connection(SALL对象)。通过连接,可以进行添加(adding)、移除(removing)、查询(query)、事务管理等操作。

连接集中:事务的清除处理和对RDF store的并发访问。

Lucene

lucene是一个全文搜索引擎库,每个字段包含字符串名称和字符串的值。每一个被检索的文档提供一个评分(根据TFIDF测量的相关性)。查询时,必须指定要查询的字段,故不可能一次只查询所有字段。因此,最好把所有文本再次存储在一个索引的字段中,就可以在所有字段上快速查询。

         图1  Lucene实现全文检索的流程

  1、绿色表示索引过程,对要搜索的原始内容进行索引构建一个索引库,索引过程包括:

    确定原始内容即要搜索的内容→采集文档→创建文档→分析文档→索引文档

  2、红色表示搜索过程,从索引库中搜索内容,搜索过程包括:

    用户通过搜索界面→创建查询→执行搜索,从索引库搜索→渲染搜索结果

 

 

二.代码

详细代码已上传至github:   https://github.com/kathy775/LuceneSail-Demo

实验环境:

java1.7

需要sesame的jar包

代码:

基于nativeStore生成索引

import java.io.File;
import java.util.Scanner;
import org.openrdf.query.Binding;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.rio.RDFFormat;
import org.openrdf.sail.lucene.LuceneSail;
import org.openrdf.sail.lucene.LuceneSailSchema;
import org.openrdf.sail.memory.MemoryStore;
import org.openrdf.sail.nativerdf.NativeStore;
/**
 * Example code showing how to use the LuceneSail
 */
public class DBPLuceneSailIndex {
	public static void main(String[] args) throws Exception {		 
		 String index_path = "/home/LuceneSailMemo/dbpedia";	 
		 createSimple(index_path);	
	}

	/**
	 * Create a LuceneSail and add some triples to it, ask a query.
	 */
	public static void createSimple(String index_path ) throws Exception {
		// create a sesame memory sail
		NativeStore myStore = new NativeStore();
		File dataDir = new File(index_path);
		myStore.setDataDir(dataDir);
		// create a lucenesail to wrap the memorystore
		LuceneSail lucenesail = new LuceneSail();
		// set this parameter to let the lucene index store its data in ram
		lucenesail.setParameter(LuceneSail.LUCENE_DIR_KEY, "true");
		// set this parameter to store the lucene index on disk
		// lucenesail.setParameter(LuceneSail.LUCENE_DIR_KEY, "./data/mydirectory");

		// wrap memorystore in a lucenesail
		lucenesail.setBaseSail(myStore);
		// create a Repository to access the sails
		SailRepository repository = new SailRepository(lucenesail);
		repository.initialize();
		SailRepositoryConnection connection = repository.getConnection();
		// connection.begin();
		try {
			// connection.setAutoCommit(false);
			// System.out.println(System.getProperty("user.dir"));  

			String file_path = "file path";
                        File file = new File(file_path);
			System.out.println(file.exists());
                        connection.add(file, "", RDFFormat.NTRIPLES);	

			connection.commit();
			System.out.println("------ 指数文件已生成 -----");
		} finally {
			connection.close();
			repository.shutDown();
		}
	}
}

 

再回首,在更新……

 

下次:luceneSail查询部分代码。memoryStore代码。细节补充

 

2018年12月14日 更新 ……

代码已上传至github:   https://github.com/kathy775/LuceneSail-Demo

包括:

基于nativeStore生成索引

基于memoryStore生成索引

使用 LuceneSail 查询 关键字

 

下次:查询的细节补充

 

      

      

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值