org.apache.lucene.search.MatchAllDocsQuery Java Examples

The following examples show how to use org.apache.lucene.search.MatchAllDocsQuery. 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: AssociationsFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** User runs a query and aggregates facets by summing their association values. */
private List<FacetResult> sumAssociations() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  
  FacetsCollector fc = new FacetsCollector();
  
  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
  
  Facets tags = new TaxonomyFacetSumIntAssociations("$tags", taxoReader, config, fc);
  Facets genre = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc);

  // Retrieve results
  List<FacetResult> results = new ArrayList<>();
  results.add(tags.getTopChildren(10, "tags"));
  results.add(genre.getTopChildren(10, "genre"));

  indexReader.close();
  taxoReader.close();
  
  return results;
}
 
Example #2
Source File: LireRequestHandler.java    From liresolr with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a random set of documents from the index. Mainly for testing purposes.
 *
 * @param req
 * @param rsp
 * @throws IOException
 */
private void handleRandomSearch(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
    SolrIndexSearcher searcher = req.getSearcher();
    Query query = new MatchAllDocsQuery();
    DocList docList = searcher.getDocList(query, getFilterQueries(req), Sort.RELEVANCE, 0, numberOfCandidateResults, 0);
    int paramRows = Math.min(req.getParams().getInt("rows", defaultNumberOfResults), docList.size());
    if (docList.size() < 1) {
        rsp.add("Error", "No documents in index");
    } else {
        LinkedList list = new LinkedList();
        while (list.size() < paramRows) {
            DocList auxList = docList.subset((int) (Math.random() * docList.size()), 1);
            Document doc = null;
            for (DocIterator it = auxList.iterator(); it.hasNext(); ) {
                doc = searcher.doc(it.nextDoc());
            }
            if (!list.contains(doc)) {
                list.add(doc);
            }
        }
        rsp.addResponse(list);
    }
}
 
Example #3
Source File: TestLongValueFacetCounts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testOnlyBigLongs() throws Exception {
  Directory d = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), d);
  for (long l = 0; l < 3; l++) {
    Document doc = new Document();
    doc.add(new NumericDocValuesField("field", Long.MAX_VALUE - l));
    w.addDocument(doc);
  }

  IndexReader r = w.getReader();
  w.close();

  FacetsCollector fc = new FacetsCollector();
  IndexSearcher s = newSearcher(r);
  s.search(new MatchAllDocsQuery(), fc);

  LongValueFacetCounts facets = new LongValueFacetCounts("field", fc, false);

  FacetResult result = facets.getAllChildrenSortByValue();
  assertEquals("dim=field path=[] value=3 childCount=3\n  9223372036854775805 (1)\n  " +
               "9223372036854775806 (1)\n  9223372036854775807 (1)\n",
               result.toString());
  r.close();
  d.close();
}
 
Example #4
Source File: TestBlockJoinValidation.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testAdvanceValidationForToChildBjq() throws Exception {
  Query parentQuery = new MatchAllDocsQuery();
  ToChildBlockJoinQuery blockJoinQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter);

  final LeafReaderContext context = indexSearcher.getIndexReader().leaves().get(0);
  Weight weight = indexSearcher.createWeight(indexSearcher.rewrite(blockJoinQuery), org.apache.lucene.search.ScoreMode.COMPLETE, 1);
  Scorer scorer = weight.scorer(context);
  final Bits parentDocs = parentsFilter.getBitSet(context);

  int target;
  do {
    // make the parent scorer advance to a doc ID which is not a parent
    target = TestUtil.nextInt(random(), 0, context.reader().maxDoc() - 2);
  } while (parentDocs.get(target + 1));

  final int illegalTarget = target;
  IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
    scorer.iterator().advance(illegalTarget);
  });
  assertTrue(expected.getMessage() != null && expected.getMessage().contains(ToChildBlockJoinQuery.INVALID_QUERY_MESSAGE));
}
 
