org.apache.lucene.search.TopFieldDocs Java Examples

The following examples show how to use org.apache.lucene.search.TopFieldDocs. 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: TestDemoExpressions.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** tests the returned sort values are correct */
public void testSortValues() throws Exception {
  Expression expr = JavascriptCompiler.compile("sqrt(_score)");
  
  SimpleBindings bindings = new SimpleBindings();
  bindings.add("_score", DoubleValuesSource.SCORES);

  Sort sort = new Sort(expr.getSortField(bindings, true));
  Query query = new TermQuery(new Term("body", "contents"));
  TopFieldDocs td = searcher.search(query, 3, sort, true);
  for (int i = 0; i < 3; i++) {
    FieldDoc d = (FieldDoc) td.scoreDocs[i];
    float expected = (float) Math.sqrt(d.score);
    float actual = ((Double)d.fields[0]).floatValue();
    assertEquals(expected, actual, 0d);
  }
}
 
Example #2
Source File: TestDemoExpressions.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** tests same binding used more than once in an expression */
public void testTwoOfSameBinding() throws Exception {
  Expression expr = JavascriptCompiler.compile("_score + _score");
  
  SimpleBindings bindings = new SimpleBindings();
  bindings.add("_score", DoubleValuesSource.SCORES);
  
  Sort sort = new Sort(expr.getSortField(bindings, true));
  Query query = new TermQuery(new Term("body", "contents"));
  TopFieldDocs td = searcher.search(query, 3, sort, true);
  for (int i = 0; i < 3; i++) {
    FieldDoc d = (FieldDoc) td.scoreDocs[i];
    float expected = 2*d.score;
    float actual = ((Double)d.fields[0]).floatValue();
    assertEquals(expected, actual, 0d);
  }
}
 
Example #3
Source File: TestDemoExpressions.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Uses variables with $ */
public void testDollarVariable() throws Exception {
  Expression expr = JavascriptCompiler.compile("$0+$score");
  
  SimpleBindings bindings = new SimpleBindings();
  bindings.add("$0", DoubleValuesSource.SCORES);
  bindings.add("$score", DoubleValuesSource.SCORES);

  Sort sort = new Sort(expr.getSortField(bindings, true));
  Query query = new TermQuery(new Term("body", "contents"));
  TopFieldDocs td = searcher.search(query, 3, sort, true);
  for (int i = 0; i < 3; i++) {
    FieldDoc d = (FieldDoc) td.scoreDocs[i];
    float expected = 2*d.score;
    float actual = ((Double)d.fields[0]).floatValue();
    assertEquals(expected, actual, 0d);
  }
}
 
Example #4
Source File: TestDemoExpressions.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** tests expression referring to another expression */
public void testExpressionRefersToExpression() throws Exception {
  Expression expr1 = JavascriptCompiler.compile("_score");
  Expression expr2 = JavascriptCompiler.compile("2*expr1");
  
  SimpleBindings bindings = new SimpleBindings();
  bindings.add("_score", DoubleValuesSource.SCORES);
  bindings.add("expr1", expr1);
  
  Sort sort = new Sort(expr2.getSortField(bindings, true));
  Query query = new TermQuery(new Term("body", "contents"));
  TopFieldDocs td = searcher.search(query, 3, sort, true);
  for (int i = 0; i < 3; i++) {
    FieldDoc d = (FieldDoc) td.scoreDocs[i];
    float expected = 2*d.score;
    float actual = ((Double)d.fields[0]).floatValue();
    assertEquals(expected, actual, 0d);
  }
}
 
Example #5
Source File: TestDemoExpressions.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void doTestLotsOfBindings(int n) throws Exception {
  SimpleBindings bindings = new SimpleBindings();    
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      sb.append("+");
    }
    sb.append("x" + i);
    bindings.add("x" + i, DoubleValuesSource.SCORES);
  }
  
  Expression expr = JavascriptCompiler.compile(sb.toString());
  Sort sort = new Sort(expr.getSortField(bindings, true));
  Query query = new TermQuery(new Term("body", "contents"));
  TopFieldDocs td = searcher.search(query, 3, sort, true);
  for (int i = 0; i < 3; i++) {
    FieldDoc d = (FieldDoc) td.scoreDocs[i];
    float expected = n*d.score;
    float actual = ((Double)d.fields[0]).floatValue();
    assertEquals(expected, actual, 0d);
  }
}
 
Example #6
Source File: FacetsCollector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Utility method, to search and also collect all hits
 *  into the provided {@link Collector}. */
public static TopFieldDocs search(IndexSearcher searcher, Query q, int n, Sort sort, Collector fc) throws IOException {
  if (sort == null) {
    throw new IllegalArgumentException("sort must not be null");
  }
  return (TopFieldDocs) doSearch(searcher, null, q, n, sort, false, fc);
}
 
Example #7
Source File: FacetsCollector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Utility method, to search and also collect all hits
 *  into the provided {@link Collector}. */
public static TopFieldDocs search(IndexSearcher searcher, Query q, int n, Sort sort, boolean doDocScores, Collector fc) throws IOException {
  if (sort == null) {
    throw new IllegalArgumentException("sort must not be null");
  }
  return (TopFieldDocs) doSearch(searcher, null, q, n, sort, doDocScores, fc);
}
 
