Chinaunix首页 | 论坛 | 博客
  • 博客访问: 821291
  • 博文数量: 754
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-12 12:54
文章分类

全部博文(754)

文章存档

2011年(1)

2008年(753)

我的朋友
最近访客

分类:

2008-09-12 13:07:42

    使用MultiFieldQueryParser类即可。

示例代码:

view plaincopy to clipboardprint?
package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {

    public static void main(String[] args) throws Exception {
        File indexDir = new File("C:\\target\\index\\book");
        if (!indexDir.exists() || !indexDir.isDirectory()) {
            throw new IOException();
        }
        search(indexDir);
    }

    public static void search(File indexDir) throws Exception {

        Directory fsDir = FSDirectory.getDirectory(indexDir);
        IndexSearcher searcher = new IndexSearcher(fsDir);

        String[] queries = { "中文版", "8*" };
        String[] fields = { "name", "isbn" };
        BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
        Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());

        Hits hits = searcher.search(query);
        System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
        for (int i = 0; i < hits.length(); i++) {
            int DocId = hits.id(i);
            String DocName  = hits.doc(i).get("name");
            String DocIsbn  = hits.doc(i).get("isbn");
            String DocPblDt = hits.doc(i).get("pbl_dt");
            System.out.println(DocId + ":" + DocName + "  ISBN:" + DocIsbn + "  PBLDT:" + DocPblDt);
        }
    }
}

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {

    public static void main(String[] args) throws Exception {
        File indexDir = new File("C:\\target\\index\\book");
        if (!indexDir.exists() || !indexDir.isDirectory()) {
            throw new IOException();
        }
        search(indexDir);
    }

    public static void search(File indexDir) throws Exception {

        Directory fsDir = FSDirectory.getDirectory(indexDir);
        IndexSearcher searcher = new IndexSearcher(fsDir);

  String[] queries = { "中文版", "8*" };
  String[] fields = { "name", "isbn" };
  BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
        Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());

        Hits hits = searcher.search(query);
        System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
        for (int i = 0; i < hits.length(); i++) {
            int DocId = hits.id(i);
            String DocName  = hits.doc(i).get("name");
            String DocIsbn  = hits.doc(i).get("isbn");
            String DocPblDt = hits.doc(i).get("pbl_dt");
            System.out.println(DocId + ":" + DocName + "  ISBN:" + DocIsbn + "  PBLDT:" + DocPblDt);
        }
    }
}注意:BooleanClause.Occur[]数组,它表示多个条件之间的关系:

BooleanClause.Occur.MUST表示and,

BooleanClause.Occur.MUST_NOT表示not,

BooleanClause.Occur.SHOULD表示or.

 

【责编:landy】

--------------------next---------------------

阅读(303) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~