org.apache.lucene.search.spans.SpanNotQuery Java Examples

The following examples show how to use org.apache.lucene.search.spans.SpanNotQuery. 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 File: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testNotSpanSimpleQuery() throws Exception {
  doSearching(new SpanNotQuery(new SpanNearQuery(new SpanQuery[] {
      new SpanTermQuery(new Term(FIELD_NAME, "shot")),
      new SpanTermQuery(new Term(FIELD_NAME, "kennedy")) }, 3, false), new SpanTermQuery(
      new Term(FIELD_NAME, "john"))));
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      mode = QUERY;
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
    }
  };

  helper.run();
  assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
      numHighlights == 4);
}
 
Example #2
Source File: TestPayloadSpans.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSpanNot() throws Exception {
  SpanQuery[] clauses = new SpanQuery[2];
  clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three"));
  SpanQuery spq = new SpanNearQuery(clauses, 5, true);
  SpanNotQuery snq = new SpanNotQuery(spq, new SpanTermQuery(new Term(PayloadHelper.FIELD, "two")));



  Directory directory = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
                                                   newIndexWriterConfig(new PayloadAnalyzer()).setSimilarity(similarity));

  Document doc = new Document();
  doc.add(newTextField(PayloadHelper.FIELD, "one two three one four three", Field.Store.YES));
  writer.addDocument(doc);
  IndexReader reader = getOnlyLeafReader(writer.getReader());
  writer.close();

  checkSpans(snq.createWeight(newSearcher(reader, false), ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(reader.leaves().get(0), SpanWeight.Postings.PAYLOADS), 1, new int[]{2});
  reader.close();
  directory.close();
}
 
Example #3
Source File: SpanNotBuilder.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
  Element includeElem = DOMUtils.getChildByTagOrFail(e, "Include");
  includeElem = DOMUtils.getFirstChildOrFail(includeElem);

  Element excludeElem = DOMUtils.getChildByTagOrFail(e, "Exclude");
  excludeElem = DOMUtils.getFirstChildOrFail(excludeElem);

  SpanQuery include = factory.getSpanQuery(includeElem);
  SpanQuery exclude = factory.getSpanQuery(excludeElem);

  SpanNotQuery snq = new SpanNotQuery(include, exclude);

  float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
  return new SpanBoostQuery(snq, boost);
}
 
Example #4
Source File: MtasSpanNotQuery.java    From mtas with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates a new mtas span not query.
 *
 * @param q1 the q 1
 * @param q2 the q 2
 */
public MtasSpanNotQuery(MtasSpanQuery q1, MtasSpanQuery q2) {
  super(q1 != null ? q1.getMinimumWidth() : null,
      q2 != null ? q2.getMaximumWidth() : null);
  if (q1 != null && (field = q1.getField()) != null) {
    if (q2 != null && q2.getField() != null && !q2.getField().equals(field)) {
      throw new IllegalArgumentException("Clauses must have same field.");
    }
  } else if (q2 != null) {
    field = q2.getField();
  } else {
    field = null;
  }
  this.q1 = q1;
  this.q2 = q2;
  baseQuery = new SpanNotQuery(q1, q2);
}
 
Example #5
Source File: TestUnifiedHighlighterMTQ.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSpanNot() 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 include = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("body", "te*")));
  SpanQuery exclude = new SpanTermQuery(new Term("body", "bogus"));
  Query query = new SpanNotQuery(include, exclude);
  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 #6
Source File: MtasSpanNotQuery.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
  MtasSpanQuery newQ1 = (MtasSpanQuery) q1.rewrite(reader);
  MtasSpanQuery newQ2 = (MtasSpanQuery) q2.rewrite(reader);
  if (!newQ1.equals(q1) || !newQ2.equals(q2)) {
    return new MtasSpanNotQuery(newQ1, newQ2).rewrite(reader);
  } else {
    baseQuery = (SpanNotQuery) baseQuery.rewrite(reader);
    return super.rewrite(reader);
  }
}