繁体   English   中英

Lucene如何在数据库中建立索引(Cassandra)

[英]Lucene how to index in Database (Cassandra)

我只是在尝试使用Lucene,并且希望将Database(Cassandra)中的对象索引为表。 但是,我没有意识到索引在Cassandra上是如何工作的。 尤其是搜寻...

当我举一个简单的例子

在Lucene中建立索引:

Document doc = new Document();
doc.add(new TextField("id", "Hotel-1345", Field.Store.YES));
doc.add(new TextField("description", "A beautiful hotel", Field.Store.YES));
writer.addDocument(doc);

然后Lucene将创建索引文件(C:..),并且使用indexSearcher进行查询时效果很好。 但...

索引数据库? 我想我不能像那样创建一个简单的表

Table1:  
ID        , Description
------------------
Hotel-1345, A beautiful hotel

Lucene的搜索过程如何工作? 数据库结构应该如何?

希望,我得到一些答案:/

它看起来不会像关系数据库表,而是Lucene使用倒排索引和余弦相似度公式搜索任何搜索词。

为了更好地理解您需要寻找用于Lucene的各种术语和公式,您可以在lucene官方网站https://lucene.apache.org/core/3_5_0/scoring.html#Query%20Classes上进行检查。

要索引数据,您需要使用Lucene索引器和搜索器API,请使用此伪代码进行索引:

    Directory directory = null;
    IndexWriter writer = null;
    try {
        directory = FSDirectory.open(new File(args[0]));
        IndexWriterConfig iwConf = new IndexWriterConfig(Version.LUCENE_48,
                new KeywordAnalyzer());
        iwConf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        writer = new IndexWriter(directory, iwConf);
        Document document = new Document();
        document.add(new StringField("title", "Cat", Store.YES));
        writer.addDocument(document);
        writer.commit();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM