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

The following examples show how to use org.apache.lucene.search.spans.SpanNearQuery. 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: TestUnifiedHighlighterStrictPhrases.java    From lucene-solr with Apache License 2.0 7 votes vote down vote up
public void testFilteredOutSpan() throws IOException {
  indexWriter.addDocument(newDoc("freezing cold stuff like stuff freedom of speech"));
  initReaderSearcherHighlighter();

  WildcardQuery wildcardQuery = new WildcardQuery(new Term("body", "free*"));
  SpanMultiTermQueryWrapper<WildcardQuery> wildcardSpanQuery = new SpanMultiTermQueryWrapper<>(wildcardQuery);
  SpanTermQuery termQuery = new SpanTermQuery(new Term("body", "speech"));
  SpanQuery spanQuery = new SpanNearQuery(new SpanQuery[]{wildcardSpanQuery, termQuery}, 3, false);

  BooleanQuery query = new BooleanQuery.Builder()
      .add(spanQuery, BooleanClause.Occur.MUST)
      .build();

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String[] snippets = highlighter.highlight("body", query, topDocs);
  // spans' MatchesIterator exposes each underlying term; thus doesn't enclose intermediate "of"
  assertArrayEquals(new String[]{"freezing cold stuff like stuff <b>freedom</b> of <b>speech</b>"}, snippets);
}
 
Example #2
Source File: TestPayloadScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testNearQuery() throws IOException {

  //   2     4
  // twenty two
  //  2     4      4     4
  // one hundred twenty two

  SpanNearQuery q = new SpanNearQuery(new SpanQuery[]{
                      new SpanTermQuery(new Term("field", "twenty")),
                      new SpanTermQuery(new Term("field", "two"))
                    }, 0, true);

  checkQuery(q, new MaxPayloadFunction(), new int[]{ 22, 122, 222 }, new float[]{ 4.0f, 4.0f, 4.0f });
  checkQuery(q, new MinPayloadFunction(), new int[]{ 122, 222, 22 }, new float[]{ 4.0f, 4.0f, 2.0f });
  checkQuery(q, new AveragePayloadFunction(), new int[] { 122, 222, 22 }, new float[] { 4.0f, 4.0f, 3.0f });

}
 
Example #3
Source File: QueryTransformer.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Query handle(Query query, QueryTransformer queryTransformer) {
    PhraseQuery phraseQuery = (PhraseQuery) query;
    SpanNearQuery.Builder builder = new SpanNearQuery.Builder(phraseQuery.getTerms()[0].field(), true);
    int i = 0;
    int position = -1;
    for (Term term : phraseQuery.getTerms()) {
        if (i > 0) {
            int gap = (phraseQuery.getPositions()[i] - position) - 1;
            if (gap > 0) {
                builder.addGap(gap);
            }
        }
        position = phraseQuery.getPositions()[i];
        builder.addClause(new CustomSpanPayloadCheckQuery(new SpanTermQuery(term), Collections.singletonList(null)));
        i++;
    }
    return builder.setSlop(phraseQuery.getSlop()).build();
}
 
Example #4
Source File: AbstractEntitySearcher.java    From webdsl with Apache License 2.0 6 votes vote down vote up
private Query createRegexQuery ( QueryDef qd ) {
    BooleanQuery query = new BooleanQuery();
    List<SpanQuery> spanClausesList = new ArrayList<SpanQuery>();
    String[] queryStrings;
    SpanQuery[] spanClausesArray;
    RegexQuery regexQuery;
    for ( String fld : qd.fields ) {
        spanClausesList.clear();
        queryStrings = qd.query.split(" ");
        spanClausesArray = new SpanQuery[queryStrings.length];
        for ( String subquery : queryStrings ) {
            regexQuery = new RegexQuery( new Term( fld, subquery ) );
            regexQuery.setRegexImplementation( new JavaUtilRegexCapabilities() );
            //if emptyable, like a query '(optional)?' or 'bla|a*', make span optional by wrapping it SpanOrQuery
            if(Pattern.matches(subquery, "")){
                spanClausesList.add( new SpanOrQuery( new SpanMultiTermQueryWrapper<RegexQuery>( regexQuery ) ) );
            } else {
                spanClausesList.add( new SpanMultiTermQueryWrapper<RegexQuery>( regexQuery ) );
            }
        }

        spanClausesList.toArray( spanClausesArray );
        query.add( new SpanNearQuery( spanClausesArray, 0, true), Occur.SHOULD );
    }
    return query;
}
 
