org.apache.lucene.search.ScoreDoc Java Examples

The following examples show how to use org.apache.lucene.search.ScoreDoc. 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: AlfrescoReRankQParserPlugin.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
public int compare(Object o1, Object o2) {
    ScoreDoc doc1 = (ScoreDoc) o1;
    ScoreDoc doc2 = (ScoreDoc) o2;
    float score1 = doc1.score;
    float score2 = doc2.score;
    if(boostedMap.containsKey(doc1.doc)) {
        score1 = boostedMap.get(doc1.doc);
    }

    if(boostedMap.containsKey(doc2.doc)) {
        score2 = boostedMap.get(doc2.doc);
    }

    if(score1 > score2) {
        return -1;
    } else if(score1 < score2) {
        return 1;
    } else {
        return 0;
    }
}
 
Example #2
Source File: ReRankCollector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public int compare(Object o1, Object o2) {
  ScoreDoc doc1 = (ScoreDoc) o1;
  ScoreDoc doc2 = (ScoreDoc) o2;
  float score1 = doc1.score;
  float score2 = doc2.score;
  int idx;
  if((idx = boostedMap.indexOf(doc1.doc)) >= 0) {
    score1 = boostedMap.indexGet(idx);
  }

  if((idx = boostedMap.indexOf(doc2.doc)) >= 0) {
    score2 = boostedMap.indexGet(idx);
  }

  return -Float.compare(score1, score2);
}
 
Example #3
Source File: TestLucene.java    From bookshop with MIT License 6 votes vote down vote up
private static void showSearchResults(IndexSearcher searcher, ScoreDoc[] hits, Query query, IKAnalyzer ikAnalyzer) throws IOException {
    System.out.println("找到 " + hits.length + " 个命中.");
    System.out.println("序号\t匹配度得分\t结果");
    for (int i = 0; i < hits.length; i++) {
        ScoreDoc scoreDoc = hits[i];
        int docId = scoreDoc.doc;
        Document document = searcher.doc(docId);
        List<IndexableField> fields = document.getFields();
        System.out.print((i + 1));
        System.out.print("\t" + scoreDoc.score);
        for (IndexableField f : fields) {
            System.out.print("\t" + document.get(f.name()));
        }
        System.out.println();

    }
}
 
Example #4
Source File: SearchImpl.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SearchResults search(
    Query query, SimilarityConfig simConfig, Sort sort, Set<String> fieldsToLoad, int pageSize, boolean exactHitsCount) {
  if (pageSize < 0) {
    throw new LukeException(new IllegalArgumentException("Negative integer is not acceptable for page size."));
  }

  // reset internal status to prepare for a new search session
  this.docs = new ScoreDoc[0];
  this.currentPage = 0;
  this.pageSize = pageSize;
  this.exactHitsCount = exactHitsCount;
  this.query = Objects.requireNonNull(query);
  this.sort = sort;
  this.fieldsToLoad = fieldsToLoad == null ? null : Set.copyOf(fieldsToLoad);
  searcher.setSimilarity(createSimilarity(Objects.requireNonNull(simConfig)));

  try {
    return search();
  } catch (IOException e) {
    throw new LukeException("Search Failed.", e);
  }
}
 
Example #5
Source File: TestMultiFieldQPHelper.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testStopWordSearching() throws Exception {
  Analyzer analyzer = new MockAnalyzer(random());
  Directory ramDir = newDirectory();
  IndexWriter iw = new IndexWriter(ramDir, newIndexWriterConfig(analyzer));
  Document doc = new Document();
  doc.add(newTextField("body", "blah the footest blah", Field.Store.NO));
  iw.addDocument(doc);
  iw.close();

  StandardQueryParser mfqp = new StandardQueryParser();

  mfqp.setMultiFields(new String[] { "body" });
  mfqp.setAnalyzer(analyzer);
  mfqp.setDefaultOperator(StandardQueryConfigHandler.Operator.AND);
  Query q = mfqp.parse("the footest", null);
  IndexReader ir = DirectoryReader.open(ramDir);
  IndexSearcher is = newSearcher(ir);
  ScoreDoc[] hits = is.search(q, 1000).scoreDocs;
  assertEquals(1, hits.length);
  ir.close();
  ramDir.close();
}
 
