Lucene-2.2.0 源代码阅读学习(37)

xiaoxiao2026-09-20  7

关于MultiTermQuery查询。

这里研究继承自MultiTermQuery的WildcardQuery查询。

WildcardQuery查询,就是使用通配符进行查询,通配符可以使用“*”和“?”这两种:“*”可以代表0~N个字符串,“?”只能代表一个字符串,而且它们可以在一个词条Term的任何位置出现,从WildcardQuery的构造方法中可以看出:

public WildcardQuery(Term term) {    super(term);    this.termContainsWildcard = (term.text().indexOf('*') != -1) || (term.text().indexOf('?') != -1);}

使用通配符,是在构造完词条以后进行通配,然后根据使用通配符构造的词条,再构造一个WildcardQuery实例,接着就可以用这个WildcardQuery实例进行检索了。

WildcardQuery的使用非常简单,测试也非常容易。

1、使用“*”通配符

package org.apache.lucene.shirdrn.main;

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

import net.teamhot.lucene.ThesaurusAnalyzer;

import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.WildcardQuery;import org.apache.lucene.store.LockObtainFailedException;

public class WildcardQuerySearcher {private String path = "E:\\Lucene\\index";private WildcardQuery wildcardQuery;public void createIndex(){   IndexWriter writer;   try {    writer = new IndexWriter(path,new ThesaurusAnalyzer(),true);       Field fieldA = new Field("contents","文人",Field.Store.YES,Field.Index.TOKENIZED);     Document docA = new Document();     docA.add(fieldA);       Field fieldB = new Field("contents","文修武偃",Field.Store.YES,Field.Index.TOKENIZED);    Document docB = new Document();     docB.add(fieldB);       Field fieldC = new Field("contents","文东武西",Field.Store.YES,Field.Index.TOKENIZED);    Document docC = new Document();     docC.add(fieldC);       Field fieldD = new Field("contents","不使用武力",Field.Store.YES,Field.Index.TOKENIZED);     Document docD = new Document();     docD.add(fieldD);       Field fieldE = new Field("contents","不文不武",Field.Store.YES,Field.Index.TOKENIZED);    Document docE = new Document();     docE.add(fieldE);

    writer.addDocument(docA);    writer.addDocument(docB);    writer.addDocument(docC);    writer.addDocument(docD);    writer.addDocument(docE);    writer.close();   } catch (CorruptIndexException e) {    e.printStackTrace();   } catch (LockObtainFailedException e) {    e.printStackTrace();   } catch (IOException e) {    e.printStackTrace();   }}public void useStarMatchExample(){    // 使用“*”通配符   Term term = new Term("contents","文*");   wildcardQuery = new WildcardQuery(term);}

public void useCompositeMatchExample(){   Term term = new Term("contents","?*武*");    // 使用“*”和“?”组合的通配符   wildcardQuery = new WildcardQuery(term);}

public static void main(String[] args) {   WildcardQuerySearcher wqs = new WildcardQuerySearcher();   wqs.createIndex();wqs.useStarMatchExample();    // 调用使用“*”通配符设置的方法   try {    Date startTime = new Date();    IndexSearcher searcher = new IndexSearcher(wqs.path);    Hits hits = searcher.search(wqs.wildcardQuery);    System.out.println("********************************************************************");    for(int i=0;i<hits.length();i++){     System.out.println("Document的内部编号为 : "+hits.id(i));     System.out.println("Document内容为 : "+hits.doc(i));     System.out.println("Document的得分为 : "+hits.score(i));    }    System.out.println("********************************************************************");    System.out.println("共检索出符合条件的Document "+hits.length()+" 个。");    Date finishTime = new Date();    long timeOfSearch = finishTime.getTime() - startTime.getTime();    System.out.println("本次搜索所用的时间为 "+timeOfSearch+" ms");   } catch (CorruptIndexException e) {    e.printStackTrace();   } catch (IOException e) {    e.printStackTrace();   }}}

构造 WildcardQuery是在useStarMatchExample()方法中:

public void useStarMatchExample(){    // 使用“*”通配符   Term term = new Term("contents","文*");   wildcardQuery = new WildcardQuery(term);}

检索结果自己也能猜想到,如下所示:

********************************************************************Document的内部编号为 : 0Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:人>>Document的得分为 : 1.0Document的内部编号为 : 1Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:修武偃>>Document的得分为 : 1.0Document的内部编号为 : 2Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:东武西>>Document的得分为 : 1.0********************************************************************共检索出符合条件的Document 3 个。本次搜索所用的时间为 313 ms

2、使用“*”和“?”组合通配符

在方法useCompositeMatchExample()中进行构造:

public void useCompositeMatchExample(){   Term term = new Term("contents","?*武*");    // 使用“*”和“?”组合的通配符   wildcardQuery = new WildcardQuery(term);}

使用“*”和“?”组合的通配符进行构造Term,只要将上面测试函数中的wqs.useStarMatchExample();替换成为:

wqs.useCompositeMatchExample();

执行检索,结果可想而知:

********************************************************************Document的内部编号为 : 1Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:文修武偃>>Document的得分为 : 0.9581454Document的内部编号为 : 2Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:文东武西>>Document的得分为 : 0.9581454Document的内部编号为 : 3Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:不使用武力>>Document的得分为 : 0.9581454Document的内部编号为 : 4Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:不文不武>>Document的得分为 : 0.9581454********************************************************************共检索出符合条件的Document 4 个。本次搜索所用的时间为 281 ms

简单总结

在以下的7篇文章中:

Lucene-2.2.0 源代码阅读学习(30)、Lucene-2.2.0 源代码阅读学习(31)、Lucene-2.2.0 源代码阅读学习(32)、Lucene-2.2.0 源代码阅读学习(33)、Lucene-2.2.0 源代码阅读学习(34)、Lucene-2.2.0 源代码阅读学习(35)、Lucene-2.2.0 源代码阅读学习(36)

以及在本文,学习了Query的一些重要的基础的实现查询的工具类,在熟练运用的基础上,综合各种查询,一定能够构造出一种非常复杂的查询,来满足实际的需求。

单独的一种Query是不可能满足用户的要求的。

转载请注明原文地址: https://www.6miu.com/read-5052820.html

最新回复(0)