1、跨度查询SpanQuery5个子类
SpanQuery类型 描述
SpanTermQuery 和其他跨度查询结合使用,单独使用相当于TermQuery
SpanFirstQuery 用来匹配域中的第一个部分内的各个spans
SpanNearQuery 用来匹配临近的spans
SpanNotQuery 用来匹配不重叠的spans
SpanOrQuery span查询的聚合匹配
2、测试SpanQuery
public class SpanQueryTest extends TestCase{
private RAMDirectory directory;
private IndexSearcher searcher;
private IndexReader reader;
private SpanTermQuery quick;
private SpanTermQuery brown;
private SpanTermQuery red;
private SpanTermQuery fox;
private SpanTermQuery lazy;
private SpanTermQuery Sleepy;
private SpanTermQuery dog;
private SpanTermQuery cat;
private Analyzer analyzer;
protected void setUp() throws Exception{
directory=new RAMDirectory();
analyzer=new WhitespaceAnalyzer();
IndexWriter writer=new IndexWriter(directory,analyzer,true);
Document doc=new Document();
doc.add(Field.Text("f","the quck brown fox jumps over the lazy dog"));
writer.addDocument(doc);
doc=new Document();
doc.add(Field.Text("f","the quick red fox jumps over the sleepy cat"));
writer.addDocument(doc);
writer.close();
searcher=nwe IndexSearcher(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=new SpanTermQuery(new Term("f","cat"));
}
private
void assertOnlyBrownFox(Query query) throws
Exception{//检查是否返回一个匹配结果
Hits hits=searcher.search(query);
assertEquals(1,hits.length());
assertEquals("wrong doc",0,hits.id(0));
}
private
void assertBothFoxes(Query query) throws
Exception{//检查是否返回2个匹配结果
Hits hits=searcher.search(query);
assertEquals(2.hits.length());
}
private
void assertNotMatch(Query query) throws Exception{
Hits hits=searcher.search(query);
assertEquals(0,hits.length());
}
}
3、使用SpanTermQuery类,它匹配文档的方式与TermQuery相似,但是它会记录一个文档中的相同项在文档中出现的不同位置。
SpanQuery对象一直跟踪它所匹配的结果:对于每个正进行匹配的文档,它记录下被匹配的查询文本的所有起点和终点位置(以语汇单元为一个位置)。
dumpSpans(new SpanTermQuery(new
Term("f","the")));//查询文本为the,带the的所有文档,并显示查询细节
定义一个方法dumpSpans()调用SpanQuery的一些底层API为我们查询细节。
public void dumpSpans(SpanQuery query) throws IOException{
Spans
spans=query.getSpans(reader);
System.out.println(query+":");
int
numSpans=0;
Hits
hits=searcher.search(query);
Float[]
scores=new float[2];
for (int
i0;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> quick
brown 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 red
fox jumps over the sleep dog(0.18123456)
第4次spans.next循环所输出:the quick red fox jumps over
<the> sleep
dog(0.18123456)