Example #6
Source File: TestNumericTerms32.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void testSorting(int precisionStep) throws Exception {
  String field="field"+precisionStep;
  // 10 random tests, the index order is ascending,
  // so using a reverse sort field should retun descending documents
  int num = TestUtil.nextInt(random(), 10, 20);
  for (int i = 0; i < num; i++) {
    int lower=(int)(random().nextDouble()*noDocs*distance)+startOffset;
    int upper=(int)(random().nextDouble()*noDocs*distance)+startOffset;
    if (lower>upper) {
      int a=lower; lower=upper; upper=a;
    }
    Query tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
    TopDocs topDocs = searcher.search(tq, noDocs, new Sort(new SortField(field, SortField.Type.INT, true)));
    if (topDocs.totalHits.value==0) continue;
    ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    int last = searcher.doc(sd[0].doc).getField(field).numericValue().intValue();
    for (int j=1; j<sd.length; j++) {
      int act = searcher.doc(sd[j].doc).getField(field).numericValue().intValue();
      assertTrue("Docs should be sorted backwards", last>act );
      last=act;
    }
  }
}
 
Example #7
Source File: DocsReader.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public Set<ProvenanceEventRecord> read(final TopDocs topDocs, final EventAuthorizer authorizer, final IndexReader indexReader, final Collection<Path> allProvenanceLogFiles,
        final AtomicInteger retrievalCount, final int maxResults, final int maxAttributeChars) throws IOException {
    if (retrievalCount.get() >= maxResults) {
        return Collections.emptySet();
    }

    final long start = System.nanoTime();
    final ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    final int numDocs = Math.min(scoreDocs.length, maxResults);
    final List<Document> docs = new ArrayList<>(numDocs);

    for (int i = numDocs - 1; i >= 0; i--) {
        final int docId = scoreDocs[i].doc;
        final Document d = indexReader.document(docId);
        docs.add(d);
    }

    final long readDocuments = System.nanoTime() - start;
    logger.debug("Reading {} Lucene Documents took {} millis", docs.size(), TimeUnit.NANOSECONDS.toMillis(readDocuments));
    return read(docs, authorizer, allProvenanceLogFiles, retrievalCount, maxResults, maxAttributeChars);
}
 
Example #8
Source File: QualityBenchmark.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private QualityStats analyzeQueryResults(QualityQuery qq, Query q, TopDocs td, Judge judge, PrintWriter logger, long searchTime) throws IOException {
  QualityStats stts = new QualityStats(judge.maxRecall(qq),searchTime);
  ScoreDoc sd[] = td.scoreDocs;
  long t1 = System.currentTimeMillis(); // extraction of first doc name we measure also construction of doc name extractor, just in case.
  DocNameExtractor xt = new DocNameExtractor(docNameField);
  for (int i=0; i<sd.length; i++) {
    String docName = xt.docName(searcher,sd[i].doc);
    long docNameExtractTime = System.currentTimeMillis() - t1;
    t1 = System.currentTimeMillis();
    boolean isRelevant = judge.isRelevant(docName,qq);
    stts.addResult(i+1,isRelevant, docNameExtractTime);
  }
  if (logger!=null) {
    logger.println(qq.getQueryID()+"  -  "+q);
    stts.log(qq.getQueryID()+" Stats:",1,logger,"  ");
  }
  return stts;
}
 
Example #9
Source File: ScoreDocRowFunction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Row apply(@Nullable ScoreDoc input) {
    if (input == null) {
        return null;
    }
    FieldDoc fieldDoc = (FieldDoc) input;
    scorer.score(fieldDoc.score);
    for (OrderByCollectorExpression orderByCollectorExpression : orderByCollectorExpressions) {
        orderByCollectorExpression.setNextFieldDoc(fieldDoc);
    }
    List<LeafReaderContext> leaves = indexReader.leaves();
    int readerIndex = ReaderUtil.subIndex(fieldDoc.doc, leaves);
    LeafReaderContext subReaderContext = leaves.get(readerIndex);
    int subDoc = fieldDoc.doc - subReaderContext.docBase;
    for (LuceneCollectorExpression<?> expression : expressions) {
        expression.setNextReader(subReaderContext);
        expression.setNextDocId(subDoc);
    }
    return inputRow;
}
 
