Java Code Examples for org.apache.lucene.index.RandomIndexWriter#addDocuments()

The following examples show how to use org.apache.lucene.index.RandomIndexWriter#addDocuments() . 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: TestBlockJoin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testAdvanceSingleParentSingleChild() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  Document childDoc = new Document();
  childDoc.add(newStringField("child", "1", Field.Store.NO));
  Document parentDoc = new Document();
  parentDoc.add(newStringField("parent", "1", Field.Store.NO));
  w.addDocuments(Arrays.asList(childDoc, parentDoc));
  IndexReader r = w.getReader();
  w.close();
  IndexSearcher s = newSearcher(r);
  Query tq = new TermQuery(new Term("child", "1"));
  BitSetProducer parentFilter = new QueryBitSetProducer(
                            new TermQuery(new Term("parent", "1")));
  CheckJoinIndex.check(s.getIndexReader(), parentFilter);

  ToParentBlockJoinQuery q = new ToParentBlockJoinQuery(tq, parentFilter, ScoreMode.Avg);
  Weight weight = s.createWeight(s.rewrite(q), org.apache.lucene.search.ScoreMode.COMPLETE, 1);
  Scorer sc = weight.scorer(s.getIndexReader().leaves().get(0));
  assertEquals(1, sc.iterator().advance(1));
  r.close();
  dir.close();
}
 
Example 2
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testParentScoringBug() throws Exception {
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

  final List<Document> docs = new ArrayList<>();
  docs.add(makeJob("java", 2007));
  docs.add(makeJob("python", 2010));
  docs.add(makeResume("Lisa", "United Kingdom"));
  w.addDocuments(docs);

  docs.clear();
  docs.add(makeJob("java", 2006));
  docs.add(makeJob("ruby", 2005));
  docs.add(makeResume("Frank", "United States"));
  w.addDocuments(docs);
  w.deleteDocuments(new Term("skill", "java")); // delete the first child of every parent

  IndexReader r = w.getReader();
  w.close();
  IndexSearcher s = newSearcher(r, false);

  // Create a filter that defines "parent" documents in the index - in this case resumes
  BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("docType", "resume")));
  Query parentQuery = new PrefixQuery(new Term("country", "United"));

  ToChildBlockJoinQuery toChildQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter);

  TopDocs hits = s.search(toChildQuery, 10);
  assertEquals(hits.scoreDocs.length, 2);
  for (int i = 0; i < hits.scoreDocs.length; i++) {
    if (hits.scoreDocs[i].score == 0.0)
      fail("Failed to calculate score for hit #"+i);
  }

  r.close();
  dir.close();
}
 
Example 3
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testToChildBlockJoinQueryExplain() throws Exception {
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

  final List<Document> docs = new ArrayList<>();
  docs.add(makeJob("java", 2007));
  docs.add(makeJob("python", 2010));
  docs.add(makeResume("Lisa", "United Kingdom"));
  w.addDocuments(docs);

  docs.clear();
  docs.add(makeJob("java", 2006));
  docs.add(makeJob("ruby", 2005));
  docs.add(makeResume("Frank", "United States"));
  w.addDocuments(docs);
  w.deleteDocuments(new Term("skill", "java")); // delete the first child of every parent

  IndexReader r = w.getReader();
  w.close();
  IndexSearcher s = newSearcher(r, false);

  // Create a filter that defines "parent" documents in the index - in this case resumes
  BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("docType", "resume")));
  Query parentQuery = new PrefixQuery(new Term("country", "United"));

  ToChildBlockJoinQuery toChildQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter);

  TopDocs hits = s.search(toChildQuery, 10);
  assertEquals(hits.scoreDocs.length, 2);
  for (int i = 0; i < hits.scoreDocs.length; i++) {
    assertEquals(hits.scoreDocs[i].score, s.explain(toChildQuery, hits.scoreDocs[i].doc).getValue().doubleValue(), 0f);
  }

  r.close();
  dir.close();
}
 