Example #5
Source File: DistanceQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public Query getSpanNearQuery(
        IndexReader reader,
        String fieldName,
        BasicQueryFactory qf) throws IOException {
  SpanQuery[] spanClauses = new SpanQuery[getNrSubQueries()];
  Iterator<?> sqi = getSubQueriesIterator();
  int qi = 0;
  while (sqi.hasNext()) {
    SpanNearClauseFactory sncf = new SpanNearClauseFactory(reader, fieldName, qf);
    
    ((DistanceSubQuery)sqi.next()).addSpanQueries(sncf);
    if (sncf.size() == 0) { /* distance operator requires all sub queries */
      while (sqi.hasNext()) { /* produce evt. error messages but ignore results */
        ((DistanceSubQuery)sqi.next()).addSpanQueries(sncf);
        sncf.clear();
      }
      return new MatchNoDocsQuery();
    }
    
    spanClauses[qi] = sncf.makeSpanClause();
    qi++;
  }

  return new SpanNearQuery(spanClauses, getOpDistance() - 1, subQueriesOrdered());
}
 
Example #6
Source File: SpanNearBuilder.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
  String slopString = DOMUtils.getAttributeOrFail(e, "slop");
  int slop = Integer.parseInt(slopString);
  boolean inOrder = DOMUtils.getAttribute(e, "inOrder", false);
  List<SpanQuery> spans = new ArrayList<>();
  for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
    if (kid.getNodeType() == Node.ELEMENT_NODE) {
      spans.add(factory.getSpanQuery((Element) kid));
    }
  }
  SpanQuery[] spanQueries = spans.toArray(new SpanQuery[spans.size()]);
  SpanQuery snq = new SpanNearQuery(spanQueries, slop, inOrder);
  float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
  return new SpanBoostQuery(snq, boost);
}
 
Example #7
Source File: TestHighlightingMatcher.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testWildcardProximityRewrites() throws Exception {
  final SpanNearQuery snq = SpanNearQuery.newOrderedNearQuery(FIELD)
      .addClause(new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term(FIELD, "term*"))))
      .addClause(new SpanTermQuery(new Term(FIELD, "foo")))
      .build();

  try (Monitor monitor = newMonitor()) {

    monitor.register(new MonitorQuery("1", snq));

    MatchingQueries<HighlightsMatch> matches = monitor.match(buildDoc("term1 foo"), HighlightsMatch.MATCHER);
    HighlightsMatch m = matches.matches("1");
    assertNotNull(m);
    assertEquals(2, m.getHitCount());
  }
}
 
Example #8
Source File: TestPayloadCheckQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testRewrite() throws IOException {
  SpanMultiTermQueryWrapper<WildcardQuery> fiv = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "fiv*")));
  SpanMultiTermQueryWrapper<WildcardQuery> hund = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "hund*")));
  SpanMultiTermQueryWrapper<WildcardQuery> twent = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "twent*")));
  SpanMultiTermQueryWrapper<WildcardQuery> nin = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("field", "nin*")));

  SpanNearQuery sq = new SpanNearQuery(new SpanQuery[] {fiv, hund, twent, nin}, 0, true);

  List<BytesRef> payloads = new ArrayList<>();
  payloads.add(new BytesRef("pos: 0"));
  payloads.add(new BytesRef("pos: 1"));
  payloads.add(new BytesRef("pos: 2"));
  payloads.add(new BytesRef("pos: 3"));

  SpanPayloadCheckQuery query = new SpanPayloadCheckQuery(sq, payloads);

  // if query wasn't rewritten properly, the query would have failed with "Rewrite first!"
  checkHits(query, new int[]{529});
}
 