Example #10
Source File: LtrQueryTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
private void assertScoresMatch(List<PrebuiltFeature> features, float[] scores,
                               RankerQuery ltrQuery, ScoreDoc scoreDoc) throws IOException {
    Document d = searcherUnderTest.doc(scoreDoc.doc);
    String idVal = d.get("id");
    int docId = Integer.decode(idVal);
    float modelScore = scores[docId];
    float queryScore = scoreDoc.score;

    assertEquals("Scores match with similarity " + similarity.getClass(), modelScore,
            queryScore, SCORE_NB_ULP_PREC *Math.ulp(modelScore));

    if (!(similarity instanceof TFIDFSimilarity)) {
        // There are precision issues with these similarities when using explain
        // It produces 0.56103003 for feat:0 in doc1 using score() but 0.5610301 using explain
        Explanation expl = searcherUnderTest.explain(ltrQuery, docId);

        assertEquals("Explain scores match with similarity " + similarity.getClass(), expl.getValue().floatValue(),
                queryScore, 5 * Math.ulp(modelScore));
        checkFeatureNames(expl, features);
    }
}
 
Example #11
Source File: TestNumericRangeQuery32.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void testLeftOpenRange(int precisionStep) throws Exception {
  String field="field"+precisionStep;
  int count=3000;
  int upper=(count-1)*distance + (distance/3) + startOffset;
  LegacyNumericRangeQuery<Integer> q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, null, upper, true, true);
  TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
  ScoreDoc[] sd = topDocs.scoreDocs;
  assertNotNull(sd);
  assertEquals("Score doc count", count, sd.length );
  Document doc=searcher.doc(sd[0].doc);
  assertEquals("First doc", startOffset, doc.getField(field).numericValue().intValue());
  doc=searcher.doc(sd[sd.length-1].doc);
  assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().intValue());
  
  q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, null, upper, false, true);
  topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
  sd = topDocs.scoreDocs;
  assertNotNull(sd);
  assertEquals("Score doc count", count, sd.length );
  doc=searcher.doc(sd[0].doc);
  assertEquals("First doc", startOffset, doc.getField(field).numericValue().intValue());
  doc=searcher.doc(sd[sd.length-1].doc);
  assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().intValue());
}
 
Example #12
Source File: TestNumericRangeQuery64.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void testRightOpenRange(int precisionStep) throws Exception {
  String field="field"+precisionStep;
  int count=3000;
  long lower=(count-1)*distance + (distance/3) +startOffset;
  LegacyNumericRangeQuery<Long> q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, true);
  TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
  ScoreDoc[] sd = topDocs.scoreDocs;
  assertNotNull(sd);
  assertEquals("Score doc count", noDocs-count, sd.length );
  Document doc=searcher.doc(sd[0].doc);
  assertEquals("First doc", count*distance+startOffset, doc.getField(field).numericValue().longValue() );
  doc=searcher.doc(sd[sd.length-1].doc);
  assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );

  q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, false);
  topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
  sd = topDocs.scoreDocs;
  assertNotNull(sd);
  assertEquals("Score doc count", noDocs-count, sd.length );
  doc=searcher.doc(sd[0].doc);
  assertEquals("First doc", count*distance+startOffset, doc.getField(field).numericValue().longValue() );
  doc=searcher.doc(sd[sd.length-1].doc);
  assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );
}
 