Example 4
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testToChildInitialAdvanceParentButNoKids() throws Exception {

    final Directory dir = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

    // degenerate case: first doc has no children
    w.addDocument(makeResume("first", "nokids"));
    w.addDocuments(Arrays.asList(makeJob("job", 42), makeResume("second", "haskid")));

    // single segment
    w.forceMerge(1);

    final IndexReader r = w.getReader();
    final IndexSearcher s = newSearcher(r, false);
    w.close();

    BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term("docType", "resume")));
    Query parentQuery = new TermQuery(new Term("docType", "resume"));

    ToChildBlockJoinQuery parentJoinQuery = new ToChildBlockJoinQuery(parentQuery, parentFilter);

    Weight weight = s.createWeight(s.rewrite(parentJoinQuery), RandomPicks.randomFrom(random(), org.apache.lucene.search.ScoreMode.values()), 1);
    Scorer advancingScorer = weight.scorer(s.getIndexReader().leaves().get(0));
    Scorer nextDocScorer = weight.scorer(s.getIndexReader().leaves().get(0));

    final int firstKid = nextDocScorer.iterator().nextDoc();
    assertTrue("firstKid not found", DocIdSetIterator.NO_MORE_DOCS != firstKid);
    assertEquals(firstKid, advancingScorer.iterator().advance(0));

    r.close();
    dir.close();
  }
 
Example 5
Source File: TestCheckJoinIndex.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testInconsistentDeletes() throws IOException {
  final Directory dir = newDirectory();
  final IndexWriterConfig iwc = newIndexWriterConfig();
  iwc.setMergePolicy(NoMergePolicy.INSTANCE); // so that deletions don't trigger merges
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);

  List<Document> block = new ArrayList<>();
  final int numChildren = TestUtil.nextInt(random(), 1, 3);
  for (int i = 0; i < numChildren; ++i) {
    Document doc = new Document();
    doc.add(new StringField("child", Integer.toString(i), Store.NO));
    block.add(doc);
  }
  Document parent = new Document();
  parent.add(new StringField("parent", "true", Store.NO));
  block.add(parent);
  w.addDocuments(block);

  if (random().nextBoolean()) {
    w.deleteDocuments(new Term("parent", "true"));
  } else {
    // delete any of the children
    w.deleteDocuments(new Term("child", Integer.toString(random().nextInt(numChildren))));
  }

  final IndexReader reader = w.getReader();
  w.close();

  BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("parent", "true")));
  try {
    expectThrows(IllegalStateException.class, () -> CheckJoinIndex.check(reader, parentsFilter));
  } finally {
    reader.close();
    dir.close();
  }
}
 
Example 6
Source File: BlockGroupingTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void indexRandomDocs(RandomIndexWriter writer) throws IOException {
  int bookCount = atLeast(20);
  for (int i = 0; i < bookCount; i++) {
    writer.addDocuments(createRandomBlock(i));
  }
}
 
