lucene中QueryParser的使用查询示例

在Lucene in action第2版中,QueryParser用的构造方法是QueryParser parser = new QueryParser(String field, Analyzer analyzer)

我在lucene3.4中找不到这个方法,我用的是

QueryParser queryParser = new QueryParser(Version.LUCENE_34,"contents",analyzer);

在Query query = queryParser.parse(str);方法中,表达式可以使用如:"+cat -dog"

下面列出的是一些查询表达式

表达式 匹配文档
java 在字段中包含java
java junit
java or junit
在字段中包含java或者junit
+java +junit
java and junit
在字段中包含java以及junit
title:ant 在title字段中包含ant
title:extreme
-subject:sports
title:extreme
AND NOT subject:sports
在title字段中包含extreme并且在subject字段中不能包含sports
(agile OR extreme) AND methodology 在字段中包含methodology并且同时包括agile或者extreme
title:"junit in action" 在title字段中包含junit in action
title:"junit action"~5 包含5次junit和action
java* 包含以java开头的,例如:javaspaces,javaserver
java~ 包含和java相似的,如lava
lastmodified:[1/1/04 TO 12/31/04] 在lastmodified字段中值为2004-01-01到2004-12-31中间的

下面是一个小示例,注意其中 java AND jbpm中的AND一定要大写


package com.cn;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class T {
    
    public static void main(String []args)throws Exception {
        
        String []id = {"1","2","3"};
        String []contents = {"java and lucene is good","I had study java and jbpm","I want to study java,hadoop and hbase"};
        
        Directory directory = new RAMDirectory();
        IndexWriter indexWriter = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34)));
        for(int i=0;i<id.length;i++){
            Document document = new Document();
            document.add(new Field("id",id[i],Field.Store.YES,Field.Index.ANALYZED));
            document.add(new Field("contents",contents[i],Field.Store.YES,Field.Index.ANALYZED));
            indexWriter.addDocument(document);
        }
        indexWriter.close();
        
        System.out.println("String is :java");
        search(directory,"java");
        
        System.out.println("\nString is :lucene");
        search(directory,"lucene");
        
        System.out.println("\nString is :+java +jbpm");
        search(directory,"+java +jbpm");
        
        System.out.println("\nString is :+java -jbpm");
        search(directory,"+java -jbpm");
        
        System.out.println("\nString is :java jbpm");
        search(directory,"java jbpm");
        
        System.out.println("\nString is :java AND jbpm");
        search(directory,"java AND jbpm");
        
        System.out.println("\nString is :java or jbpm");
        search(directory,"java or jbpm");
    }
    
    public static void search(Directory directory,String str)throws Exception {
        IndexSearcher indexSearcher = new IndexSearcher(directory);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
        QueryParser queryParser = new QueryParser(Version.LUCENE_34,"contents",analyzer);
        Query query = queryParser.parse(str);
        TopDocs topDocs = indexSearcher.search(query, 10);
        ScoreDoc [] scoreDoc = topDocs.scoreDocs;
        for(int i =0;i<scoreDoc.length;i++){
            Document doc = indexSearcher.doc(scoreDoc[i].doc);
            System.out.println(doc.get("id")+" "+doc.get("contents"));
        }
        indexSearcher.close();
    }
}



运行结果为:

String is :java
1 java and lucene is good
2 I had study java and jbpm
3 I want to study java,hadoop and hbase

String is :lucene
1 java and lucene is good

String is :+java +jbpm
2 I had study java and jbpm

String is :+java -jbpm
1 java and lucene is good
3 I want to study java,hadoop and hbase

String is :java jbpm
2 I had study java and jbpm
1 java and lucene is good
3 I want to study java,hadoop and hbase

String is :java and jbpm
2 I had study java and jbpm

String is :java or jbpm
2 I had study java and jbpm
1 java and lucene is good
3 I want to study java,hadoop and hbase




阅读更多
个人分类: lucene
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