Java Code Examples for org.apache.lucene.search.spans.SpanFirstQuery

The following examples show how to use org.apache.lucene.search.spans.SpanFirstQuery. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jstarcraft-core   Source File: LuceneQueryTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSpanFirstQuery() throws Exception {
    // 临近查询(匹配域中[0,n]范围内的词项)
    SpanQuery spanQuery = new SpanTermQuery(new Term("title", "Story"));
    SpanFirstQuery firstQuery = new SpanFirstQuery(spanQuery, 5);
    TopDocs search = searcher.search(firstQuery, 1000);
    Assert.assertEquals(5, search.totalHits.value);
}
 
Example 2
Source Project: lucene-solr   Source File: TestUnifiedHighlighterMTQ.java    License: Apache License 2.0 5 votes vote down vote up
public void testSpanPositionCheck() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("This is a test.");
  iw.addDocument(doc);
  body.setStringValue("Test a one sentence document.");
  iw.addDocument(doc);

  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  SpanQuery childQuery = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("body", "te*")));
  Query query = new SpanFirstQuery(childQuery, 1000000);
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  String snippets[] = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a <b>test</b>.", snippets[0]);
  assertEquals("<b>Test</b> a one sentence document.", snippets[1]);

  ir.close();
}
 
Example 3
Source Project: lucene-solr   Source File: ReutersQueryMaker.java    License: Apache License 2.0 5 votes vote down vote up
private static Query[] getPrebuiltQueries(String field) {
  //  be wary of unanalyzed text
  return new Query[] {
      new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 5),
      new SpanNearQuery(new SpanQuery[]{new SpanTermQuery(new Term(field, "night")), new SpanTermQuery(new Term(field, "trading"))}, 4, false),
      new SpanNearQuery(new SpanQuery[]{new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 10), new SpanTermQuery(new Term(field, "credit"))}, 10, false),
      new WildcardQuery(new Term(field, "fo*")),
  };
}
 
Example 4
Source Project: lucene-solr   Source File: EnwikiQueryMaker.java    License: Apache License 2.0 5 votes vote down vote up
private static Query[] getPrebuiltQueries(String field) {
  WildcardQuery wcq = new WildcardQuery(new Term(field, "fo*"));
  wcq .setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
  // be wary of unanalyzed text
  return new Query[] {
      new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 5),
      new SpanNearQuery(new SpanQuery[] {
          new SpanTermQuery(new Term(field, "night")),
          new SpanTermQuery(new Term(field, "trading")) }, 4, false),
      new SpanNearQuery(new SpanQuery[] {
          new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 10),
          new SpanTermQuery(new Term(field, "credit")) }, 10, false), wcq, };
}
 
Example 5
Source Project: lucene-solr   Source File: SpanFirstBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
  int end = DOMUtils.getAttribute(e, "end", 1);
  Element child = DOMUtils.getFirstChildElement(e);
  SpanQuery q = factory.getSpanQuery(child);

  SpanFirstQuery sfq = new SpanFirstQuery(q, end);

  float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
  return new SpanBoostQuery(sfq, boost);
}
 
Example 6
Source Project: lucene-solr   Source File: TestSpanExtractors.java    License: Apache License 2.0 4 votes vote down vote up
public void testSpanPositionQuery() {
  Term t1 = new Term("field", "term");
  Query q = new SpanFirstQuery(new SpanTermQuery(t1), 10);
  assertEquals(Collections.singleton(t1), collectTerms(q));
}