简体   繁体   English

Apache Lucene中的多个属性查询

[英]Multiple attribute queries in Apache Lucene

The below program satisfies the query where title has both lucene and action. 下面的程序满足标题具有lucene和action的查询。 If I want to search for a tupple where isbn (considering isbn is not unique) is 1234 and title contains both Lucene and dummies. 如果我想搜索一个isbn(考虑isbn不是唯一的)为1234且标题同时包含Lucene和虚拟变量的连音。 Does lucene provide a facility for that. lucene是否为此提供便利。

 StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
 Directory index = new RAMDirectory();

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);

 IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action", "193398817");
 addDoc(w, "Lucene for Dummies", "55320055Z");
 addDoc(w, "Managing Gigabytes", "55063554A");
  addDoc(w, "The Art of Computer Science", "9900333X");
   w.close();

 private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
Document doc = new Document();
doc.add(new TextField("title", title, Field.Store.YES));
doc.add(new StringField("isbn", isbn, Field.Store.YES));
 w.addDocument(doc);
 } 


String querystr = args.length > 0 ? args[0] : "lucene AND action";
Query q = new QueryParser(Version.LUCENE_40, "title", analyzer).parse(querystr);

From the top of my head , your QueryParser class is built to query the title field only, so in order to make a query that targets both title and isbn fields you have to make use of a class like MultiFieldQueryParser and a query like title:(lucene AND dummies) AND isbn:1234 or just build your BooleanQuery (this is what you end up with) by hand from multiple TermQuery objects . 从我的头开始,您的QueryParser类仅用于查询title字段,因此,要进行以titleisbn字段为目标的查询,您必须使用MultiFieldQueryParser这样的类和title:(lucene AND dummies) AND isbn:1234这样的查询title:(lucene AND dummies) AND isbn:1234 BooleanQuery title:(lucene AND dummies) AND isbn:1234或者只是从多个TermQuery对象中手动构建BooleanQuery (最终得到的结果)。

I hope this helps 我希望这有帮助

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

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