lucene 查询 (转载)

原网址:http://hi.baidu.com/lszhuhaichao/blog/item/ccffc7cb858f1514bf09e66f.html

Lucene3.0之查询处理(1):原理2010-03-06 23:37Lucene3.0之查询处理(1):原理

1、 查询的三种方式

①     顺序查询:简单,但查询效率低

②     索引查询:快速,需要基础索引结构支撑

2、 理论模型

①     布尔模型:基于集合论和布尔代数的一种简单检索模型

②     向量模型:查询串和文档之间分配不同的权值,权值大小放映了文档库中的文档与用户查询串的相关度。查询得到的结果文档按照权值计算相关度有关排序,所以向量模型得到的匹配文档可以是全部精确匹配,也可以是部分匹配查询串。

3、 查询流程

用户查询请求输入->查询词频->查询词频出现->查询词格式化->文本库索引匹配->相似度和排序计算->结果排重与生成。

4、 Lucence3.0查询概述

1、 主要利用查询工具IndexSearcher类

这是检索的主要控制和工具,也是所有搜索操作的入口。其构造方法主要有:

IndexSearcher(Directory path)

IndexSearcher(Directory path, boolean readOnly)

IndexSearcher(IndexReader r)

IndexSearcher(IndexReader reader, IndexReader[] subReaders, int[] docStarts)

这里推荐主要使用第1个和第2个构造方法。

2、 其它相关的类

①     Query:抽象类,必须通过一系列子类来表述检索的具体需求。

②     QueryParser:查询分析器。处理用户输入的查询条件。把用户输入的非格式化检索词转化成后台检索可以理解的Query对象

查询最基本的结果返回方式是通过Hits对象来提供。Hits提供了检索查询结果的缓冲,为结果的展示和返回提供支持。Hits中的结果集已经按照相关性进行了排序,前面的文档结果表明与查询词更为相似。

Lucene3.0之查询(2):查询类型1

1、 查询Query对象

Lucnce查询主要有两种方式。一是通过Query子类构造函数方法生成子类。这种方法最大的好处是非常直观,可以根据自己的功能目标挑选合适的子类来够着具体的Query对象。

另一种查询方式是通过QueryParse动态构造查询对象。这种方法使用了parse方法,具体构造的对象类型需要根据查询词的内容来确定。除了少数特殊查询,几乎所有的查询检索都可以通过QueryParser来代替特定子类的构造函数来查询对象生成功能。

2、 最小项查询TermQuery

适合关键字查询文档,大小写敏感。

①     Term term = new Term(“content”, “星期一”);

TermQueryquery = new TermQuery(term);

②     String str = “星期一”;

Analyzer analyzer = new Analyzer();

QueryParser parser = new QueryParser(“content”, analyzer);

Query query = parser.parse(str);

3、 区域范围查询RangeQuery

在年龄、日期、分数、数量等情况下经常会使用到。通常的模式使用起始值和终止值来确定区间。有点类似SQL语句中的between…and…语 句。生成RangeQuery的实例需要两个对应的Term对象分别描述起始点和终止点。另外还要有一个标志参数,用来表明是否包含区间范围的边界。如果 标志参数为true,表明检索查询匹配时需要包含边界,否则为不包含边界。

①     Term termStart = new Term(“weight”, ”40”);

Term termEnd = new Term(“weight”, “50”);

TermRangeQuery query = new TermRangeQuery("numval",lowerTerm,upperTerm,true,true);

②     String str = “{40 TO 50}”;

Analyzer analyzer = new Analyzer();

QueryParser parser = new QueryParser(“content”, analyzer);

Query query = parser.parse(str);

4、 逻辑组合搜索BooleanQuery

①   Term term1 = new Term(“content”, “星期一”);

Term term2 = new Term(“content”, “五月一日”);

TermQuery query1 = new TermQuery(term1);

TermQuery query2 = new TermQuery(term2);

BooleanQuery query = new BooleanQuery();

Query.add(query1.BooleanClause.Occur.MUST);

Query.add(query2.BooleanClause.Occur.MUST);

AND查询:MUST+MUST;NO查询:MUST+MUST_NOT或者SHOULD+MUST_NOT;OR查询:SHOULD+SHOULD;

②     String str = ”(星期一 AND 五月一日)”

Analyzer analyzer = new Analyzer();