Example #5
Source File: TestQueryBitSetProducer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSimple() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE);
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
  w.addDocument(new Document());
  DirectoryReader reader = w.getReader();

  QueryBitSetProducer producer = new QueryBitSetProducer(new MatchNoDocsQuery());
  assertNull(producer.getBitSet(reader.leaves().get(0)));
  assertEquals(1, producer.cache.size());

  producer = new QueryBitSetProducer(new MatchAllDocsQuery());
  BitSet bitSet = producer.getBitSet(reader.leaves().get(0));
  assertEquals(1, bitSet.length());
  assertEquals(true, bitSet.get(0));
  assertEquals(1, producer.cache.size());

  IOUtils.close(reader, w, dir);
}
 
Example #6
Source File: IpColumnReferenceTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testIpExpression() throws Exception {
    IpColumnReference columnReference = new IpColumnReference(IP_COLUMN);
    columnReference.startCollect(ctx);
    columnReference.setNextReader(readerContext);
    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), 21);
    assertThat(topDocs.scoreDocs.length, is(21));

    int i = 0;
    for (ScoreDoc doc : topDocs.scoreDocs) {
        columnReference.setNextDocId(doc.doc);
        if (i == 20) {
            assertThat(columnReference.value(), is(nullValue()));
        } else if (i < 10) {
            assertThat(columnReference.value(), is("192.168.0." + i));
        } else {
            assertThat(columnReference.value(),
                is("7bd0:8082:2df8:487e:e0df:e7b5:9362:" + Integer.toHexString(i)));
        }
        i++;
    }
}
 
Example #7
Source File: GroupingLongCollectorBenchmark.java    From crate with Apache License 2.0 6 votes vote down vote up
@Benchmark
public LongObjectHashMap<Long> measureGroupingOnSortedNumericDocValues() throws Exception {
    var weight = searcher.createWeight(new MatchAllDocsQuery(), ScoreMode.COMPLETE_NO_SCORES, 1.0f);
    var leaf = searcher.getTopReaderContext().leaves().get(0);
    var scorer = weight.scorer(leaf);
    var docValues = DocValues.getSortedNumeric(leaf.reader(), "y");
    var docIt = scorer.iterator();
    LongObjectHashMap<Long> sumByKey = new LongObjectHashMap<>();
    for (int docId = docIt.nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = docIt.nextDoc()) {
        if (docValues.advanceExact(docId)) {
            if (docValues.docValueCount() == 1) {
                long number = docValues.nextValue();
                sumByKey.compute(number, (key, oldValue) -> {
                    if (oldValue == null) {
                        return number;
                    } else {
                        return oldValue + number;
                    }
                });
            }
        }
    }
    return sumByKey;
}
 
Example #8
Source File: RecoveryStrategy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
final private void cloudDebugLog(SolrCore core, String op) {
  if (!log.isDebugEnabled()) {
    return;
  }
  try {
    RefCounted<SolrIndexSearcher> searchHolder = core.getNewestSearcher(false);
    SolrIndexSearcher searcher = searchHolder.get();
    try {
      final int totalHits = searcher.count(new MatchAllDocsQuery());
      final String nodeName = core.getCoreContainer().getZkController().getNodeName();
      log.debug("[{}] {} [{} total hits]", nodeName, op, totalHits);
    } finally {
      searchHolder.decref();
    }
  } catch (Exception e) {
    log.debug("Error in solrcloud_debug block", e);
  }
}
 
Example #9
Source File: TestSortedSetDocValuesFacets.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static Facets getAllFacets(IndexSearcher searcher, SortedSetDocValuesReaderState state,
                                   ExecutorService exec) throws IOException, InterruptedException {
  if (random().nextBoolean()) {
    FacetsCollector c = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), c);
    if (exec != null) {
      return new ConcurrentSortedSetDocValuesFacetCounts(state, c, exec);
    } else {
      return new SortedSetDocValuesFacetCounts(state, c);
    }
  } else if (exec != null) {
    return new ConcurrentSortedSetDocValuesFacetCounts(state, exec);
  } else {
    return new SortedSetDocValuesFacetCounts(state);
  }
}
 
Example #10
Source File: QueryUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Combines a scoring query with a non-scoring (filter) query.
 * If both parameters are null then return a {@link MatchAllDocsQuery}.
 * If only {@code scoreQuery} is present then return it.
 * If only {@code filterQuery} is present then return it wrapped with constant scoring.
 * If neither are null then we combine with a BooleanQuery.
 */