Example 7
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testBQShouldJoinedChild() throws Exception {
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

  final List<Document> docs = new ArrayList<>();

  docs.add(makeJob("java", 2007));
  docs.add(makeJob("python", 2010));
  docs.add(makeResume("Lisa", "United Kingdom"));
  w.addDocuments(docs);

  docs.clear();
  docs.add(makeJob("ruby", 2005));
  docs.add(makeJob("java", 2006));
  docs.add(makeResume("Frank", "United States"));
  w.addDocuments(docs);

  IndexReader r = w.getReader();
  w.close();
  IndexSearcher s = newSearcher(r, false);
  //IndexSearcher s = new IndexSearcher(r);

  // Create a filter that defines "parent" documents in the index - in this case resumes
  BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("docType", "resume")));
  CheckJoinIndex.check(r, parentsFilter);

  // Define child document criteria (finds an example of relevant work experience)
  BooleanQuery.Builder childQuery = new BooleanQuery.Builder();
  childQuery.add(new BooleanClause(new TermQuery(new Term("skill", "java")), Occur.MUST));
  childQuery.add(new BooleanClause(IntPoint.newRangeQuery("year", 2006, 2011), Occur.MUST));

  // Define parent document criteria (find a resident in the UK)
  Query parentQuery = new TermQuery(new Term("country", "United Kingdom"));

  // Wrap the child document query to 'join' any matches
  // up to corresponding parent:
  ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery.build(), parentsFilter, ScoreMode.Avg);

  // Combine the parent and nested child queries into a single query for a candidate
  BooleanQuery.Builder fullQuery = new BooleanQuery.Builder();
  fullQuery.add(new BooleanClause(parentQuery, Occur.SHOULD));
  fullQuery.add(new BooleanClause(childJoinQuery, Occur.SHOULD));

  final TopDocs topDocs = s.search(fullQuery.build(), 2);
  assertEquals(2, topDocs.totalHits.value);
  assertEquals(asSet("Lisa", "Frank"),
      asSet(s.doc(topDocs.scoreDocs[0].doc).get("name"), s.doc(topDocs.scoreDocs[1].doc).get("name")));

  ParentChildrenBlockJoinQuery childrenQuery =
      new ParentChildrenBlockJoinQuery(parentsFilter, childQuery.build(), topDocs.scoreDocs[0].doc);
  TopDocs matchingChildren = s.search(childrenQuery, 1);
  assertEquals(1, matchingChildren.totalHits.value);
  assertEquals("java", s.doc(matchingChildren.scoreDocs[0].doc).get("skill"));

  childrenQuery = new ParentChildrenBlockJoinQuery(parentsFilter, childQuery.build(), topDocs.scoreDocs[1].doc);
  matchingChildren = s.search(childrenQuery, 1);
  assertEquals(1, matchingChildren.totalHits.value);
  assertEquals("java", s.doc(matchingChildren.scoreDocs[0].doc).get("skill"));

  r.close();
  dir.close();
}
 
Example 8
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testSimple() throws Exception {

    final Directory dir = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

    final List<Document> docs = new ArrayList<>();

    docs.add(makeJob("java", 2007));
    docs.add(makeJob("python", 2010));
    docs.add(makeResume("Lisa", "United Kingdom"));
    w.addDocuments(docs);

    docs.clear();
    docs.add(makeJob("ruby", 2005));
    docs.add(makeJob("java", 2006));
    docs.add(makeResume("Frank", "United States"));
    w.addDocuments(docs);

    IndexReader r = w.getReader();
    w.close();
    IndexSearcher s = newSearcher(r, false);

    // Create a filter that defines "parent" documents in the index - in this case resumes
    BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("docType", "resume")));
    CheckJoinIndex.check(r, parentsFilter);

    // Define child document criteria (finds an example of relevant work experience)
    BooleanQuery.Builder childQuery = new BooleanQuery.Builder();
    childQuery.add(new BooleanClause(new TermQuery(new Term("skill", "java")), Occur.MUST));
    childQuery.add(new BooleanClause(IntPoint.newRangeQuery("year", 2006, 2011), Occur.MUST));

    // Define parent document criteria (find a resident in the UK)
    Query parentQuery = new TermQuery(new Term("country", "United Kingdom"));

    // Wrap the child document query to 'join' any matches
    // up to corresponding parent:
    ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery.build(), parentsFilter, ScoreMode.Avg);

    // Combine the parent and nested child queries into a single query for a candidate
    BooleanQuery.Builder fullQuery = new BooleanQuery.Builder();
    fullQuery.add(new BooleanClause(parentQuery, Occur.MUST));
    fullQuery.add(new BooleanClause(childJoinQuery, Occur.MUST));

    CheckHits.checkHitCollector(random(), fullQuery.build(), "country", s, new int[] {2});

    TopDocs topDocs = s.search(fullQuery.build(), 1);

    //assertEquals(1, results.totalHitCount);
    assertEquals(1, topDocs.totalHits.value);
    Document parentDoc = s.doc(topDocs.scoreDocs[0].doc);
    assertEquals("Lisa", parentDoc.get("name"));

    ParentChildrenBlockJoinQuery childrenQuery =
        new ParentChildrenBlockJoinQuery(parentsFilter, childQuery.build(), topDocs.scoreDocs[0].doc);
    TopDocs matchingChildren = s.search(childrenQuery, 1);
    assertEquals(1, matchingChildren.totalHits.value);
    assertEquals("java", s.doc(matchingChildren.scoreDocs[0].doc).get("skill"));


    //System.out.println("TEST: now test up");

    // Now join "up" (map parent hits to child docs) instead...:
    ToChildBlockJoinQuery parentJoinQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter);
    BooleanQuery.Builder fullChildQuery = new BooleanQuery.Builder();
    fullChildQuery.add(new BooleanClause(parentJoinQuery, Occur.MUST));
    fullChildQuery.add(new BooleanClause(childQuery.build(), Occur.MUST));

    //System.out.println("FULL: " + fullChildQuery);
    TopDocs hits = s.search(fullChildQuery.build(), 10);
    assertEquals(1, hits.totalHits.value);
    Document childDoc = s.doc(hits.scoreDocs[0].doc);
    //System.out.println("CHILD = " + childDoc + " docID=" + hits.scoreDocs[0].doc);
    assertEquals("java", childDoc.get("skill"));
    assertEquals(2007, childDoc.getField("year").numericValue());
    assertEquals("Lisa", getParentDoc(r, parentsFilter, hits.scoreDocs[0].doc).get("name"));

    // Test with filter on child docs:
    fullChildQuery.add(new TermQuery(new Term("skill", "foosball")), Occur.FILTER);
    assertEquals(0, s.count(fullChildQuery.build()));

    r.close();
    dir.close();
  }
 