Example #13
Source File: ProductIndex.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public List<ProductCatalogEntry> search(String queryString) throws IOException, ParseException {
	List<ProductCatalogEntry> results = new ArrayList<ProductCatalogEntry>();
	
	IndexReader reader = DirectoryReader.open(dir);
	IndexSearcher searcher = new IndexSearcher(reader);
	Analyzer analyzer = new SimpleAnalyzer();
	
	QueryParser parser = new QueryParser(searchField, analyzer);
	Query query = parser.parse(queryString);
	
	TopDocs docs = searcher.search(query, 100);
	ScoreDoc[] hits = docs.scoreDocs;
	
	for (ScoreDoc sd: hits) {
		Document doc = searcher.doc(sd.doc);
		results.add(prodcat.getProductById(doc.get("id")));
	}
	reader.close();
	
	return results;
}
 
Example #14
Source File: TestMultiFieldQueryParser.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testStopWordSearching() throws Exception {
  Analyzer analyzer = new MockAnalyzer(random());
  Directory ramDir = newDirectory();
  IndexWriter iw =  new IndexWriter(ramDir, newIndexWriterConfig(analyzer));
  Document doc = new Document();
  doc.add(newTextField("body", "blah the footest blah", Field.Store.NO));
  iw.addDocument(doc);
  iw.close();
  
  MultiFieldQueryParser mfqp = 
    new MultiFieldQueryParser(new String[] {"body"}, analyzer);
  mfqp.setDefaultOperator(QueryParser.Operator.AND);
  Query q = mfqp.parse("the footest");
  IndexReader ir = DirectoryReader.open(ramDir);
  IndexSearcher is = newSearcher(ir);
  ScoreDoc[] hits = is.search(q, 1000).scoreDocs;
  assertEquals(1, hits.length);
  ir.close();
  ramDir.close();
}
 
Example #15
Source File: LuceneTranslator.java    From Indra with MIT License 6 votes vote down vote up
private Map<String, List<String>> doTranslate(Set<String> terms) {

        Map<String, List<String>> res = new HashMap<>();

        try {
            TopDocs topDocs = LuceneUtils.getTopDocs(searcher, terms, TERM_FIELD);


            if (topDocs != null) {
                for (ScoreDoc sd : topDocs.scoreDocs) {
                    Document doc = searcher.doc(sd.doc);
                    Map<String, Double> content = convert(doc.getBinaryValue(TRANSLATION_FIELD).bytes);
                    res.put(doc.get(TERM_FIELD), getRelevantTranslations(content));
                }
            }

        } catch (IOException e) {
            logger.error(e.getMessage());
            //TODO throw new expection here.
            e.printStackTrace();
        }

        return res;
    }
 
Example #16
Source File: LuceneSearchIndex.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private List<Doc> toDocs(ScoreDoc[] hits, Searcher searcher) throws IOException{
  List<Doc> documentList = new ArrayList<>();
  for (int i = 0; i < hits.length; ++i) {
    ScoreDoc scoreDoc = hits[i];
    Document doc = searcher.doc(scoreDoc.doc);
    IndexableField idField = doc.getField("_id");
    if(idField == null){
      // deleted between index hit and retrieval.
      continue;
    }
    final BytesRef ref = idField.binaryValue();
    final byte[] bytes = new byte[ref.length];
    System.arraycopy(ref.bytes, ref.offset, bytes, 0, ref.length);
    Doc outputDoc = new Doc(scoreDoc, bytes, 0 /*version*/);
    documentList.add(outputDoc);
  }
  return documentList;
}
 
Example #17
Source File: RetrievalApp.java    From lucene4ir with Apache License 2.0 6 votes vote down vote up
public ScoreDoc[] runQuery(String qno, String queryTerms){
    ScoreDoc[] hits = null;

    System.out.println("Query No.: " + qno + " " + queryTerms);
    try {
        Query query = parser.parse(QueryParser.escape(queryTerms));

        try {
            TopDocs results = searcher.search(query, p.maxResults);
            hits = results.scoreDocs;
        }
        catch (IOException ioe){
            ioe.printStackTrace();
            System.exit(1);
        }
    } catch (ParseException pe){
        pe.printStackTrace();
        System.exit(1);
    }
    return hits;
}
 