public static Query combineQueryAndFilter(Query scoreQuery, Query filterQuery) {
  // check for *:* is simple and avoids needless BooleanQuery wrapper even though BQ.rewrite optimizes this away
  if (scoreQuery == null || scoreQuery instanceof MatchAllDocsQuery) {
    if (filterQuery == null) {
      return new MatchAllDocsQuery(); // default if nothing -- match everything
    } else {
      return new ConstantScoreQuery(filterQuery);
    }
  } else {
    if (filterQuery == null || filterQuery instanceof MatchAllDocsQuery) {
      return scoreQuery;
    } else {
      return new BooleanQuery.Builder()
          .add(scoreQuery, Occur.MUST)
          .add(filterQuery, Occur.FILTER)
          .build();
    }
  }
}
 
Example #11
Source File: TestFieldCacheSort.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** test that we throw exception on multi-valued field, creates corrupt reader, use SORTED_SET instead */
public void testMultiValuedField() throws IOException {
  Directory indexStore = newDirectory();
  IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(new MockAnalyzer(random())));
  for(int i=0; i<5; i++) {
      Document doc = new Document();
      doc.add(new StringField("string", "a"+i, Field.Store.NO));
      doc.add(new StringField("string", "b"+i, Field.Store.NO));
      writer.addDocument(doc);
  }
  writer.forceMerge(1); // enforce one segment to have a higher unique term count in all cases
  writer.close();
  Sort sort = new Sort(
      new SortField("string", SortField.Type.STRING),
      SortField.FIELD_DOC);
  IndexReader reader = UninvertingReader.wrap(DirectoryReader.open(indexStore),
                       Collections.singletonMap("string", Type.SORTED));
  IndexSearcher searcher = new IndexSearcher(reader);
  expectThrows(IllegalStateException.class, () -> {
    searcher.search(new MatchAllDocsQuery(), 500, sort);
  });
  reader.close();
  indexStore.close();
}
 
Example #12
Source File: TestFunctionScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testNaN() throws IOException {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
  Document doc = new Document();
  doc.add(new NumericDocValuesField("foo", Double.doubleToLongBits(Double.NaN)));
  w.addDocument(doc);
  IndexReader reader = DirectoryReader.open(w);
  w.close();
  IndexSearcher searcher = newSearcher(reader);
  Query q = new FunctionScoreQuery(new MatchAllDocsQuery(), DoubleValuesSource.fromDoubleField("foo"));
  QueryUtils.check(random(), q, searcher);
  Explanation expl = searcher.explain(q, 0);
  assertEquals(0, expl.getValue().doubleValue(), 0f);
  assertTrue(expl.toString(), expl.getDetails()[0].getDescription().contains("NaN is an illegal score"));
  reader.close();
  dir.close();
}
 
Example #13
Source File: TestFunctionScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testTruncateNegativeScores() throws IOException {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
  Document doc = new Document();
  doc.add(new NumericDocValuesField("foo", -2));
  w.addDocument(doc);
  IndexReader reader = DirectoryReader.open(w);
  w.close();
  IndexSearcher searcher = newSearcher(reader);
  Query q = new FunctionScoreQuery(new MatchAllDocsQuery(), DoubleValuesSource.fromLongField("foo"));
  QueryUtils.check(random(), q, searcher);
  Explanation expl = searcher.explain(q, 0);
  assertEquals(0, expl.getValue().doubleValue(), 0f);
  assertTrue(expl.toString(), expl.getDetails()[0].getDescription().contains("truncated score"));
  reader.close();
  dir.close();
}
 
Example #14
Source File: Queries.java    From crate with Apache License 2.0 5 votes vote down vote up
/** Return a query that matches all documents but those that match the given query. */
public static Query not(Query q) {
    return new BooleanQuery.Builder()
        .add(new MatchAllDocsQuery(), Occur.MUST)
        .add(q, Occur.MUST_NOT)
        .build();
}
 