Example 9
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testMultiChildTypes() throws Exception {

    final Directory dir = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

    final List<Document> docs = new ArrayList<>();

    docs.add(makeJob("java", 2007));
    docs.add(makeJob("python", 2010));
    docs.add(makeQualification("maths", 1999));
    docs.add(makeResume("Lisa", "United Kingdom"));
    w.addDocuments(docs);

    IndexReader r = w.getReader();
    w.close();
    IndexSearcher s = newSearcher(r, false);

    // Create a filter that defines "parent" documents in the index - in this case resumes
    BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("docType", "resume")));
    CheckJoinIndex.check(s.getIndexReader(), parentsFilter);

    // Define child document criteria (finds an example of relevant work experience)
    BooleanQuery.Builder childJobQuery = new BooleanQuery.Builder();
    childJobQuery.add(new BooleanClause(new TermQuery(new Term("skill", "java")), Occur.MUST));
    childJobQuery.add(new BooleanClause(IntPoint.newRangeQuery("year", 2006, 2011), Occur.MUST));

    BooleanQuery.Builder childQualificationQuery = new BooleanQuery.Builder();
    childQualificationQuery.add(new BooleanClause(new TermQuery(new Term("qualification", "maths")), Occur.MUST));
    childQualificationQuery.add(new BooleanClause(IntPoint.newRangeQuery("year", 1980, 2000), Occur.MUST));


    // Define parent document criteria (find a resident in the UK)
    Query parentQuery = new TermQuery(new Term("country", "United Kingdom"));

    // Wrap the child document query to 'join' any matches
    // up to corresponding parent:
    ToParentBlockJoinQuery childJobJoinQuery = new ToParentBlockJoinQuery(childJobQuery.build(), parentsFilter, ScoreMode.Avg);
    ToParentBlockJoinQuery childQualificationJoinQuery = new ToParentBlockJoinQuery(childQualificationQuery.build(), parentsFilter, ScoreMode.Avg);

    // Combine the parent and nested child queries into a single query for a candidate
    BooleanQuery.Builder fullQuery = new BooleanQuery.Builder();
    fullQuery.add(new BooleanClause(parentQuery, Occur.MUST));
    fullQuery.add(new BooleanClause(childJobJoinQuery, Occur.MUST));
    fullQuery.add(new BooleanClause(childQualificationJoinQuery, Occur.MUST));

    final TopDocs topDocs = s.search(fullQuery.build(), 10);
    assertEquals(1, topDocs.totalHits.value);
    Document parentDoc = s.doc(topDocs.scoreDocs[0].doc);
    assertEquals("Lisa", parentDoc.get("name"));

    ParentChildrenBlockJoinQuery childrenQuery =
        new ParentChildrenBlockJoinQuery(parentsFilter, childJobQuery.build(), topDocs.scoreDocs[0].doc);
    TopDocs matchingChildren = s.search(childrenQuery, 1);
    assertEquals(1, matchingChildren.totalHits.value);
    assertEquals("java", s.doc(matchingChildren.scoreDocs[0].doc).get("skill"));

    childrenQuery = new ParentChildrenBlockJoinQuery(parentsFilter, childQualificationQuery.build(), topDocs.scoreDocs[0].doc);
    matchingChildren = s.search(childrenQuery, 1);
    assertEquals(1, matchingChildren.totalHits.value);
    assertEquals("maths", s.doc(matchingChildren.scoreDocs[0].doc).get("qualification"));

    r.close();
    dir.close();
  }
 