Example #9
Source File: TestPayloadCheckQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testUnorderedPayloadChecks() throws Exception {

    SpanTermQuery term5 = new SpanTermQuery(new Term("field", "five"));
    SpanTermQuery term100 = new SpanTermQuery(new Term("field", "hundred"));
    SpanTermQuery term4 = new SpanTermQuery(new Term("field", "four"));
    SpanNearQuery nearQuery = new SpanNearQuery(new SpanQuery[]{term5, term100, term4}, 0, false);

    List<BytesRef> payloads = new ArrayList<>();
    payloads.add(new BytesRef("pos: " + 2));
    payloads.add(new BytesRef("pos: " + 1));
    payloads.add(new BytesRef("pos: " + 0));

    SpanPayloadCheckQuery payloadQuery = new SpanPayloadCheckQuery(nearQuery, payloads);
    checkHits(payloadQuery, new int[]{ 405 });

    payloads.clear();
    payloads.add(new BytesRef("pos: " + 0));
    payloads.add(new BytesRef("pos: " + 1));
    payloads.add(new BytesRef("pos: " + 2));

    payloadQuery = new SpanPayloadCheckQuery(nearQuery, payloads);
    checkHits(payloadQuery, new int[]{ 504 });

  }
 
Example #10
Source File: TestPayloadScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpanContainingQuery() throws Exception {

  // twenty WITHIN ((one OR hundred) NEAR two)~2
  SpanContainingQuery q = new SpanContainingQuery(
      new SpanNearQuery(new SpanQuery[]{
          new SpanOrQuery(new SpanTermQuery(new Term("field", "one")), new SpanTermQuery(new Term("field", "hundred"))),
          new SpanTermQuery(new Term("field", "two"))
      }, 2, true),
      new SpanTermQuery(new Term("field", "twenty"))
  );

  checkQuery(q, new AveragePayloadFunction(), new int[] { 222, 122 }, new float[]{ 4.0f, 3.666666f });
  checkQuery(q, new MaxPayloadFunction(), new int[]{ 122, 222 }, new float[]{ 4.0f, 4.0f });
  checkQuery(q, new MinPayloadFunction(), new int[]{ 222, 122 }, new float[]{ 4.0f, 2.0f });

}
 
Example #11
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 #12
Source File: TestUnifiedHighlighterStrictPhrases.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testReverseOrderSpanCollection() throws IOException {
  // Processing order may depend on various optimizations or other weird factor.
  indexWriter.addDocument(newDoc("alpha bravo - alpha charlie"));
  indexWriter.addDocument(newDoc("alpha charlie - alpha bravo"));
  initReaderSearcherHighlighter();

  SpanNearQuery query = new SpanNearQuery(new SpanQuery[]{
      new SpanNearQuery(new SpanQuery[]{
          new SpanTermQuery(new Term("body", "alpha")),
          new SpanTermQuery(new Term("body", "bravo"))
      }, 0, true),
      new SpanNearQuery(new SpanQuery[]{
          new SpanTermQuery(new Term("body", "alpha")),
          new SpanTermQuery(new Term("body", "charlie"))
      }, 0, true)
  }, 10, false);

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String[] snippets = highlighter.highlight("body", query, topDocs);

  assertArrayEquals(new String[]{
          "<b>alpha</b> <b>bravo</b> - <b>alpha</b> <b>charlie</b>",
          "<b>alpha</b> <b>charlie</b> - <b>alpha</b> <b>bravo</b>",
      },
      snippets);
}
 
Example #13
Source File: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testNearSpanSimpleQuery() throws Exception {
  doSearching(new SpanNearQuery(new SpanQuery[] {
      new SpanTermQuery(new Term(FIELD_NAME, "beginning")),
      new SpanTermQuery(new Term(FIELD_NAME, "kennedy")) }, 3, false));

  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 == 2);
}
 
Example #14
Source File: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testGetBestFragmentsFilteredQuery() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      SpanQuery clauses[] = { new SpanTermQuery(new Term("contents", "john")),
          new SpanTermQuery(new Term("contents", "kennedy")), };
      SpanNearQuery snq = new SpanNearQuery(clauses, 1, true);
      BooleanQuery.Builder bq = new BooleanQuery.Builder();
      bq.add(snq, Occur.MUST);
      bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER);

      doSearching(bq.build());
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      // Currently highlights "John" and "Kennedy" separately
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 2);
    }
  };

  helper.start();
}
 