Example #18
Source File: SearchDfsQueryThenFetchAsyncAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
void innerExecuteFetchPhase() throws Exception {
    boolean useScroll = request.scroll() != null;
    sortedShardList = searchPhaseController.sortDocs(useScroll, queryResults);
    searchPhaseController.fillDocIdsToLoad(docIdsToLoad, sortedShardList);

    if (docIdsToLoad.asList().isEmpty()) {
        finishHim();
        return;
    }

    final ScoreDoc[] lastEmittedDocPerShard = searchPhaseController.getLastEmittedDocPerShard(
        request, sortedShardList, firstResults.length()
    );
    final AtomicInteger counter = new AtomicInteger(docIdsToLoad.asList().size());
    for (final AtomicArray.Entry<IntArrayList> entry : docIdsToLoad.asList()) {
        QuerySearchResult queryResult = queryResults.get(entry.index);
        DiscoveryNode node = nodes.get(queryResult.shardTarget().nodeId());
        ShardFetchSearchRequest fetchSearchRequest = createFetchRequest(queryResult, entry, lastEmittedDocPerShard);
        executeFetch(entry.index, queryResult.shardTarget(), counter, fetchSearchRequest, node);
    }
}
 
Example #19
Source File: LindenResultParser.java    From linden with Apache License 2.0 6 votes vote down vote up
private List<LindenHit> parseLindenHits(ScoreDoc[] hits) throws IOException {
  List<LindenHit> lindenHits = new ArrayList<>();
  String idFieldName = config.getSchema().getId();
  for (ScoreDoc hit : hits) {
    LindenHit lindenHit = new LindenHit();
    if (Double.isNaN(hit.score)) {
      // get score for cluster result merge
      if (sortScoreFieldPos != -1) {
        lindenHit.setScore(Double.valueOf(((FieldDoc) hit).fields[sortScoreFieldPos].toString()));
      } else {
        lindenHit.setScore(1);
      }
    } else {
      lindenHit.setScore(hit.score);
    }
    String id = LindenUtil.getFieldStringValue(leaves, hit.doc, idFieldName);
    lindenHit.setId(id);
    lindenHit = this.parseSpatial(hit.doc, lindenHit);
    lindenHit = this.parseSort(hit, lindenHit);
    lindenHit = this.parseSource(hit.doc, lindenHit);
    lindenHit = this.parseExplain(hit.doc, lindenHit);
    lindenHits.add(lindenHit);
  }
  lindenHits = this.parseSnippets(lindenHits, hits);
  return lindenHits;
}
 
Example #20
Source File: TestNumericRangeQuery64.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void testLeftOpenRange(int precisionStep) throws Exception {
  String field="field"+precisionStep;
  int count=3000;
  long upper=(count-1)*distance + (distance/3) + startOffset;
  LegacyNumericRangeQuery<Long> q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true);

  TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
  ScoreDoc[] sd = topDocs.scoreDocs;
  assertNotNull(sd);
  assertEquals("Score doc count", count, sd.length );
  Document doc=searcher.doc(sd[0].doc);
  assertEquals("First doc", startOffset, doc.getField(field).numericValue().longValue() );
  doc=searcher.doc(sd[sd.length-1].doc);
  assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );

  q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, null, upper, false, true);
  topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
  sd = topDocs.scoreDocs;
  assertNotNull(sd);
  assertEquals("Score doc count", count, sd.length );
  doc=searcher.doc(sd[0].doc);
  assertEquals("First doc", startOffset, doc.getField(field).numericValue().longValue() );
  doc=searcher.doc(sd[sd.length-1].doc);
  assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );
}
 
