简体   繁体   English

如何使用Lucene查询ElasticSearch索引

[英]How to use Lucene to query ElasticSearch index

Can I use Lucene to query an ElasticSearch index? 我可以使用Lucene查询ElasticSearch索引吗?

Using ElasticSearch I created an index and inserted these three documents: 使用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"}'

So, I have one index, two types, and three documents. 所以,我有一个索引,两个类型和三个文档。 Now, I would like to search for these using standard Lucene. 现在,我想使用标准的Lucene来搜索这些内容。 Using a hex editor, I identified which shard has the indexed documents, and I can successfully query that index. 使用十六进制编辑器,我确定哪个分片具有索引文档,并且我可以成功查询该索引。 I can't figure out though, how to retrieve the field values from the matching document(s). 我无法弄清楚,如何从匹配的文档中检索字段值。

The following program successfully searches but is unable to retrieve results. 以下程序成功搜索但无法检索结果。

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

The _uid value above is correct. 上面的_uid值是正确的。

Eclipse shows me that the variable Document d does have two fields: Eclipse向我展示变量Document d确实有两个字段:

  • stored,indexed,tokenized,omitNorms<_uid:type2#3K5QXeZhQnit9UXM9_4bng> 存储,索引,标记化,omitNorms <_uid:类型2#3K5QXeZhQnit9UXM9_4bng>
  • stored<_source:[7b 22 66 32 22 3a 22 63 61 74 22 7d]> 储存<_source:[7b 22 66 32 22 3a 22 63 61 74 22 7d]>

Unfortunately, d.get("_source") also returns null. 不幸的是,d.get(“_ source”)也返回null。

How can I retrieve the document fields for a matching query? 如何检索匹配查询的文档字段?

Thank you. 谢谢。

As stated in the comment, I needed to retrieve the field "_source" as a binary value. 如评论中所述,我需要将字段“_source”检索为二进制值。 So this worked: d.getBinaryValue("_source") and it retrieved [7b 22 66 32 22 3a 22 63 61 74 22 7d] which is {"f2":"cat"}. 所以这工作:d.getBinaryValue(“_ source”)并检索[7b 22 66 32 22 3a 22 63 61 74 22 7d],这是{“f2”:“cat”}。 Javanna, thanks for helping. Javanna,谢谢你的帮助。

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

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