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

The following examples show how to use org.apache.lucene.search.spans.SpanQuery. 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: SpanOrTermsBuilder.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
  String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
  String value = DOMUtils.getNonBlankTextOrFail(e);

  List<SpanQuery> clausesList = new ArrayList<>();

  try (TokenStream ts = analyzer.tokenStream(fieldName, value)) {
    TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class);
    ts.reset();
    while (ts.incrementToken()) {
      SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, BytesRef.deepCopyOf(termAtt.getBytesRef())));
      clausesList.add(stq);
    }
    ts.end();
    SpanOrQuery soq = new SpanOrQuery(clausesList.toArray(new SpanQuery[clausesList.size()]));
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    return new SpanBoostQuery(soq, boost);
  }
  catch (IOException ioe) {
    throw new ParserException("IOException parsing value:" + value);
  }
}
 
Example #3
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 #4
Source File: TestPayloadScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static void checkQuery(SpanQuery query, PayloadFunction function, boolean includeSpanScore, int[] expectedDocs, float[] expectedScores) throws IOException {

    assertTrue("Expected docs and scores arrays must be the same length!", expectedDocs.length == expectedScores.length);

    PayloadScoreQuery psq = new PayloadScoreQuery(query, function, PayloadDecoder.FLOAT_DECODER, includeSpanScore);
    TopDocs hits = searcher.search(psq, expectedDocs.length);

    for (int i = 0; i < hits.scoreDocs.length; i++) {
      if (i > expectedDocs.length - 1)
        fail("Unexpected hit in document " + hits.scoreDocs[i].doc);
      if (hits.scoreDocs[i].doc != expectedDocs[i])
        fail("Unexpected hit in document " + hits.scoreDocs[i].doc);
      assertEquals("Bad score in document " + expectedDocs[i], expectedScores[i], hits.scoreDocs[i].score, 0.000001);
    }

    if (hits.scoreDocs.length > expectedDocs.length)
      fail("Unexpected hit in document " + hits.scoreDocs[expectedDocs.length]);

    QueryUtils.check(random(), psq, searcher);
  }
 
Example #5
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 #6
Source File: TestPayloadCheckQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEquality() {
  SpanQuery sq1 = new SpanTermQuery(new Term("field", "one"));
  SpanQuery sq2 = new SpanTermQuery(new Term("field", "two"));
  BytesRef payload1 = new BytesRef("pay1");
  BytesRef payload2 = new BytesRef("pay2");
  SpanQuery query1 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload1));
  SpanQuery query2 = new SpanPayloadCheckQuery(sq2, Collections.singletonList(payload1));
  SpanQuery query3 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload2));
  SpanQuery query4 = new SpanPayloadCheckQuery(sq2, Collections.singletonList(payload2));
  SpanQuery query5 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload1));

  assertEquals(query1, query5);
  assertFalse(query1.equals(query2));
  assertFalse(query1.equals(query3));
  assertFalse(query1.equals(query4));
  assertFalse(query2.equals(query3));
  assertFalse(query2.equals(query4));
  assertFalse(query3.equals(query4));
}
 
Example #7
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 #8
Source File: TestPayloadTermQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void test() throws IOException {
  SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term("field", "seventy")),
          new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  TopDocs hits = searcher.search(query, 100);
  assertTrue("hits is null and it shouldn't be", hits != null);
  assertTrue("hits Size: " + hits.totalHits.value + " is not: " + 100, hits.totalHits.value == 100);

  //they should all have the exact same score, because they all contain seventy once, and we set
  //all the other similarity factors to be 1

  for (int i = 0; i < hits.scoreDocs.length; i++) {
    ScoreDoc doc = hits.scoreDocs[i];
    assertTrue(doc.score + " does not equal: " + 1, doc.score == 1);
  }
  CheckHits.checkExplanations(query, PayloadHelper.FIELD, searcher, true);
  Spans spans = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertTrue("spans is null and it shouldn't be", spans != null);
  /*float score = hits.score(0);
  for (int i =1; i < hits.length(); i++)
  {
    assertTrue("scores are not equal and they should be", score == hits.score(i));
  }*/

}
 