QueryParser parser = new QueryParser(“content”, analyzer);

Query query = parser.parse(str);

5、 字串前缀查询RefixQuery

①     使用PrefixQuery构造前缀查询

前缀查询的直接构造方法是使用Term构造一个最小项对象,同时把它作为前缀的生成参数。构造的查询对象提交检索查询,得到的结果以Term项内的文本值为开头字符的所有文章。

Term term = new Term(“content”, “五月一日”);

PrefixQuery query = new PrefixQuery(term);

②     String str = “(五月一日)”;

Analyzer analyzer = new Analyzer();

QueryParser parser = new QueryParser(“content”, analyzer);

Query query = parser.parse(str);

6、 短语搜索PhraseQuery

①     PhraseQuery构造短语查询

Term term1 = new Term(“content”, “星期”);

Term term2 = new Term(“content”, “一”);

PhraseQuery query = new PhraseQuery();

query.add(term1);

query.add(term2);

query.setSlop(1);

PhraseQuery和Boolean的区别:

PhraseQuery对象的查询结果符合关键词的添加次序。BooleanQuery的与检索查询结果范围更大,检索项次序相反的文档也会检索 到。严格的检索词次序匹配会限制使用范围。为了能找到最相近的结果,可以使用setSlop方法,指定小于编辑距离的匹配文档也作为结果出现。

②     QueryParser构造短语查询

用户输入的单个检索项的查询词会通过QueryParser的Parse方法生成TermQuery对象,带空格的多个检索项会生成BooleanQuery对象的与检索。如果要生成PhraseQuery对象,需要给查询间加上双引号。

String str = “”星期一””;

Analyzer analyzer = new Analyzer();

QueryParser parser = new QueryParser(“content”, analyzer);

Query query = parser.parse(str);



Lucene3.0之查询(3):查询类型2

7、 模糊查询FuzzyQuery

这种模糊查询搜索是按照检索文本的形似度进行判断的。两个检索器或者字符串的相似是通过编辑距离来判定的。这种编辑距离实际上是表明两个不同的字 符串需要经过多少次编辑和变换才能变为对方。通常的编辑行为包括了增加一个检索项,删除一个检索项,修改一个检索项,与普通的字符串匹配函数不同,模糊搜 索里的编辑距离是以索引项为单位的。

①     FuzzyQuery()

Term term = new Term(“content”, “星期”);

FuzzyQuery query = new FuzzyQuery(term);

②     QueryParser:

查询词后携带“~0.1f”格式的限定。整个查询词不需要专门使用双引号。

String str = “星期一 ~0.1f”;

8、 通配符查询WildcardQuery

?:1个特定字符;*:0个或者多个待定字符。

①     Term term = new Term(“content”, “星期*”);

WildcardQuery query = new WildcardQuery(term);

②     String str = “0*1”;

9、 位置跨度查询SpanQuery

①     SpanTermQuery

SpanTermQuery携带了位置信息的Term对象查询类,单独使用时输出地结果与TermQuery相同。但是它携带的位置信息可以为其 它复杂的SpanQuery提供支持,是跨度检索的基础类。也可以为后续的自定义排序规则提供位置信息,或者用来特殊显示相关结果。

②     SpanFirstQuery

SpanFirstQuery用来指定查询域中前面指定数量索引项的范围内进行检索,提高查询检索效率。如果匹配的检索项在指定范围之外,查询中不会返回该文档作为结果。

Term t = new Term(“content”, str);

SpanTermQuery query = new SpanTermQuery(t);

SpanFirstQuery firstquery = new SpanFirstQuery(query, 2);

③     SpanNearQuery

SpanNearQuery用来指定不同查询检索项在文本中的间隔距离,如果间隔太久,以致超出了参数指定的距离。即使所有检索引都存在,也不能作为结果输出。

查询过程需要生成多个Term对象,利用每个Term对象分别构造SpanTermQuery对象并形成数组。

Term t1 = new Term(“content”, “星期一”);

Term t2 = new Term(“content”, “星期二”);

Term t3 = new Term(“content”, “星期三”);

SpanTermQuery query1 = new SpanTermQuery(t1);

SpanTermQuery query2 = new SpanTermQuery(t2);

SpanTermQuery query3 = new SpanTermQuery(t3);

SpanQuery[] queryarray = new SpanQuery[]{query1, query2, query3};