Example #8
Source File: FloatPointNearestNeighbor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static TopFieldDocs nearest(IndexSearcher searcher, String field, int topN, float... origin) throws IOException {
  if (topN < 1) {
    throw new IllegalArgumentException("topN must be at least 1; got " + topN);
  }
  if (field == null) {
    throw new IllegalArgumentException("field must not be null");
  }
  if (searcher == null) {
    throw new IllegalArgumentException("searcher must not be null");
  }
  List<BKDReader> readers = new ArrayList<>();
  List<Integer> docBases = new ArrayList<>();
  List<Bits> liveDocs = new ArrayList<>();
  int totalHits = 0;
  for (LeafReaderContext leaf : searcher.getIndexReader().leaves()) {
    PointValues points = leaf.reader().getPointValues(field);
    if (points != null) {
      if (points instanceof BKDReader == false) {
        throw new IllegalArgumentException("can only run on Lucene60PointsReader points implementation, but got " + points);
      }
      totalHits += points.getDocCount();
      readers.add((BKDReader)points);
      docBases.add(leaf.docBase);
      liveDocs.add(leaf.reader().getLiveDocs());
    }
  }

  NearestHit[] hits = nearest(readers, liveDocs, docBases, topN, origin);

  // Convert to TopFieldDocs:
  ScoreDoc[] scoreDocs = new ScoreDoc[hits.length];
  for(int i=0;i<hits.length;i++) {
    NearestHit hit = hits[i];
    scoreDocs[i] = new FieldDoc(hit.docID, 0.0f, new Object[] { (float)Math.sqrt(hit.distanceSquared) });
  }
  return new TopFieldDocs(new TotalHits(totalHits, TotalHits.Relation.EQUAL_TO), scoreDocs, null);
}
 
Example #9
Source File: TestNumericDocValuesUpdates.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testMultipleUpdatesSameDoc() throws Exception {

    Directory dir = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    
    conf.setMaxBufferedDocs(3); // small number of docs, so use a tiny maxBufferedDocs

    IndexWriter writer = new IndexWriter(dir, conf);

    writer.updateDocument       (new Term("id","doc-1"), doc(1, 1000000000L ));
    writer.updateNumericDocValue(new Term("id","doc-1"), "val", 1000001111L );
    writer.updateDocument       (new Term("id","doc-2"), doc(2, 2000000000L ));
    writer.updateDocument       (new Term("id","doc-2"), doc(2, 2222222222L ));
    writer.updateNumericDocValue(new Term("id","doc-1"), "val", 1111111111L );

    final DirectoryReader reader;
    if (random().nextBoolean()) {
      writer.commit();
      reader = DirectoryReader.open(dir);
    } else {
      reader = DirectoryReader.open(writer);
    }
    final IndexSearcher searcher = new IndexSearcher(reader);
    TopFieldDocs td;
    
    td = searcher.search(new TermQuery(new Term("id", "doc-1")), 1, 
                         new Sort(new SortField("val", SortField.Type.LONG)));
    assertEquals("doc-1 missing?", 1, td.scoreDocs.length);
    assertEquals("doc-1 value", 1111111111L, ((FieldDoc)td.scoreDocs[0]).fields[0]);
    
    td = searcher.search(new TermQuery(new Term("id", "doc-2")), 1, 
                        new Sort(new SortField("val", SortField.Type.LONG)));
    assertEquals("doc-2 missing?", 1, td.scoreDocs.length);
    assertEquals("doc-2 value", 2222222222L, ((FieldDoc)td.scoreDocs[0]).fields[0]);
    
    IOUtils.close(reader, writer, dir);
  }
 
Example #10
Source File: BlendedInfixSuggester.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Lookup.LookupResult> createResults(IndexSearcher searcher, TopFieldDocs hits, int num, CharSequence key,
                                                  boolean doHighlight, Set<String> matchedTokens, String prefixToken)
    throws IOException {

  TreeSet<Lookup.LookupResult> results = new TreeSet<>(LOOKUP_COMP);

  // we reduce the num to the one initially requested
  int actualNum = num / numFactor;

  for (int i = 0; i < hits.scoreDocs.length; i++) {
    FieldDoc fd = (FieldDoc) hits.scoreDocs[i];

    BinaryDocValues textDV = MultiDocValues.getBinaryValues(searcher.getIndexReader(), TEXT_FIELD_NAME);
    assert textDV != null;

    textDV.advance(fd.doc);

    final String text = textDV.binaryValue().utf8ToString();
    long weight = (Long) fd.fields[0];

    // This will just be null if app didn't pass payloads to build():
    // TODO: maybe just stored fields?  they compress...
    BinaryDocValues payloadsDV = MultiDocValues.getBinaryValues(searcher.getIndexReader(), "payloads");

    BytesRef payload;
    if (payloadsDV != null) {
      if (payloadsDV.advance(fd.doc) == fd.doc) {
        payload = BytesRef.deepCopyOf(payloadsDV.binaryValue());
      } else {
        payload = new BytesRef(BytesRef.EMPTY_BYTES);
      }
    } else {
      payload = null;
    }

    double coefficient;
    if (text.startsWith(key.toString())) {
      // if hit starts with the key, we don't change the score
      coefficient = 1;
    } else {
      coefficient = createCoefficient(searcher, fd.doc, matchedTokens, prefixToken);
    }
    if (weight == 0) {
      weight = 1;
    }
    if (weight < 1 / LINEAR_COEF && weight > -1 / LINEAR_COEF) {
      weight *= 1 / LINEAR_COEF;
    }
    long score = (long) (weight * coefficient);

    LookupResult result;
    if (doHighlight) {
      result = new LookupResult(text, highlight(text, matchedTokens, prefixToken), score, payload);
    } else {
      result = new LookupResult(text, score, payload);
    }

    boundedTreeAdd(results, result, actualNum);
  }

  return new ArrayList<>(results.descendingSet());
}