Example 10
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testIntersectionWithRandomApproximation() throws IOException {
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);

  final int numBlocks = atLeast(100);
  for (int i = 0; i < numBlocks; ++i) {
    List<Document> docs = new ArrayList<>();
    final int numChildren = random().nextInt(3);
    for (int j = 0; j < numChildren; ++j) {
      Document child = new Document();
      child.add(new StringField("foo_child", random().nextBoolean() ? "bar" : "baz", Store.NO));
      docs.add(child);
    }
    Document parent = new Document();
    parent.add(new StringField("parent", "true", Store.NO));
    parent.add(new StringField("foo_parent", random().nextBoolean() ? "bar" : "baz", Store.NO));
    docs.add(parent);
    w.addDocuments(docs);
  }
  final IndexReader reader = w.getReader();
  final IndexSearcher searcher = newSearcher(reader);
  searcher.setQueryCache(null); // to have real advance() calls

  final BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("parent", "true")));
  final Query toChild = new ToChildBlockJoinQuery(new TermQuery(new Term("foo_parent", "bar")), parentsFilter);
  final Query childQuery = new TermQuery(new Term("foo_child", "baz"));

  BooleanQuery bq1 = new BooleanQuery.Builder()
      .add(toChild, Occur.MUST)
      .add(childQuery, Occur.MUST)
      .build();
  BooleanQuery bq2 = new BooleanQuery.Builder()
      .add(toChild, Occur.MUST)
      .add(new RandomApproximationQuery(childQuery, random()), Occur.MUST)
      .build();

  assertEquals(searcher.count(bq1), searcher.count(bq2));

  searcher.getIndexReader().close();
  w.close();
  dir.close();
}
 
Example 11
Source File: TestBlockJoin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testScoreMode() throws IOException {
  Similarity sim = new SimilarityBase() {

    @Override
    public String toString() {
      return "TestSim";
    }

    @Override
    protected double score(BasicStats stats, double freq, double docLen) {
      return freq;
    }
  };
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setSimilarity(sim));
  w.addDocuments(Arrays.asList(
      Collections.singleton(newTextField("foo", "bar bar", Store.NO)),
      Collections.singleton(newTextField("foo", "bar", Store.NO)),
      Collections.emptyList(),
      Collections.singleton(newStringField("type", new BytesRef("parent"), Store.NO))));
  DirectoryReader reader = w.getReader();
  w.close();
  IndexSearcher searcher = newSearcher(reader);
  searcher.setSimilarity(sim);
  BitSetProducer parents = new QueryBitSetProducer(new TermQuery(new Term("type", "parent")));
  for (ScoreMode scoreMode : ScoreMode.values()) {
    Query query = new ToParentBlockJoinQuery(new TermQuery(new Term("foo", "bar")), parents, scoreMode);
    TopDocs topDocs = searcher.search(query, 10);
    assertEquals(1, topDocs.totalHits.value);
    assertEquals(3, topDocs.scoreDocs[0].doc);
    float expectedScore;
    switch (scoreMode) {
      case Avg:
        expectedScore = 1.5f;
        break;
      case Max:
        expectedScore = 2f;
        break;
      case Min:
        expectedScore = 1f;
        break;
      case None:
        expectedScore = 0f;
        break;
      case Total:
        expectedScore = 3f;
        break;
      default:
        throw new AssertionError();
    }
    assertEquals(expectedScore, topDocs.scoreDocs[0].score, 0f);
  }
  reader.close();
  dir.close();
}
 