Example #15
Source File: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testGetBestFragmentsQueryScorer() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      SpanQuery clauses[] = { new SpanTermQuery(new Term("contents", "john")),
          new SpanTermQuery(new Term("contents", "kennedy")), };

      SpanNearQuery snq = new SpanNearQuery(clauses, 1, true);
      doSearching(snq);
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      // Currently highlights "John" and "Kennedy" separately
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 2);
    }
  };

  helper.start();
}
 
Example #16
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 #17
Source File: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSpanHighlighting() throws Exception {
  Query query1 = new SpanNearQuery(new SpanQuery[] {
      new SpanTermQuery(new Term(FIELD_NAME, "wordx")),
      new SpanTermQuery(new Term(FIELD_NAME, "wordy")) }, 1, false);
  Query query2 = new SpanNearQuery(new SpanQuery[] {
      new SpanTermQuery(new Term(FIELD_NAME, "wordy")),
      new SpanTermQuery(new Term(FIELD_NAME, "wordc")) }, 1, false);
  BooleanQuery.Builder bquery = new BooleanQuery.Builder();
  bquery.add(query1, Occur.SHOULD);
  bquery.add(query2, Occur.SHOULD);
  doSearching(bquery.build());
  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 == 7);
}
 
Example #18
Source File: TestUnifiedHighlighterStrictPhrases.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Test it does *not* highlight the same term's not next to the span-near.  "charlie" in this case.
 * This particular example exercises "Rewrite" plus "MTQ" in the same query.
 */
public void testRewriteAndMtq() throws IOException {
  indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
  initReaderSearcherHighlighter();

  SpanNearQuery snq = new SpanNearQuery(
      new SpanQuery[]{
          new SpanTermQuery(new Term("body", "bravo")),
          new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch")))}, // REWRITES
      0, true);

  BooleanQuery query = new BooleanQuery.Builder()
      .add(snq, BooleanClause.Occur.MUST)
      .add(new PrefixQuery(new Term("body", "al")), BooleanClause.Occur.MUST) // MTQ
      .add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST)
      // add queries for other fields; we shouldn't highlight these because of that.
      .add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD)
      .build();

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String[] snippets = highlighter.highlight("body", query, topDocs);

  if (highlighter.getFlags("body").contains(HighlightFlag.WEIGHT_MATCHES)) {
    assertArrayEquals(new String[]{"<b>alpha bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  } else {
    assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  }

  // do again, this time with MTQ disabled.  We should only find "alpha bravo".
  highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setHandleMultiTermQuery(false);//disable but leave phrase processing enabled

  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  snippets = highlighter.highlight("body", query, topDocs);

  assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha"},
      snippets);
}
 
Example #19
Source File: TestSolrCoreParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testHandySpanQuery() throws IOException, ParserException {
  final String lhsXml = "<SpanOr fieldName='contents'>"
      + "<SpanTerm>rain</SpanTerm>"
      + "<SpanTerm>spain</SpanTerm>"
      + "<SpanTerm>plain</SpanTerm>"
      + "</SpanOr>";
  final String rhsXml = "<SpanNear fieldName='contents' slop='2' inOrder='true'>"
      + "<SpanTerm>sunny</SpanTerm>"
      + "<SpanTerm>sky</SpanTerm>"
      + "</SpanNear>";
  final Query query = parseHandyQuery(lhsXml, rhsXml);
  final BooleanQuery bq = (BooleanQuery)query;
  assertEquals(2, bq.clauses().size());
  for (int ii=0; ii<bq.clauses().size(); ++ii) {
    final Query clauseQuery = bq.clauses().get(ii).getQuery();
    switch (ii) {
      case 0:
        assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanOrQuery);
        break;
      case 1:
        assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanNearQuery);
        break;
      default:
        fail("unexpected clause index "+ii);
    }
  }
}
 