Example #15
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testLongGetAllDims() throws Exception {
  Directory d = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), d);
  Document doc = new Document();
  NumericDocValuesField field = new NumericDocValuesField("field", 0L);
  doc.add(field);
  for(long l=0;l<100;l++) {
    field.setLongValue(l);
    w.addDocument(doc);
  }

  // Also add Long.MAX_VALUE
  field.setLongValue(Long.MAX_VALUE);
  w.addDocument(doc);

  IndexReader r = w.getReader();
  w.close();

  FacetsCollector fc = new FacetsCollector();
  IndexSearcher s = newSearcher(r);
  s.search(new MatchAllDocsQuery(), fc);

  Facets facets = new LongRangeFacetCounts("field", fc,
      new LongRange("less than 10", 0L, true, 10L, false),
      new LongRange("less than or equal to 10", 0L, true, 10L, true),
      new LongRange("over 90", 90L, false, 100L, false),
      new LongRange("90 or above", 90L, true, 100L, false),
      new LongRange("over 1000", 1000L, false, Long.MAX_VALUE, true));

  List<FacetResult> result = facets.getAllDims(10);
  assertEquals(1, result.size());
  assertEquals("dim=field path=[] value=22 childCount=5\n  less than 10 (10)\n  less than or equal to 10 (11)\n  over 90 (9)\n  90 or above (10)\n  over 1000 (1)\n",
               result.get(0).toString());
  
  r.close();
  d.close();
}
 
Example #16
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testOverlappedEndStart() throws Exception {
  Directory d = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), d);
  Document doc = new Document();
  NumericDocValuesField field = new NumericDocValuesField("field", 0L);
  doc.add(field);
  for(long l=0;l<100;l++) {
    field.setLongValue(l);
    w.addDocument(doc);
  }
  field.setLongValue(Long.MAX_VALUE);
  w.addDocument(doc);

  IndexReader r = w.getReader();
  w.close();

  FacetsCollector fc = new FacetsCollector();
  IndexSearcher s = newSearcher(r);
  s.search(new MatchAllDocsQuery(), fc);

  Facets facets = new LongRangeFacetCounts("field", fc,
      new LongRange("0-10", 0L, true, 10L, true),
      new LongRange("10-20", 10L, true, 20L, true),
      new LongRange("20-30", 20L, true, 30L, true),
      new LongRange("30-40", 30L, true, 40L, true));
  
  FacetResult result = facets.getTopChildren(10, "field");
  assertEquals("dim=field path=[] value=41 childCount=4\n  0-10 (11)\n  10-20 (11)\n  20-30 (11)\n  30-40 (11)\n",
               result.toString());
  
  r.close();
  d.close();
}
 
Example #17
Source File: TestIndexReaderFunctions.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void assertHits(DoubleValuesSource vs, float scores[]) throws Exception {
  Query q = new FunctionScoreQuery(new MatchAllDocsQuery(), vs);
  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);
  assertSort(vs, expected);
}
 
Example #18
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testBasicDouble() throws Exception {
  Directory d = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), d);
  Document doc = new Document();
  DoubleDocValuesField field = new DoubleDocValuesField("field", 0.0);
  doc.add(field);
  for(long l=0;l<100;l++) {
    field.setDoubleValue(l);
    w.addDocument(doc);
  }

  IndexReader r = w.getReader();

  FacetsCollector fc = new FacetsCollector();

  IndexSearcher s = newSearcher(r);
  s.search(new MatchAllDocsQuery(), fc);
  Facets facets = new DoubleRangeFacetCounts("field", fc,
      new DoubleRange("less than 10", 0.0, true, 10.0, false),
      new DoubleRange("less than or equal to 10", 0.0, true, 10.0, true),
      new DoubleRange("over 90", 90.0, false, 100.0, false),
      new DoubleRange("90 or above", 90.0, true, 100.0, false),
      new DoubleRange("over 1000", 1000.0, false, Double.POSITIVE_INFINITY, false));
                                       
  assertEquals("dim=field path=[] value=21 childCount=5\n  less than 10 (10)\n  less than or equal to 10 (11)\n  over 90 (9)\n  90 or above (10)\n  over 1000 (0)\n",
               facets.getTopChildren(10, "field").toString());
  w.close();
  IOUtils.close(r, d);
}
 
Example #19
Source File: GraphTermsQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private FixedBitSet getLiveDocs(IndexSearcher searcher) throws IOException {
  if (!searcher.getIndexReader().hasDeletions()) {
    return null;
  }
  if (searcher instanceof SolrIndexSearcher) {
    return ((SolrIndexSearcher) searcher).getLiveDocSet().getBits();
  } else { // could happen in Delete-by-query situation
    //smallSetSize==0 thus will always produce a BitDocSet (FixedBitSet)
    DocSetCollector docSetCollector = new DocSetCollector(0, searcher.getIndexReader().maxDoc());
    searcher.search(new MatchAllDocsQuery(), docSetCollector);
    return ((BitDocSet) docSetCollector.getDocSet()).getBits();
  }
}
 
