2013-05-29 111 views
2

我可以使用Lucene查询ElasticSearch索引吗?如何使用Lucene查询ElasticSearch索引

使用ElasticSearch我创建的索引和插入这三个文件:

$ curl -XPOST localhost:9200/index1/type1 -d '{"f1":"dog"}' 
$ curl -XPOST localhost:9200/index1/type2 -d '{"f2":"cat"}' 
$ curl -XPOST localhost:9200/index1/type2 -d '{"f3":"horse"}' 

所以,我有一个索引,两种类型,和三个文件。现在,我想用标准的Lucene搜索这些。使用十六进制编辑器,我确定哪个分片具有索引文档,并且我可以成功查询该索引。我不知道,但如何从匹配的文档中检索字段值。

以下程序成功搜索但无法检索结果。

import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.index.DirectoryReader; 
import org.apache.lucene.index.IndexReader; 
import org.apache.lucene.queryparser.classic.QueryParser; 
import org.apache.lucene.search.IndexSearcher; 
import org.apache.lucene.search.Query; 
import org.apache.lucene.search.ScoreDoc; 
import org.apache.lucene.search.TopScoreDocCollector; 
import org.apache.lucene.store.Directory; 
import org.apache.lucene.store.FSDirectory; 
import org.apache.lucene.util.Version; 

import java.io.File; 

public class TestES { 

void doWork(String[] args) throws Exception { 
    // Index reader for already created ElasticSearch index 
    String indx1 = "/path-to-index/elasticsearch-0.90.0.RC2-SNAPSHOT/data/elasticsearch/nodes/0/indices/index1/1/index"; 
    Directory index = FSDirectory.open(new File(indx1)); 
    IndexReader reader = DirectoryReader.open(index); 
    IndexSearcher searcher = new IndexSearcher(reader); 

    // Looks like the query is correct since we do get a hit 
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_41); 
    Query q = new QueryParser(Version.LUCENE_41, "f2", analyzer).parse("cat"); 
    TopScoreDocCollector collector = TopScoreDocCollector.create(10, true); 
    searcher.search(q, collector); 
    ScoreDoc[] hits = collector.topDocs().scoreDocs; 

    // We do get a hit, but results always displayed as null except for "_uid" 
    if (hits.length > 0) { 
     int docId = hits[0].doc; 
     Document d = searcher.doc(docId); 
     System.out.println("DocID " + docId + ", _uid: " + d.get("_uid")); 
     System.out.println("DocID " + docId + ", f2: " + d.get("f2")); 
    } 
    reader.close(); 
} 

public static void main(String[] args) throws Exception { 
    TestES hl = new TestES(); 
    hl.doWork(args); 
} 
} 

Results: 
DocID 0, _uid: type2#3K5QXeZhQnit9UXM9_4bng 
DocID 0, f2: null 

上面的_uid值是正确的。

的Eclipse显示我该变量文献d确实有两个字段:

  • 存储,索引,标记化,omitNorms < _uid:TYPE2#3K5QXeZhQnit9UXM9_4bng>
  • 存储< _source:[7B 22 66 32 22 3a 22 63 61 74 22 7d]>

不幸的是,d.get(“_ source”)也返回null。

如何检索匹配查询的文档字段?

谢谢。

+0

那么,首先我会问你为什么你让自己变得比应该更难:)不管怎样,你做得很对,'_source'字段默认存储并且包含你发送的整个文档进行弹性搜索。您必须检索它并将其解析为json文档。不知道为什么你得到空。你确定你使用的是正确的lucene版本吗? – javanna

+0

我担心有人会问这个问题:)是的,我证实我正在运行elasticsearch-0.90.0.RC2-SNAPSHOT/bin,Lucene jar在elasticsearch-0.90.0.RC2-SNAPSHOT/lib中。我仍然无法检索“_source” – user2434291

+1

啊,我明白了。有趣的是,我需要检索字段“_source”作为二进制值。所以这工作:d.getBinaryValue(“_源”),它检索[7b 22 66 32 22 3a 22 63 61 74 22 7d]这是{“f2”:“猫”} – user2434291

回答

0

正如评论所述,我需要检索字段“_source”作为二进制值。所以这工作:d.getBinaryValue(“_ source”),它检索到[7b 22 66 32 22 3a 22 63 61 74 22 7d],它是{“f2”:“cat”}。 Javanna,谢谢你的帮助。

相关问题