Example #20
Source File: TestSolrCoreParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void checkApacheLuceneSolr(Query query, String fieldName) {
  assertTrue(query instanceof SpanNearQuery);
  final SpanNearQuery snq = (SpanNearQuery)query;
  assertEquals(fieldName, snq.getField());
  assertEquals(42, snq.getSlop());
  assertFalse(snq.isInOrder());
  assertEquals(3, snq.getClauses().length);
  assertTrue(snq.getClauses()[0] instanceof SpanTermQuery);
  assertTrue(snq.getClauses()[1] instanceof SpanTermQuery);
  assertTrue(snq.getClauses()[2] instanceof SpanTermQuery);
}
 
Example #21
Source File: ApacheLuceneSolrNearQueryBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SpanQuery getSpanQuery(Element e) throws ParserException {
  final String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
  final SpanQuery[] spanQueries = new SpanQuery[]{
      new SpanTermQuery(new Term(fieldName, "Apache")),
      new SpanTermQuery(new Term(fieldName, "Lucene")),
      new SpanTermQuery(new Term(fieldName, "Solr"))
  };
  final int slop = 42;
  final boolean inOrder = false;
  return new SpanNearQuery(spanQueries, slop, inOrder);
}
 
Example #22
Source File: TestMatchesIterator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSpanQuery() throws IOException {
  SpanQuery subq = SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)
      .addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "with")))
      .addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "many")))
      .build();
  Query q = SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)
      .addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "sentence")))
      .addClause(new SpanOrQuery(subq, new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "iterations"))))
      .build();
  checkMatches(q, FIELD_WITH_OFFSETS, new int[][]{
      { 0 }, { 1 }, { 2 }, { 3 },
      { 4, 2, 4, 9, 27, 6, 7, 35, 54 }
  });
  checkLabelCount(q, FIELD_WITH_OFFSETS, new int[]{ 0, 0, 0, 0, 1 });
  checkTermMatches(q, FIELD_WITH_OFFSETS, new TermMatch[][][]{
      {}, {}, {}, {},
      {
          {
              new TermMatch(2, 9, 17),
              new TermMatch(3, 18, 22),
              new TermMatch(4, 23, 27)
          }, {
            new TermMatch(6, 35, 43), new TermMatch(7, 44, 54)
      }
      }
  });
}
 
Example #23
Source File: TestHighlightingMatcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisjunctionWithOrderedNearMatch() throws Exception {

    final Query bq = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "a")), BooleanClause.Occur.SHOULD)
        .add(SpanNearQuery.newOrderedNearQuery(FIELD)
            .addClause(new SpanTermQuery(new Term(FIELD, "b")))
            .addClause(new SpanTermQuery(new Term(FIELD, "c")))
            .setSlop(1)
            .build(), BooleanClause.Occur.SHOULD)
        .build();
    final Query parent = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "a")), BooleanClause.Occur.MUST)
        .add(bq, BooleanClause.Occur.MUST)
        .build();

    try (Monitor monitor = newMonitor()) {
      monitor.register(new MonitorQuery("1", parent));

      Document doc = buildDoc("a b c");
      MatchingQueries<HighlightsMatch> matches = monitor.match(doc, HighlightsMatch.MATCHER);

      HighlightsMatch m = matches.matches("1");
      assertNotNull(m);
      assertEquals(3, m.getHitCount());
      assertTrue(m.getFields().contains(FIELD));
      assertTrue(m.getHits(FIELD).contains(new HighlightsMatch.Hit(0, 0, 0, 1)));
      assertTrue(m.getHits(FIELD).contains(new HighlightsMatch.Hit(1, 2, 1, 3)));
      assertTrue(m.getHits(FIELD).contains(new HighlightsMatch.Hit(2, 4, 2, 5)));
    }

  }
 
Example #24
Source File: TestHighlightingMatcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisjunctionWithUnorderedNearSpans() throws Exception {

    final Query bq = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "a")), BooleanClause.Occur.SHOULD)
        .add(SpanNearQuery.newUnorderedNearQuery(FIELD)
            .addClause(new SpanTermQuery(new Term(FIELD, "b")))
            .addClause(new SpanTermQuery(new Term(FIELD, "c")))
            .setSlop(1)
            .build(), BooleanClause.Occur.SHOULD)
        .build();
    final Query parent = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "a")), BooleanClause.Occur.MUST)
        .add(bq, BooleanClause.Occur.MUST)
        .build();

    try (Monitor monitor = new Monitor(ANALYZER)) {
      monitor.register(new MonitorQuery("1", parent));

      Document doc = buildDoc("a b x x x x c");
      MatchingQueries<HighlightsMatch> matches = monitor.match(doc, HighlightsMatch.MATCHER);

      HighlightsMatch m = matches.matches("1");
      assertNotNull(m);
      assertEquals(1, m.getHitCount());
    }

  }
 