Example #20
Source File: TestQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWildcardAlone() throws ParseException {
  //seems like crazy edge case, but can be useful in concordance 
  QueryParser parser = new QueryParser(FIELD, new ASCIIAnalyzer());
  parser.setAllowLeadingWildcard(false);
  expectThrows(ParseException.class, () -> {
    parser.parse("*");
  });

  QueryParser parser2 = new QueryParser("*", new ASCIIAnalyzer());
  parser2.setAllowLeadingWildcard(false);
  assertEquals(new MatchAllDocsQuery(), parser2.parse("*"));
}
 
Example #21
Source File: TestSolrCoreParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testHandyQuery() throws IOException, ParserException {
  final String lhsXml = "<HelloQuery/>";
  final String rhsXml = "<GoodbyeQuery/>";
  final Query query = parseHandyQuery(lhsXml, rhsXml);
  assertTrue(query instanceof BooleanQuery);
  final BooleanQuery bq = (BooleanQuery)query;
  assertEquals(2, bq.clauses().size());
  assertTrue(bq.clauses().get(0).getQuery() instanceof MatchAllDocsQuery);
  assertTrue(bq.clauses().get(1).getQuery() instanceof MatchNoDocsQuery);
}
 
Example #22
Source File: TestExpressionSorts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testQueries() throws Exception {
  int n = atLeast(1);
  for (int i = 0; i < n; i++) {
    assertQuery(new MatchAllDocsQuery());
    assertQuery(new TermQuery(new Term("english", "one")));
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new TermQuery(new Term("english", "one")), BooleanClause.Occur.SHOULD);
    bq.add(new TermQuery(new Term("oddeven", "even")), BooleanClause.Occur.SHOULD);
    assertQuery(bq.build());
    // force in order
    bq.add(new TermQuery(new Term("english", "two")), BooleanClause.Occur.SHOULD);
    bq.setMinimumNumberShouldMatch(2);
    assertQuery(bq.build());
  }
}
 
Example #23
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testIntSumAssociation() throws Exception {
  
  FacetsCollector fc = new FacetsCollector();
  
  IndexSearcher searcher = newSearcher(reader);
  searcher.search(new MatchAllDocsQuery(), fc);

  Facets facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
  assertEquals("dim=int path=[] value=-1 childCount=2\n  a (200)\n  b (150)\n", facets.getTopChildren(10, "int").toString());
  assertEquals("Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
  assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
}
 
Example #24
Source File: TestFeatureSort.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFeature() throws IOException {
  Directory dir = newDirectory();
  IndexWriterConfig config = newIndexWriterConfig().setMergePolicy(newLogMergePolicy(random().nextBoolean()));
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir, config);
  Document doc = new Document();
  doc.add(new FeatureField("field", "name", 30.1F));
  doc.add(newStringField("value", "30.1", Field.Store.YES));
  writer.addDocument(doc);
  doc = new Document();
  doc.add(new FeatureField("field", "name", 1.3F));
  doc.add(newStringField("value", "1.3", Field.Store.YES));
  writer.addDocument(doc);
  doc = new Document();
  doc.add(new FeatureField("field", "name", 4.2F));
  doc.add(newStringField("value", "4.2", Field.Store.YES));
  writer.addDocument(doc);
  IndexReader ir = writer.getReader();
  writer.close();

  IndexSearcher searcher = newSearcher(ir);
  Sort sort = new Sort(FeatureField.newFeatureSort("field", "name"));

  TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
  assertEquals(3, td.totalHits.value);
  // numeric order
  assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
  assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
  assertEquals("1.3", searcher.doc(td.scoreDocs[2].doc).get("value"));

  ir.close();
  dir.close();
}
 
Example #25
Source File: LuceneBatchIteratorTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testLuceneBatchIterator() throws Exception {
    BatchIteratorTester tester = new BatchIteratorTester(
        () -> new LuceneBatchIterator(
            indexSearcher,
            new MatchAllDocsQuery(),
            null,
            false,
            new CollectorContext(),
            columnRefs,
            columnRefs
        )
    );
    tester.verifyResultAndEdgeCaseBehaviour(expectedResult);
}
 