Example #9
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 #10
Source File: TestPayloadScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testEquality() {
  SpanQuery sq1 = new SpanTermQuery(new Term("field", "one"));
  SpanQuery sq2 = new SpanTermQuery(new Term("field", "two"));
  PayloadFunction minFunc = new MinPayloadFunction();
  PayloadFunction maxFunc = new MaxPayloadFunction();
  PayloadScoreQuery query1 = new PayloadScoreQuery(sq1, minFunc, PayloadDecoder.FLOAT_DECODER, true);
  PayloadScoreQuery query2 = new PayloadScoreQuery(sq2, minFunc, PayloadDecoder.FLOAT_DECODER, true);
  PayloadScoreQuery query3 = new PayloadScoreQuery(sq2, maxFunc, PayloadDecoder.FLOAT_DECODER, true);
  PayloadScoreQuery query4 = new PayloadScoreQuery(sq2, maxFunc, PayloadDecoder.FLOAT_DECODER, false);
  PayloadScoreQuery query5 = new PayloadScoreQuery(sq1, minFunc, PayloadDecoder.FLOAT_DECODER);

  assertEquals(query1, query5);
  assertFalse(query1.equals(query2));
  assertFalse(query1.equals(query3));
  assertFalse(query1.equals(query4));
  assertFalse(query2.equals(query3));
  assertFalse(query2.equals(query4));
  assertFalse(query3.equals(query4));
}
 
Example #11
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 #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: 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 #14
Source File: TestPayloadTermQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testNoMatch() throws Exception {
  SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.FIELD, "junk")),
          new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  TopDocs hits = searcher.search(query, 100);
  assertTrue("hits is null and it shouldn't be", hits != null);
  assertTrue("hits Size: " + hits.totalHits.value + " is not: " + 0, hits.totalHits.value == 0);

}
 
Example #15
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 #16
Source File: TestPayloadSpans.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testHeavilyNestedSpanQuery() throws Exception {
  Spans spans;
  IndexSearcher searcher = getSearcher();

  SpanQuery[] clauses = new SpanQuery[3];
  clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "two"));
  clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three"));

  SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 5, true);
 
  clauses = new SpanQuery[3];
  clauses[0] = spanNearQuery; 
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "five"));
  clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "six"));

  SpanNearQuery spanNearQuery2 = new SpanNearQuery(clauses, 6, true);
   
  SpanQuery[] clauses2 = new SpanQuery[2];
  clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "eleven"));
  clauses2[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ten"));
  SpanNearQuery spanNearQuery3 = new SpanNearQuery(clauses2, 2, false);
  
  SpanQuery[] clauses3 = new SpanQuery[3];
  clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nine"));
  clauses3[1] = spanNearQuery2;
  clauses3[2] = spanNearQuery3;
   
  SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false);

  spans = nestedSpanNearQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);
  assertTrue("spans is null and it shouldn't be", spans != null);
  checkSpans(spans, 2, new int[]{8, 8});
  closeIndexReader.close();
  directory.close();
}
 
Example #17
Source File: MtasSpanEndQuery.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  SpanWeight spanWeight = ((SpanQuery) searcher.rewrite(clause))
      .createWeight(searcher, needsScores, boost);
  return new SpanTermWeight(spanWeight, searcher, boost);
}
 
Example #18
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 #19
Source File: TestMemoryIndexAgainstDirectory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testPassesIfWrapped() throws IOException {
  RegexpQuery regex = new RegexpQuery(new Term("field", "worl."));
  SpanQuery wrappedquery = new SpanOrQuery(new SpanMultiTermQueryWrapper<>(regex));

  MemoryIndex mindex = randomMemoryIndex();
  mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", "hello there"));

  // This passes though
  assertEquals(0, mindex.search(wrappedquery), 0.00001f);
  TestUtil.checkReader(mindex.createSearcher().getIndexReader());
}
 
