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搜索分页过程,可以有两种方式 一种是将搜索结果集直接放到session,但是假如结果集非常大,同时又存在大并发访问的时候,很可能造成服务器的内存不足,而使服务器宕机 还有一种是每次都重新进行搜索,这样虽然避免了内存溢出的可能,但是,每次搜索都要进行一次IO操作,如果大并发访问的时候,你要保证你的硬盘的转速足够的快,还要保证你的cpu有足够高的频率 而我们可以将这两种方式结合下,每次查询都多缓存一部分的结果集,翻页的时候看看所查询的内容是不是在已经存在在缓存当,如果已经存在了就直接拿出来,如果不存在,就进行查询后,从缓存读出来. 比如:现在我们有一个搜索结果集 一个有100条数据,每页显示10条,就有10页数据. 安装第一种的思路就是,我直接把这100条数据缓存起来,每次翻页时从缓存种读取 而第二种思路就是,我直接从搜索到的结果集种显示前十条给第一页显示,第二页的时候,我在查询一次,给出10-20条数据给第二页显示,我每次翻页都要重新查询 第三种思路就变成了 我第一页仅需要10条数据,但是我一次读出来50条数据,把这50条数据放入到缓存当,当我需要10--20之间的数据的时候,我的发现我的这些数据已经在我的缓存种存在了,我就直接存缓存把数据读出来,少了一次查询,速度自然也提高了很多. 如果我访问第六页的数据,我就把我的缓存更新一次.这样连续翻页10次才进行两次IO操作 同时又保证了内存不容易被溢出.而具体缓存设置多少,要看你的服务器的能力和访问的人数来决定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值