SpanNearQuery nearQUery = new SpanNearQuery(queryarray, 1, true);

④     SpanNotQuery

SpanNotQuery用来指定查询中,某两个查询对内容会不会发生重叠,如果特定索引项落入到查询的跨度范围内,就把该文档以结果集中排除

使用SpanNearQuery相同。

⑤     SpanOrQuery

SpanOrQuery用来对SpanOrQuery对象进行封装,用来组合其它SpanQuery对象得到满足任一个跨度的查询结果合并后作为整体输出。

使用SpanNearQuery相同。



Lucene3.0之查询(4):实例

package luceneQuery;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.search.spans.SpanFirstQuery;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanNotQuery;
import org.apache.lucene.search.spans.SpanOrQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

public class QueryTest {

static String sIndex_Path="E:/index";
static String sText_path="E:/textbook";
static protected String[] keywords = {"001","002","003","004","005"};
static protected String[] textdetail = {"记录 一","记录 二","记录 三","一 2345 记录","记录 新 一"};
static File fIndex_Path=new File(sIndex_Path);
/**===========================================================
* 名称:IndexBuilder
* 功能:构造磁盘索引,添加内容到指定目录,为后继检索查询做好准备
=============================================================**/
public static void IndexBuilder(){
   try{
    Date start = new Date();
    File f=new File(sText_path);
    File[] list=f.listFiles();
    File file2 = new File(sIndex_Path);
    //创建磁盘索引目录
    Directory dir = FSDirectory.open(file2);
    Directory ramdir = new RAMDirectory();
    Analyzer TextAnalyzer = new SimpleAnalyzer();
    //创建磁盘索引
    IndexWriter TextIndex = new IndexWriter(dir, TextAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED);
    //创建内存索引
    IndexWriter RAMTextIndex = new IndexWriter(ramdir,TextAnalyzer,true, IndexWriter.MaxFieldLength.LIMITED);
    for(int i=0;i<list.length;i++){
     Document document = new Document();
     Field field_name = new Field("name", list[1].getName(),
       Field.Store.YES, Field.Index.NOT_ANALYZED);
     document.add(field_name);
     FileInputStream inputfile = new FileInputStream(list[i]);
     int len = inputfile.available();
     byte[] buffer = new byte[len];
     inputfile.read(buffer);
     inputfile.close();

     String contenttext = new String(buffer);
     Field field_content = new Field("content", contenttext,
       Field.Store.YES, Field.Index.ANALYZED);
     document.add(field_content);
   
     Field field_size = new Field("size",String.valueOf(len),Field.Store.YES,Field.Index.NOT_ANALYZED);
     document.add(field_size);
     TextIndex.addDocument(document);
     TextIndex.optimize();
    }
      //关闭磁盘索引
      TextIndex.close();
      Date end = new Date();
      long tm_index = end.getTime()-start.getTime();
      System.out.print("Total Time:(ms)");
      System.out.println(tm_index);
   }catch(IOException e){
    e.printStackTrace();
   }
   System.out.println("index Sccess");
}
/**===================================================================
*名称:LuceneTermQuery
*功能:构造检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
===================================================================**/
public static void LuceneTermQuery(String word){
   try{
    Directory Index_Dir=FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term t = new Term("id", "002");
    TermQuery query = new TermQuery(t);
    System.out.print(query.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   }catch(IOException e){
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**===================================================================
*名称:LuceneRangeQuery
*功能:构造范围检索查询器,对指定的索引进行查询,找到指定的文档,并输
===================================================================**/
public static void LuceneRangeQuery(String lowerTerm, String upperTerm){
   try{
    Directory Index_Dir=FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    TermRangeQuery query = new TermRangeQuery("numval",lowerTerm,upperTerm,true,true);
    System.out.print(query.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   }catch(IOException e){
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
*名称:LuceneBooleanQuery
*功能:构造布尔检索查询器,对指定的索引进行查询,找到指定的值,并输出相应的结果
=========================================================================**/
public static void LuceneBooleanQuery(){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term1 = new Term("content","记录");
    Term term2 = new Term("content","二");
    TermQuery query1 = new TermQuery(term1);
    TermQuery query2 = new TermQuery(term2);
    BooleanQuery query = new BooleanQuery();
    query.add(query1,BooleanClause.Occur.MUST);
    query.add(query2,BooleanClause.Occur.MUST);
    System.out.println(query.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LucenePrefixQuery
* 功能:构造前缀检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LucenePrefixQuery(String word){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term = new Term("content",word);
    PrefixQuery query = new PrefixQuery(term);
    System.out.println(query.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LucenePhraseQuery
* 功能:构造短语检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LucenePhraseQuery(String word1, String word2){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term1 = new Term("content",word1);
    Term term2 = new Term("content",word2);
    PhraseQuery query = new PhraseQuery();
    query.add(term1);
    query.add(term2);
    System.out.println(query.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LuceneFuzzyQuery
* 功能:构造模糊检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LuceneFuzzyQuery(String word){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term = new Term("content",word);
    FuzzyQuery query = new FuzzyQuery(term);
    System.out.println(query.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LuceneWildcardQuery
* 功能:构造通配符检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LuceneWildcardQuery(String word){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term = new Term("content",word);
    WildcardQuery query = new WildcardQuery(term);
    System.out.println(query.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LuceneSpanFirstQuery
* 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LuceneSpanFirstQuery(String word){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term = new Term("content",word);
    SpanTermQuery query = new SpanTermQuery(term);
    SpanFirstQuery firstquery = new SpanFirstQuery(query,2);
    System.out.println(firstquery.toString());
    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LuceneSpanNearQuery
* 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LuceneSpanNearQuery(String word1,String word2,String word3){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term1 = new Term("content",word1);
    Term term2 = new Term("content",word2);
    Term term3 = new Term("content",word3);
    SpanTermQuery query1 = new SpanTermQuery(term1);
    SpanTermQuery query2 = new SpanTermQuery(term2);
    SpanTermQuery query3 = new SpanTermQuery(term3);
    SpanQuery[] queryarray = new SpanQuery[]{query1,query2,query3};
    SpanNearQuery nearquery = new SpanNearQuery(queryarray,1,true);
    System.out.println(nearquery.toString());
    ScoreDoc[] hits = searcher.search(nearquery, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LuceneSpanNotQuery
* 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LuceneSpanNotQuery(String word1,String word2,String word3){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term1 = new Term("content",word1);
    Term term2 = new Term("content",word2);
    Term term3 = new Term("content",word3);
    SpanTermQuery query1 = new SpanTermQuery(term1);
    SpanTermQuery query2 = new SpanTermQuery(term2);
    SpanTermQuery query3 = new SpanTermQuery(term3);
    SpanQuery[] queryarray = new SpanQuery[]{query1,query2};
    SpanNearQuery nearquery = new SpanNearQuery(queryarray,1,true);
    SpanNotQuery notquery = new SpanNotQuery(nearquery,query3);
    System.out.println(notquery.toString());
    ScoreDoc[] hits = searcher.search(notquery, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}
/**=========================================================================
* 名称:LuceneSpanOrQuery
* 功能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果
==========================================================================*/
public static void LuceneSpanOrQuery(String word1,String word2,String word3){
   try {
    Directory Index_Dir = FSDirectory.open(fIndex_Path);
    IndexSearcher searcher = new IndexSearcher(Index_Dir);
    Term term1 = new Term("content",word1);
    Term term2 = new Term("content",word2);
    Term term3 = new Term("content",word3);
    SpanTermQuery query1 = new SpanTermQuery(term1);
    SpanTermQuery query2 = new SpanTermQuery(term2);
    SpanTermQuery query3 = new SpanTermQuery(term3);
    SpanQuery[] queryarray1 = new SpanQuery[]{query1,query2};
    SpanQuery[] queryarray2 = new SpanQuery[]{query2,query3};
    SpanNearQuery nearquery1 = new SpanNearQuery(queryarray1,1,true);
    SpanNearQuery nearquery2 = new SpanNearQuery(queryarray2,1,true);
    SpanOrQuery orquery = new SpanOrQuery(new SpanNearQuery[]{nearquery1,nearquery2});
    System.out.println(orquery.toString());
    ScoreDoc[] hits = searcher.search(orquery, null, 1000).scoreDocs;
    System.out.println("Search result:");
    for (int i = 0; i < hits.length; i++) {
     Document hitDoc = searcher.doc(hits[i].doc);
        System.out.println(hitDoc.get("fieldname"));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println("Search Success");
}

}

原文地址:https://www.cnblogs.com/1130136248wlxk/p/5035120.html