Example #21
Source File: TestFieldScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void doTestExactScore (ValueSource valueSource) throws Exception {
  Query functionQuery = getFunctionQuery(valueSource);
  IndexReader r = DirectoryReader.open(dir);
  IndexSearcher s = newSearcher(r);
  TopDocs td = s.search(functionQuery,1000);
  assertEquals("All docs should be matched!",N_DOCS,td.totalHits.value);
  ScoreDoc sd[] = td.scoreDocs;
  for (ScoreDoc aSd : sd) {
    float score = aSd.score;
    log(s.explain(functionQuery, aSd.doc));
    String id = s.getIndexReader().document(aSd.doc).get(ID_FIELD);
    float expectedScore = expectedFieldScore(id); // "ID7" --> 7.0
    assertEquals("score of " + id + " shuould be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA);
  }
  r.close();
}
 
Example #22
Source File: TestTermsEnum2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** tests a pre-intersected automaton against the original */
public void testFiniteVersusInfinite() throws Exception {

  for (int i = 0; i < numIterations; i++) {
    String reg = AutomatonTestUtil.randomRegexp(random());
    Automaton automaton = Operations.determinize(new RegExp(reg, RegExp.NONE).toAutomaton(),
      DEFAULT_MAX_DETERMINIZED_STATES);
    final List<BytesRef> matchedTerms = new ArrayList<>();
    for(BytesRef t : terms) {
      if (Operations.run(automaton, t.utf8ToString())) {
        matchedTerms.add(t);
      }
    }

    Automaton alternate = Automata.makeStringUnion(matchedTerms);
    //System.out.println("match " + matchedTerms.size() + " " + alternate.getNumberOfStates() + " states, sigma=" + alternate.getStartPoints().length);
    //AutomatonTestUtil.minimizeSimple(alternate);
    //System.out.println("minimize done");
    AutomatonQuery a1 = new AutomatonQuery(new Term("field", ""), automaton);
    AutomatonQuery a2 = new AutomatonQuery(new Term("field", ""), alternate, Integer.MAX_VALUE);

    ScoreDoc[] origHits = searcher.search(a1, 25).scoreDocs;
    ScoreDoc[] newHits = searcher.search(a2, 25).scoreDocs;
    CheckHits.checkEqual(a1, origHits, newHits);
  }
}
 
Example #23
Source File: SpatialTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected SearchResults executeQuery(Query query, int numDocs) {
  try {
    TopDocs topDocs = indexSearcher.search(query, numDocs);

    List<SearchResult> results = new ArrayList<>();
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
      results.add(new SearchResult(scoreDoc.score, indexSearcher.doc(scoreDoc.doc)));
    }
    return new SearchResults(topDocs.totalHits.value, results);
  } catch (IOException ioe) {
    throw new RuntimeException("IOException thrown while executing query", ioe);
  }
}
 
Example #24
Source File: SearchController.java    From bookshop with MIT License 5 votes vote down vote up
@RequestMapping("searchBook.do")
public ModelAndView searchBook(Book book) throws IOException, ParseException {

    ModelAndView mav = new ModelAndView("searchBook");

    // 关键字
    String keyword = book.getName();
    System.out.println(keyword);
    // 准备中文分词器
    IKAnalyzer analyzer = new IKAnalyzer();
    // 索引
    Directory index = createIndex(analyzer);
    // 查询器
    Query query = new QueryParser("name",analyzer).parse(keyword);
    // 搜索
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    int numberPerPage = 10;
    ScoreDoc[] hits = searcher.search(query,numberPerPage).scoreDocs;
    List<Book> books = new ArrayList<>();
    for (int i = 0; i < hits.length; i++) {
        ScoreDoc scoreDoc = hits[i];
        int docId = scoreDoc.doc;
        Document document = searcher.doc(docId);
        Book tmpBook = bookService.get(Integer.parseInt(document.get("id")));
        books.add(tmpBook);
    }

    mav.addObject("books",books);
    return mav;
}
 
Example #25
Source File: TopGroupsResultTransformer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected ScoreDoc[] transformToNativeShardDoc(List<NamedList<Object>> documents, Sort groupSort, String shard,
                                               IndexSchema schema) {
  ScoreDoc[] scoreDocs = new ScoreDoc[documents.size()];
  int j = 0;
  for (NamedList<Object> document : documents) {
    Object docId = document.get(ID);
    if (docId != null) {
      docId = docId.toString();
    } else {
      log.error("doc {} has null 'id'", document);
    }
    Float score = (Float) document.get("score");
    if (score == null) {
      score = Float.NaN;
    }
    Object[] sortValues = null;
    Object sortValuesVal = document.get("sortValues");
    if (sortValuesVal != null) {
      sortValues = ((List) sortValuesVal).toArray();
      for (int k = 0; k < sortValues.length; k++) {
        SchemaField field = groupSort.getSort()[k].getField() != null
            ? schema.getFieldOrNull(groupSort.getSort()[k].getField()) : null;
        sortValues[k] = ShardResultTransformerUtils.unmarshalSortValue(sortValues[k], field);
      }
    } else {
      log.debug("doc {} has null 'sortValues'", document);
    }
    scoreDocs[j++] = new ShardDoc(score, sortValues, docId, shard);
  }
  return scoreDocs;
}
 
