1、跨度查询SpanQuery5个子类
SpanQuery类型描述
SpanTermQuery和其他跨度查询结合使用,单独使用相当于TermQuery
SpanFirstQuery用来匹配域中的第一个部分内的各个spans
SpanNearQuery用来匹配临近的spans
SpanNotQuery用来匹配不重叠的spans
SpanOrQueryspan查询的聚合匹配
2、测试SpanQuery
public class SpanQueryTest extends TestCase{
privateRAMDirectory directory;
privateIndexSearcher searcher;
privateIndexReader reader;
privateSpanTermQuery quick;
privateSpanTermQuery brown;
privateSpanTermQuery red;
privateSpanTermQuery fox;
privateSpanTermQuery lazy;
privateSpanTermQuerySleepy;
privateSpanTermQuery dog;
privateSpanTermQuery cat;
privateAnalyzer analyzer;
protectedvoid setUp() throws Exception{
directory=new RAMDirectory();
analyzer=newWhitespaceAnalyzer();
IndexWriter writer=new IndexWriter(directory,analyzer,true);
Document doc=new Document();
doc.add(Field.Text("f","the quck brown fox jumps over the lazydog"));
writer.addDocument(doc);
doc=new Document();
doc.add(Field.Text("f","the quick red fox jumps over the sleepycat"));
writer.addDocument(doc);
writer.close();
searcher=nweIndexSearcher(directory);
reader=IndexReader.open(directory);
quick=new SpanTermQuery(new Term("f","quick"));
brown=new SpanTermQuery(new Term("f","brown"));
red=new SpanTermQuery(new Term("f","red"));
fox=new SpanTermQuery(new Term("f","fox"));
lazy=new SpanTermQuery(new Term("f","lazy"));
sleepy=new SpanTermQuery(new Term("f","dog"));
dog=newSpanTermQuery(new Term("f","cat"));
}
privatevoid assertOnlyBrownFox(Query query) throwsException{//检查是否返回一个匹配结果
Hits hits=searcher.search(query);
assertEquals(1,hits.length());
assertEquals("wrong doc",0,hits.id(0));
}
privatevoid assertBothFoxes(Query query) throwsException{//检查是否返回2个匹配结果
Hits hits=searcher.search(query);
assertEquals(2.hits.length());
}
privatevoid assertNotMatch(Query query) throws Exception{
Hits hits=searcher.search(query);
assertEquals(0,hits.length());
}
}
3、使用SpanTermQuery类,它匹配文档的方式与TermQuery相似,但是它会记录一个文档中的相同项在文档中出现的不同位置。
SpanQuery对象一直跟踪它所匹配的结果:对于每个正进行匹配的文档,它记录下被匹配的查询文本的所有起点和终点位置(以语汇单元为一个位置)。
dumpSpans(new SpanTermQuery(newTerm("f","the")));//查询文本为the,带the的所有文档,并显示查询细节
定义一个方法dumpSpans()调用SpanQuery的一些底层API为我们查询细节。
public void dumpSpans(SpanQuery query) throws IOException{
Spansspans=query.getSpans(reader);
System.out.println(query+":");
intnumSpans=0;
Hitshits=searcher.search(query);
Float[]scores=new float[2];
for (inti0;i<hits.length();i++){
scores[hits.id(i)]=hits.score(i);
}
while(spans.next()){//spans里是单个查询文本匹配情况情况,记录着query在文档中的位置,
//next表示query在文档中的下一个位置,以及查询文本所在的文档号
numSpans++;
int id=spans.doc();
Document doc=reader.document(id);
Token[]tokens=AnaylzerUtilts.tokensFromAnalysis(analyzer,doc.get("f"));
StringBuffer buffer=new StringBuffer();
buffer.append(" ");
for (int i=0;i<tokens.length;i++){
if (i==spans.start()){
buffer.append("<");
}
buffer.append(tokens[i].termText());
if (i+1==spans.end()){
buffer.append(">");
}
buffer.append(" ");
}
buffer.append("("+scores[id]+")");
System.out.println(buffer);//输出spans的查询文本的在文档中本次出现的信息。
}
if(numspans==0){
System.out.println("no spans");//没有任何匹配
}
System.out.println();
}
第1次spans.next循环所输出:<the> quickbrown fox jumps over the lazy dog(0.18123456)
第2次spans.next循环所输出:the quick brown fox jumps over<the> lazy dog(0.18123456)
第3次spans.next循环所输出:<the> quick redfox jumps over thesleep dog(0.18123456)
第4次spans.next循环所输出:the quick red fox jumps over<the>sleepdog(0.18123456)