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




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值