Example #26
Source File: TestFunctionRangeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoRangeQueries() throws IOException {
  Query rq1 = new FunctionRangeQuery(INT_VALUESOURCE, 2, 4, true, true);
  Query rq2 = new FunctionRangeQuery(INT_VALUESOURCE, 8, 10, true, true);
  Query bq = new BooleanQuery.Builder()
      .add(rq1, BooleanClause.Occur.SHOULD)
      .add(rq2, BooleanClause.Occur.SHOULD)
      .build();

  ScoreDoc[] scoreDocs = indexSearcher.search(bq, N_DOCS).scoreDocs;
  expectScores(scoreDocs, 10, 9, 8, 4, 3, 2);
}
 
Example #27
Source File: TestParallelCompositeReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void queryTest(Query query) throws IOException {
  ScoreDoc[] parallelHits = parallel.search(query, 1000).scoreDocs;
  ScoreDoc[] singleHits = single.search(query, 1000).scoreDocs;
  assertEquals(parallelHits.length, singleHits.length);
  for(int i = 0; i < parallelHits.length; i++) {
    assertEquals(parallelHits[i].score, singleHits[i].score, 0.001f);
    Document docParallel = parallel.doc(parallelHits[i].doc);
    Document docSingle = single.doc(singleHits[i].doc);
    assertEquals(docParallel.get("f1"), docSingle.get("f1"));
    assertEquals(docParallel.get("f2"), docSingle.get("f2"));
    assertEquals(docParallel.get("f3"), docSingle.get("f3"));
    assertEquals(docParallel.get("f4"), docSingle.get("f4"));
  }
}
 
Example #28
Source File: TestNumericRangeQuery64.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneMatchQuery() throws Exception {
  LegacyNumericRangeQuery<Long> q = LegacyNumericRangeQuery.newLongRange("ascfield8", 8, 1000L, 1000L, true, true);
  TopDocs topDocs = searcher.search(q, noDocs);
  ScoreDoc[] sd = topDocs.scoreDocs;
  assertNotNull(sd);
  assertEquals("Score doc count", 1, sd.length );
}
 
Example #29
Source File: TestValueSources.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void assertHits(Query q, float scores[]) throws Exception {
  ScoreDoc expected[] = new ScoreDoc[scores.length];
  int expectedDocs[] = new int[scores.length];
  for (int i = 0; i < expected.length; i++) {
    expectedDocs[i] = i;
    expected[i] = new ScoreDoc(i, scores[i]);
  }
  TopDocs docs = searcher.search(q, documents.size(),
      new Sort(new SortField("id", SortField.Type.STRING)), true);
  CheckHits.checkHits(random(), q, "", searcher, expectedDocs);
  CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs);
  CheckHits.checkExplanations(q, "", searcher);
}
 
Example #30
Source File: LuceneWordSearch.java    From preDict with GNU Lesser General Public License v3.0 5 votes vote down vote up
private List<String> getUsingFuzzySearch(String searchQuery) throws IOException {
	Query query = new FuzzyQuery(new Term(WORD_FIELD, searchQuery), 2, 0);
	List<String> foundWords = new ArrayList<>();
	TopDocs rs = searcher.search(query, 2);
	for (ScoreDoc docRef : rs.scoreDocs) {
		Document docHit = searcher.doc(docRef.doc);
		foundWords.add(docHit.get(WORD_FIELD));
	}
	return foundWords;
}