Example #26
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testMatchAllDocs() throws Exception {
  StandardQueryParser qp = new StandardQueryParser();
  qp.setAnalyzer(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));

  assertEquals(new MatchAllDocsQuery(), qp.parse("*:*", "field"));
  assertEquals(new MatchAllDocsQuery(), qp.parse("(*:*)", "field"));
  BooleanQuery bq = (BooleanQuery) qp.parse("+*:* -*:*", "field");
  for (BooleanClause c : bq) {
    assertTrue(c.getQuery().getClass() == MatchAllDocsQuery.class);
  }
}
 
Example #27
Source File: MovieService.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param userIndex
 * @param modelKey
 * @param queryKey
 * @param filterClicked
 * @return
 * @throws Exception
 */
@LockableMethod(strategy = HashLockableStrategy.class)
public Object2FloatMap<MovieItem> getItems(@LockableParameter int userIndex, String modelKey, String queryKey, boolean filterClicked) throws Exception {
    // 标识-得分映射
    Object2FloatMap<MovieItem> item2ScoreMap = new Object2FloatOpenHashMap<>();

    long current = System.currentTimeMillis();
    Model model = models.get(modelKey);
    ArrayInstance instance = new ArrayInstance(qualityOrder, quantityOrder);
    MovieUser user = users.get(userIndex);
    Query query = StringUtility.isBlank(queryKey) ? new MatchAllDocsQuery() : queryParser.parse(queryKey, MovieItem.TITLE);
    KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, 1000);
    List<Document> documents = retrieve.getKey();
    for (int index = 0, size = documents.size(); index < size; index++) {
        Document document = documents.get(index);
        MovieItem item = items.get(document.getField(MovieItem.INDEX).numericValue().intValue());
        int itemIndex = item.getIndex();
        // 过滤条目
        if (filterClicked && user.isClicked(itemIndex)) {
            continue;
        }
        instance.setQualityFeature(userDimension, userIndex);
        instance.setQualityFeature(itemDimension, itemIndex);
        model.predict(instance);
        float score = instance.getQuantityMark();
        item2ScoreMap.put(item, score);
    }
    String message = StringUtility.format("预测数量:{},预测耗时:{}", modelKey, documents.size(), System.currentTimeMillis() - current);
    logger.info(message);

    return item2ScoreMap;
}
 
Example #28
Source File: FloatColumnReferenceTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldCacheExpression() throws Exception {
    FloatColumnReference floatColumn = new FloatColumnReference(column);
    floatColumn.startCollect(ctx);
    floatColumn.setNextReader(readerContext);
    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10);
    float f = -0.5f;
    for (ScoreDoc doc : topDocs.scoreDocs) {
        floatColumn.setNextDocId(doc.doc);
        assertThat(floatColumn.value(), is(f));
        f++;
    }
}
 
Example #29
Source File: TestDemoParallelLeafReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void testNumericDVSort(IndexSearcher s) throws IOException {
  // Confirm we can sort by the new DV field:
  TopDocs hits = s.search(new MatchAllDocsQuery(), 100, new Sort(new SortField("number", SortField.Type.LONG)));
  long last = Long.MIN_VALUE;
  for(ScoreDoc scoreDoc : hits.scoreDocs) {
    long value = Long.parseLong(s.doc(scoreDoc.doc).get("text").split(" ")[1]);
    assertTrue(value >= last);
    assertEquals(value, ((Long) ((FieldDoc) scoreDoc).fields[0]).longValue());
    last = value;
  }
}
 
Example #30
Source File: DrillDownQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Used by DrillSideways */
DrillDownQuery(FacetsConfig config, Query filter, DrillDownQuery other) {
  this.baseQuery = new BooleanQuery.Builder()
      .add(other.baseQuery == null ? new MatchAllDocsQuery() : other.baseQuery, Occur.MUST)
      .add(filter, Occur.FILTER)
      .build();
  this.dimQueries.addAll(other.dimQueries);
  this.drillDownDims.putAll(other.drillDownDims);
  this.config = config;
}