Example #25
Source File: TestHighlightingMatcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisjunctionWithOrderedNearSpans() throws Exception {

    final Query bq = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "a")), BooleanClause.Occur.SHOULD)
        .add(SpanNearQuery.newOrderedNearQuery(FIELD)
            .addClause(new SpanTermQuery(new Term(FIELD, "b")))
            .addClause(new SpanTermQuery(new Term(FIELD, "c")))
            .setSlop(1)
            .build(), BooleanClause.Occur.SHOULD)
        .build();
    final Query parent = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "a")), BooleanClause.Occur.MUST)
        .add(bq, BooleanClause.Occur.MUST)
        .build();

    try (Monitor monitor = new Monitor(ANALYZER)) {
      monitor.register(new MonitorQuery("1", parent));

      Document doc = buildDoc("a b x x x x c");
      MatchingQueries<HighlightsMatch> matches = monitor.match(doc, HighlightsMatch.MATCHER);

      HighlightsMatch m = matches.matches("1");
      assertNotNull(m);
      assertEquals(1, m.getHitCount());
    }

  }
 
Example #26
Source File: TestPhraseWildcardQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected SpanNearQuery spanNearQuery(String field, int slop, String... terms) {
  SpanQuery[] spanQueries = new SpanQuery[terms.length];
  for (int i = 0; i < terms.length; i++) {
    String term = terms[i];
    spanQueries[i] = term.contains("*") || term.contains("?") ?
        new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term(field, term)))
        : new SpanTermQuery(new Term(field, term));
  }
  return new SpanNearQuery(spanQueries, slop, true);
}
 
Example #27
Source File: TestSpanExtractors.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSpanContains() {
  Term t1 = new Term("field", "term1");
  Term t2 = new Term("field", "term22");
  Term t3 = new Term("field", "term333");
  SpanContainingQuery swq = new SpanContainingQuery(
      SpanNearQuery.newOrderedNearQuery("field")
          .addClause(new SpanTermQuery(t1))
          .addClause(new SpanTermQuery(t2))
          .build(),
      new SpanTermQuery(t3));

  assertEquals(Collections.singleton(t3), collectTerms(swq));
}
 
Example #28
Source File: TestSpanExtractors.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSpanWithin() {
  Term t1 = new Term("field", "term1");
  Term t2 = new Term("field", "term22");
  Term t3 = new Term("field", "term333");
  SpanWithinQuery swq = new SpanWithinQuery(
      SpanNearQuery.newOrderedNearQuery("field")
          .addClause(new SpanTermQuery(t1))
          .addClause(new SpanTermQuery(t2))
          .build(),
      new SpanTermQuery(t3));

  assertEquals(Collections.singleton(t3), collectTerms(swq));
}
 
Example #29
Source File: TestSpanExtractors.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testOrderedNearWithWildcardExtractor() {
  SpanNearQuery q = new SpanNearQuery(new SpanQuery[]{
      new SpanMultiTermQueryWrapper<>(new RegexpQuery(new Term("field", "super.*cali.*"))),
      new SpanTermQuery(new Term("field", "is"))
  }, 0, true);

  Set<Term> expected = Collections.singleton(new Term("field", "is"));
  assertEquals(expected, collectTerms(q));
}
 
Example #30
Source File: TestSpanExtractors.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testOrderedNearExtractor() {
  SpanNearQuery q = new SpanNearQuery(new SpanQuery[]{
      new SpanTermQuery(new Term("field1", "term1")),
      new SpanTermQuery(new Term("field1", "term"))
  }, 0, true);

  Set<Term> expected = Collections.singleton(new Term("field1", "term1"));
  assertEquals(expected, collectTerms(q));
}