Example #20
Source File: TestPayloadTermQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testMultipleMatchesPerDoc() throws Exception {
  SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")),
          new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  TopDocs hits = searcher.search(query, 100);
  assertTrue("hits is null and it shouldn't be", hits != null);
  assertTrue("hits Size: " + hits.totalHits.value + " is not: " + 100, hits.totalHits.value == 100);

  //they should all have the exact same score, because they all contain seventy once, and we set
  //all the other similarity factors to be 1

  //System.out.println("Hash: " + seventyHash + " Twice Hash: " + 2*seventyHash);
  //there should be exactly 10 items that score a 4, all the rest should score a 2
  //The 10 items are: 70 + i*100 where i in [0-9]
  int numTens = 0;
  for (int i = 0; i < hits.scoreDocs.length; i++) {
    ScoreDoc doc = hits.scoreDocs[i];
    if (doc.doc % 10 == 0) {
      numTens++;
      assertTrue(doc.score + " does not equal: " + 4.0, doc.score == 4.0);
    } else {
      assertTrue(doc.score + " does not equal: " + 2, doc.score == 2);
    }
  }
  assertTrue(numTens + " does not equal: " + 10, numTens == 10);
  CheckHits.checkExplanations(query, "field", searcher, true);
  Spans spans = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertTrue("spans is null and it shouldn't be", spans != null);
  //should be two matches per document
  int count = 0;
  //100 hits times 2 matches per hit, we should have 200 in count
  while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
    while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
      count++;
    }
  }
  assertTrue(count + " does not equal: " + 200, count == 200);
}
 
Example #21
Source File: TestPayloadScoreQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedNearQuery() throws Exception {

  // (one OR hundred) NEAR (twenty two) ~ 1
  //  2    4        4    4
  // one hundred twenty two
  // two hundred twenty two

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

  // check includeSpanScore makes a difference here
  searcher.setSimilarity(new ClassicSimilarity());
  try {
    checkQuery(q, new MaxPayloadFunction(), new int[]{ 122, 222 }, new float[]{ 20.901256561279297f, 17.06580352783203f });
    checkQuery(q, new MinPayloadFunction(), new int[]{ 222, 122 }, new float[]{ 17.06580352783203f, 10.450628280639648f });
    checkQuery(q, new AveragePayloadFunction(), new int[] { 122, 222 }, new float[]{ 19.15948486328125f, 17.06580352783203f });
    checkQuery(q, new MaxPayloadFunction(), false, new int[]{122, 222}, new float[]{4.0f, 4.0f});
    checkQuery(q, new MinPayloadFunction(), false, new int[]{222, 122}, new float[]{4.0f, 2.0f});
    checkQuery(q, new AveragePayloadFunction(), false, new int[]{222, 122}, new float[]{4.0f, 3.666666f});
  }
  finally {
    searcher.setSimilarity(similarity);
  }

}
 
Example #22
Source File: TestPayloadSpans.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testShrinkToAfterShortestMatch2() throws IOException {
  Directory directory = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
                                                   newIndexWriterConfig(new TestPayloadAnalyzer()));

  Document doc = new Document();
  doc.add(new TextField("content", new StringReader("a b a d k f a h i k a k")));
  writer.addDocument(doc);
  IndexReader reader = writer.getReader();
  IndexSearcher is = newSearcher(getOnlyLeafReader(reader), false);
  writer.close();

  SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
  SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));
  SpanQuery[] sqs = { stq1, stq2 };
  SpanNearQuery snq = new SpanNearQuery(sqs, 0, true);
  VerifyingCollector collector = new VerifyingCollector();
  Spans spans = snq.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);

  TopDocs topDocs = is.search(snq, 1);
  Set<String> payloadSet = new HashSet<>();
  for (int i = 0; i < topDocs.scoreDocs.length; i++) {
    while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
      while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
        collector.reset();
        spans.collect(collector);
        for (final BytesRef payload: collector.payloads) {
          payloadSet.add(Term.toString(payload));
        }
      }
    }
  }
  assertEquals(2, payloadSet.size());
  assertTrue(payloadSet.contains("a:Noise:10"));
  assertTrue(payloadSet.contains("k:Noise:11"));
  reader.close();
  directory.close();
}
 
Example #23
Source File: TestPayloadTermQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testQuery() {
  SpanQuery boostingFuncTermQuery = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")),
      new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  QueryUtils.check(boostingFuncTermQuery);
  
  SpanTermQuery spanTermQuery = new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"));

  assertTrue(boostingFuncTermQuery.equals(spanTermQuery) == spanTermQuery.equals(boostingFuncTermQuery));
  
  SpanQuery boostingFuncTermQuery2 = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")),
      new AveragePayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  
  QueryUtils.checkUnequal(boostingFuncTermQuery, boostingFuncTermQuery2);
}
 