Example 12
Source File: TestParentChildrenBlockJoinQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testParentChildrenBlockJoinQuery() throws Exception {
  int numParentDocs = 8 + random().nextInt(8);
  int maxChildDocsPerParent = 8 + random().nextInt(8);

  Directory dir = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
  for (int i = 0; i < numParentDocs; i++) {
    int numChildDocs = random().nextInt(maxChildDocsPerParent);
    List<Document> docs = new ArrayList<>(numChildDocs + 1);
    for (int j = 0; j < numChildDocs; j++) {
      Document childDoc = new Document();
      childDoc.add(new StringField("type", "child", Field.Store.NO));
      childDoc.add(new NumericDocValuesField("score", j + 1));
      docs.add(childDoc);
    }

    Document parenDoc = new Document();
    parenDoc.add(new StringField("type", "parent", Field.Store.NO));
    parenDoc.add(new NumericDocValuesField("num_child_docs", numChildDocs));
    docs.add(parenDoc);
    writer.addDocuments(docs);
  }

  IndexReader reader = writer.getReader();
  writer.close();

  IndexSearcher searcher = newSearcher(reader);
  BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term("type", "parent")));
  Query childQuery = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("type", "child")), BooleanClause.Occur.FILTER)
      .add(TestJoinUtil.numericDocValuesScoreQuery("score"), BooleanClause.Occur.SHOULD)
      .build();

  TopDocs parentDocs = searcher.search(new TermQuery(new Term("type", "parent")), numParentDocs);
  assertEquals(parentDocs.scoreDocs.length, numParentDocs);
  for (ScoreDoc parentScoreDoc : parentDocs.scoreDocs) {
    LeafReaderContext leafReader = reader.leaves().get(ReaderUtil.subIndex(parentScoreDoc.doc, reader.leaves()));
    NumericDocValues numericDocValuesField = leafReader.reader().getNumericDocValues("num_child_docs");
    numericDocValuesField.advance(parentScoreDoc.doc - leafReader.docBase);
    long expectedChildDocs = numericDocValuesField.longValue();

    ParentChildrenBlockJoinQuery parentChildrenBlockJoinQuery =
        new ParentChildrenBlockJoinQuery(parentFilter, childQuery, parentScoreDoc.doc);
    TopDocs topDocs = searcher.search(parentChildrenBlockJoinQuery, maxChildDocsPerParent);
    assertEquals(expectedChildDocs, topDocs.totalHits.value);
    if (expectedChildDocs > 0) {
      for (int i = 0; i < topDocs.scoreDocs.length; i++) {
        ScoreDoc childScoreDoc = topDocs.scoreDocs[i];
        assertEquals(expectedChildDocs - i, childScoreDoc.score, 0);
      }
    }

  }

  reader.close();
  dir.close();
}
 
Example 13
Source File: TestPhraseWildcardQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected static void addDocs(RandomIndexWriter iw, Document... docs) throws IOException {
  iw.addDocuments(Arrays.asList(docs));
}
 