Example #24
Source File: SpanFirstBuilder.java    From lucene-solr with 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 #25
Source File: SpanPositionRangeBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
  int start = DOMUtils.getAttribute(e, "start", 1);
  int end = DOMUtils.getAttribute(e, "end", 1);
  Element child = DOMUtils.getFirstChildElement(e);
  SpanQuery q = factory.getSpanQuery(child);

  SpanPositionRangeQuery query = new SpanPositionRangeQuery(q, start, end);

  float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
  return new SpanBoostQuery(query, boost);
}
 
Example #26
Source File: TestBooleanQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testBooleanSpanQuery() throws Exception {
  boolean failed = false;
  int hits = 0;
  Directory directory = newDirectory();
  Analyzer indexerAnalyzer = new MockAnalyzer(random());

  IndexWriterConfig config = new IndexWriterConfig(indexerAnalyzer);
  IndexWriter writer = new IndexWriter(directory, config);
  String FIELD = "content";
  Document d = new Document();
  d.add(new TextField(FIELD, "clockwork orange", Field.Store.YES));
  writer.addDocument(d);
  writer.close();

  IndexReader indexReader = DirectoryReader.open(directory);
  IndexSearcher searcher = newSearcher(indexReader);

  BooleanQuery.Builder query = new BooleanQuery.Builder();
  SpanQuery sq1 = new SpanTermQuery(new Term(FIELD, "clockwork"));
  SpanQuery sq2 = new SpanTermQuery(new Term(FIELD, "clckwork"));
  query.add(sq1, BooleanClause.Occur.SHOULD);
  query.add(sq2, BooleanClause.Occur.SHOULD);
  TopScoreDocCollector collector = TopScoreDocCollector.create(1000, Integer.MAX_VALUE);
  searcher.search(query.build(), collector);
  hits = collector.topDocs().scoreDocs.length;
  for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs){
    System.out.println(scoreDoc.doc);
  }
  indexReader.close();
  assertEquals("Bug in boolean query composed of span queries", failed, false);
  assertEquals("Bug in boolean query composed of span queries", hits, 1);
  directory.close();
}
 
Example #27
Source File: BoostingTermBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
  String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
  String value = DOMUtils.getNonBlankTextOrFail(e);

  // TODO make function and decoder pluggable somehow?
  SpanQuery btq = new PayloadScoreQuery(new SpanTermQuery(new Term(fieldName, value)),
      new AveragePayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  btq = new SpanBoostQuery(btq, DOMUtils.getAttribute(e, "boost", 1.0f));
  return btq;
}
 
Example #28
Source File: MtasSpanStartQuery.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  SpanWeight spanWeight = ((SpanQuery) searcher.rewrite(clause))
      .createWeight(searcher, needsScores, boost);
  return new SpanTermWeight(spanWeight, searcher, boost);
}
 
Example #29
Source File: MtasSpanOrQuery.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public String toString(String field) {
  StringBuilder buffer = new StringBuilder();
  buffer.append(this.getClass().getSimpleName() + "([");
  Iterator<MtasSpanQuery> i = clauses.iterator();
  while (i.hasNext()) {
    SpanQuery clause = i.next();
    buffer.append(clause.toString(field));
    if (i.hasNext()) {
      buffer.append(", ");
    }
  }
  buffer.append("])");
  return buffer.toString();
}
 
Example #30
Source File: TestPayloadSpans.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFirstClauseWithoutPayload() throws Exception {
  Spans spans;
  IndexSearcher searcher = getSearcher();

  SpanQuery[] clauses = new SpanQuery[3];
  clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nopayload"));
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "qq"));
  clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ss"));

  SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 6, true);
  
  SpanQuery[] clauses2 = new SpanQuery[2];
   
  clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "pp"));
  clauses2[1] = spanNearQuery;

  SpanNearQuery snq = new SpanNearQuery(clauses2, 6, false);
  
  SpanQuery[] clauses3 = new SpanQuery[2];
   
  clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "np"));
  clauses3[1] = snq;

  SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false);
  spans = nestedSpanNearQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);

  assertTrue("spans is null and it shouldn't be", spans != null);
  checkSpans(spans, 1, new int[]{3});
  closeIndexReader.close();
  directory.close();
}