Example 14
Source File: TestLRUQueryCache.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testSkipCachingForRangeQuery() throws IOException {
  Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  Document doc1 = new Document();
  doc1.add(new StringField("name", "tom", Store.YES));
  doc1.add(new LongPoint("age", 15));
  doc1.add(new SortedNumericDocValuesField("age", 15));
  Document doc2 = new Document();
  doc2.add(new StringField("name", "alice", Store.YES));
  doc2.add(new LongPoint("age", 20));
  doc2.add(new SortedNumericDocValuesField("age", 20));
  w.addDocuments(Arrays.asList(doc1, doc2));
  final IndexReader reader = w.getReader();
  final IndexSearcher searcher = newSearcher(reader);
  searcher.setQueryCachingPolicy(ALWAYS_CACHE);
  w.close();

  // lead cost is 1, cost of subQuery1 is 1, cost of subQuery2 is 2
  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  TermQuery subQuery1 = new TermQuery(new Term("name", "tom"));
  IndexOrDocValuesQuery subQuery2 = new IndexOrDocValuesQuery(
      LongPoint.newRangeQuery("age", 10, 30),
      SortedNumericDocValuesField.newSlowRangeQuery("age", 10, 30));
  BooleanQuery query = bq.add(subQuery1, Occur.FILTER).add(subQuery2, Occur.FILTER).build();
  Set<Query> cacheSet = new HashSet<>();

  // only term query is cached
  final LRUQueryCache partCache = new LRUQueryCache(1000000, 10000000, context -> true, 1);
  searcher.setQueryCache(partCache);
  searcher.search(query, 1);
  cacheSet.add(subQuery1);
  assertEquals(cacheSet, new HashSet<>(partCache.cachedQueries()));

  // both queries are cached
  final LRUQueryCache allCache = new LRUQueryCache(1000000, 10000000, context -> true, Float.POSITIVE_INFINITY);
  searcher.setQueryCache(allCache);
  searcher.search(query, 1);
  cacheSet.add(subQuery2);
  assertEquals(cacheSet, new HashSet<>(allCache.cachedQueries()));

  reader.close();
  dir.close();
}
 
Example 15
Source File: TestLRUQueryCache.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testSkipCachingForTermQuery() throws IOException {
  Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  Document doc1 = new Document();
  doc1.add(new StringField("name", "tom", Store.YES));
  doc1.add(new StringField("hobby", "movie", Store.YES));
  Document doc2 = new Document();
  doc2.add(new StringField("name", "alice", Store.YES));
  doc2.add(new StringField("hobby", "book", Store.YES));
  Document doc3 = new Document();
  doc3.add(new StringField("name", "alice", Store.YES));
  doc3.add(new StringField("hobby", "movie", Store.YES));
  w.addDocuments(Arrays.asList(doc1, doc2, doc3));
  final IndexReader reader = w.getReader();
  final IndexSearcher searcher = newSearcher(reader);
  final UsageTrackingQueryCachingPolicy policy = new UsageTrackingQueryCachingPolicy();
  searcher.setQueryCachingPolicy(policy);
  w.close();

  // lead cost is 2, cost of subQuery1 is 3, cost of subQuery2 is 2
  BooleanQuery.Builder inner = new BooleanQuery.Builder();
  TermQuery innerSubQuery1 = new TermQuery(new Term("hobby", "book"));
  TermQuery innerSubQuery2 = new TermQuery(new Term("hobby", "movie"));
  BooleanQuery subQuery1 = inner.add(innerSubQuery1, Occur.SHOULD).add(innerSubQuery2, Occur.SHOULD).build();

  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  TermQuery subQuery2 = new TermQuery(new Term("name", "alice"));
  BooleanQuery query = bq.add(new ConstantScoreQuery(subQuery1), Occur.FILTER).add(subQuery2, Occur.FILTER).build();
  Set<Query> cacheSet = new HashSet<>();

  // both queries are not cached
  final LRUQueryCache partCache = new LRUQueryCache(1000000, 10000000, context -> true, 1);
  searcher.setQueryCache(partCache);
  searcher.search(query, 1);
  assertEquals(cacheSet, new HashSet<>(partCache.cachedQueries()));

  // only subQuery1 is cached
  final LRUQueryCache allCache = new LRUQueryCache(1000000, 10000000, context -> true, Float.POSITIVE_INFINITY);
  searcher.setQueryCache(allCache);
  searcher.search(query, 1);
  cacheSet.add(subQuery1);
  assertEquals(cacheSet, new HashSet<>(allCache.cachedQueries()));

  reader.close();
  dir.close();
}