Java Code Examples for org.apache.lucene.index.MultiReader

The following examples show how to use org.apache.lucene.index.MultiReader. These examples are extracted from open source projects. 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 Project: linden   Source File: ShardWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Process an intermediate form by carrying out, on the Lucene instance of
 * the shard, the deletes and the inserts (a ram index) in the form.
 * @param form  the intermediate form containing deletes and a ram index
 * @throws IOException
 */
public void process(IntermediateForm form, FacetsConfig facetsConfig) throws IOException {
  if (facetsConfig != null) {
    DirectoryTaxonomyWriter.OrdinalMap map = new DirectoryTaxonomyWriter.MemoryOrdinalMap();
    // merge the taxonomies
    taxoWriter.addTaxonomy(form.getTaxoDirectory(), map);
    int ordinalMap[] = map.getMap();
    DirectoryReader reader = DirectoryReader.open(form.getDirectory());
    try {
      List<AtomicReaderContext> leaves = reader.leaves();
      int numReaders = leaves.size();
      AtomicReader wrappedLeaves[] = new AtomicReader[numReaders];
      for (int i = 0; i < numReaders; i++) {
        wrappedLeaves[i] = new OrdinalMappingAtomicReader(leaves.get(i).reader(), ordinalMap, facetsConfig);
      }
      writer.addIndexes(new MultiReader(wrappedLeaves));
    } finally {
      reader.close();
    }
  } else {
    writer.addIndexes(new Directory[] { form.getDirectory() });
  }
  numForms++;
}
 
Example 2
Source Project: lucene-solr   Source File: TestBM25FQuery.java    License: Apache License 2.0 6 votes vote down vote up
public void testRewrite() throws IOException {
  BM25FQuery.Builder builder = new BM25FQuery.Builder();
  IndexReader reader = new MultiReader();
  IndexSearcher searcher = new IndexSearcher(reader);
  Query actual = searcher.rewrite(builder.build());
  assertEquals(actual, new MatchNoDocsQuery());
  builder.addField("field", 1f);
  actual = searcher.rewrite(builder.build());
  assertEquals(actual, new MatchNoDocsQuery());
  builder.addTerm(new BytesRef("foo"));
  actual = searcher.rewrite(builder.build());
  assertEquals(actual, new TermQuery(new Term("field", "foo")));
  builder.addTerm(new BytesRef("bar"));
  actual = searcher.rewrite(builder.build());
  assertEquals(actual, new SynonymQuery.Builder("field")
      .addTerm(new Term("field", "foo"))
      .addTerm(new Term("field", "bar"))
      .build());
  builder.addField("another_field", 1f);
  Query query = builder.build();
  actual = searcher.rewrite(query);
  assertEquals(actual, query);
}
 
Example 3
Source Project: lucene-solr   Source File: QueryUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Given an IndexSearcher, returns a new IndexSearcher whose IndexReader
 * is a MultiReader containing the Reader of the original IndexSearcher,
 * as well as several "empty" IndexReaders -- some of which will have
 * deleted documents in them.  This new IndexSearcher should
 * behave exactly the same as the original IndexSearcher.
 * @param s the searcher to wrap
 * @param edge if negative, s will be the first sub; if 0, s will be in the middle, if positive s will be the last sub
 */
public static IndexSearcher wrapUnderlyingReader(Random random, final IndexSearcher s, final int edge)
  throws IOException {

  IndexReader r = s.getIndexReader();

  // we can't put deleted docs before the nested reader, because
  // it will throw off the docIds
  IndexReader[] readers = new IndexReader[] {
    edge < 0 ? r : new MultiReader(),
    new MultiReader(),
    new MultiReader(edge < 0 ? emptyReader(4) : new MultiReader(),
        new MultiReader(),
        0 == edge ? r : new MultiReader()),
    0 < edge ? new MultiReader() : emptyReader(7),
    new MultiReader(),
    new MultiReader(0 < edge ? new MultiReader() : emptyReader(5),
        new MultiReader(),
        0 < edge ? r : new MultiReader())
  };

  IndexSearcher out = LuceneTestCase.newSearcher(new MultiReader(readers));
  out.setSimilarity(s.getSimilarity());
  return out;
}
 
Example 4
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldTurnAnalysisOn() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("smembers");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.getBool("useAnalyzer", false)).thenReturn(true);
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new WhitespaceAnalyzer());
  when(jedisMock.smembers(anyString())).thenReturn(new HashSet<>(Arrays.asList("123 124", "321")));
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).smembers("simpleKey");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(3, terms.size());
}
 
Example 5
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 6 votes vote down vote up
public void testSingleMustMatchAllWithShouldClauses() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  BooleanQuery bq = new BooleanQuery.Builder()
      .add(new MatchAllDocsQuery(), Occur.MUST)
      .add(new TermQuery(new Term("foo", "bar")), Occur.FILTER)
      .add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "quux")), Occur.SHOULD)
      .build();
  BooleanQuery expected = new BooleanQuery.Builder()
      .add(new ConstantScoreQuery(new TermQuery(new Term("foo", "bar"))), Occur.MUST)
      .add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "quux")), Occur.SHOULD)
      .build();
  assertEquals(expected, searcher.rewrite(bq));
}
 
Example 6
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 6 votes vote down vote up
public void testDeduplicateMustAndFilter() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  BooleanQuery bq = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "bar")), Occur.FILTER)
      .build();
  assertEquals(new TermQuery(new Term("foo", "bar")), searcher.rewrite(bq));

  bq = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "bar")), Occur.FILTER)
      .add(new TermQuery(new Term("foo", "baz")), Occur.FILTER)
      .build();
  BooleanQuery expected = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "baz")), Occur.FILTER)
      .build();
  assertEquals(expected, searcher.rewrite(bq));
}
 
Example 7
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 6 votes vote down vote up
public void testMatchAllMustNot() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  // Test Must with MatchAll MustNot
  BooleanQuery bq = new BooleanQuery.Builder()
          .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
          .add(new TermQuery(new Term("foo", "baz")), Occur.FILTER)
          .add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD)
          //
          .add(new MatchAllDocsQuery(), Occur.MUST_NOT)
          .build();

  assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq));

  // Test Must with MatchAll MustNot and other MustNot
  BooleanQuery bq2 = new BooleanQuery.Builder()
          .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
          .add(new TermQuery(new Term("foo", "baz")), Occur.FILTER)
          .add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD)
          //
          .add(new TermQuery(new Term("foo", "bor")), Occur.MUST_NOT)
          .add(new MatchAllDocsQuery(), Occur.MUST_NOT)
          .build();

  assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq2));
}
 
Example 8
Source Project: lucene-solr   Source File: TestBooleanRewrites.java    License: Apache License 2.0 6 votes vote down vote up
public void testRemoveMatchAllFilter() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  BooleanQuery bq = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new MatchAllDocsQuery(), Occur.FILTER)
      .build();
  assertEquals(new TermQuery(new Term("foo", "bar")), searcher.rewrite(bq));

  bq = new BooleanQuery.Builder()
      .setMinimumNumberShouldMatch(random().nextInt(5))
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "baz")), Occur.MUST)
      .add(new MatchAllDocsQuery(), Occur.FILTER)
      .build();
  BooleanQuery expected = new BooleanQuery.Builder()
      .setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch())
      .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
      .add(new TermQuery(new Term("foo", "baz")), Occur.MUST)
      .build();
  assertEquals(expected, searcher.rewrite(bq));
}
 
Example 9
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromRedisOnZrevrangeCommandWithCustomRange() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("zrevrange");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("range_start")).thenReturn("1");
  when(localParamsMock.get("range_end")).thenReturn("100");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.zrevrangeWithScores(anyString(), anyLong(), anyLong()))
      .thenReturn(new HashSet<>(Arrays.asList(new Tuple("123", (double) 1.0f), new Tuple("321", (double) 1.0f))));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).zrevrangeWithScores("simpleKey", 1, 100);
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 10
Source Project: lucene-solr   Source File: TestIndexSearcher.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetQueryCache() throws IOException {
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  assertEquals(IndexSearcher.getDefaultQueryCache(), searcher.getQueryCache());
  QueryCache dummyCache = new QueryCache() {
    @Override
    public Weight doCache(Weight weight, QueryCachingPolicy policy) {
      return weight;
    }
  };
  searcher.setQueryCache(dummyCache);
  assertEquals(dummyCache, searcher.getQueryCache());

  IndexSearcher.setDefaultQueryCache(dummyCache);
  searcher = new IndexSearcher(new MultiReader());
  assertEquals(dummyCache, searcher.getQueryCache());

  searcher.setQueryCache(null);
  assertNull(searcher.getQueryCache());

  IndexSearcher.setDefaultQueryCache(null);
  searcher = new IndexSearcher(new MultiReader());
  assertNull(searcher.getQueryCache());
}
 
Example 11
Source Project: lucene-solr   Source File: TestIndexSearcher.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetSlices() throws Exception {
  assertNull(new IndexSearcher(new MultiReader()).getSlices());

  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  w.addDocument(new Document());
  IndexReader r = w.getReader();
  w.close();

  ExecutorService service = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS,
                                 new LinkedBlockingQueue<Runnable>(),
                                 new NamedThreadFactory("TestIndexSearcher"));
  IndexSearcher s = new IndexSearcher(r, service);
  IndexSearcher.LeafSlice[] slices = s.getSlices();
  assertNotNull(slices);
  assertEquals(1, slices.length);
  assertEquals(1, slices[0].leaves.length);
  assertTrue(slices[0].leaves[0] == r.leaves().get(0));
  service.shutdown();
  IOUtils.close(r, dir);
}
 
Example 12
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromSortOffset() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sort");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("offset")).thenReturn("100");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321"));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class);
  verify(jedisMock).sort(eq("simpleKey"), argument.capture());
  Assert.assertEquals(getSortingParamString(new SortingParams().limit(100, 0)),
      getSortingParamString(argument.getValue()));
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 13
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromSortWithSingleGetClause() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sort");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("get")).thenReturn("get_*");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "get").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321"));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class);
  verify(jedisMock).sort(eq("simpleKey"), argument.capture());
  Assert.assertEquals(getSortingParamString(new SortingParams().get("get_*")),
      getSortingParamString(argument.getValue()));
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 14
Source Project: lucene-solr   Source File: TestMaxClauseLimit.java    License: Apache License 2.0 6 votes vote down vote up
public void testFlattenInnerDisjunctionsWithMoreThan1024Terms() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());

  BooleanQuery.Builder builder1024 = new BooleanQuery.Builder();
  for(int i = 0; i < 1024; i++) {
    builder1024.add(new TermQuery(new Term("foo", "bar-" + i)), BooleanClause.Occur.SHOULD);
  }
  Query inner = builder1024.build();
  Query query = new BooleanQuery.Builder()
      .add(inner, BooleanClause.Occur.SHOULD)
      .add(new TermQuery(new Term("foo", "baz")), BooleanClause.Occur.SHOULD)
      .build();

  expectThrows(IndexSearcher.TooManyClauses.class, () -> {
    searcher.rewrite(query);
  });
}
 
Example 15
Source Project: lucene-solr   Source File: TestMaxClauseLimit.java    License: Apache License 2.0 6 votes vote down vote up
public void testLargeTermsNestedFirst() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());
  BooleanQuery.Builder nestedBuilder = new BooleanQuery.Builder();

  nestedBuilder.setMinimumNumberShouldMatch(5);

  for(int i = 0; i < 600; i++) {
    nestedBuilder.add(new TermQuery(new Term("foo", "bar-" + i)), BooleanClause.Occur.SHOULD);
  }
  Query inner = nestedBuilder.build();
  BooleanQuery.Builder builderMixed = new BooleanQuery.Builder()
      .add(inner, BooleanClause.Occur.SHOULD);

  builderMixed.setMinimumNumberShouldMatch(5);

  for (int i = 0; i < 600; i++) {
    builderMixed.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
  }

  Query query = builderMixed.build();

  expectThrows(IndexSearcher.TooManyClauses.class, () -> {
    searcher.rewrite(query);
  });
}
 
Example 16
Source Project: lucene-solr   Source File: TestMaxClauseLimit.java    License: Apache License 2.0 6 votes vote down vote up
public void testLargeTermsNestedLast() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());
  BooleanQuery.Builder nestedBuilder = new BooleanQuery.Builder();

  nestedBuilder.setMinimumNumberShouldMatch(5);

  for(int i = 0; i < 600; i++) {
    nestedBuilder.add(new TermQuery(new Term("foo", "bar-" + i)), BooleanClause.Occur.SHOULD);
  }
  Query inner = nestedBuilder.build();
  BooleanQuery.Builder builderMixed = new BooleanQuery.Builder();

  builderMixed.setMinimumNumberShouldMatch(5);

  for (int i = 0; i < 600; i++) {
    builderMixed.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
  }

  builderMixed.add(inner, BooleanClause.Occur.SHOULD);

  Query query = builderMixed.build();

  expectThrows(IndexSearcher.TooManyClauses.class, () -> {
    searcher.rewrite(query);
  });
}
 
Example 17
Source Project: lucene-solr   Source File: TestMaxClauseLimit.java    License: Apache License 2.0 6 votes vote down vote up
public void testLargeDisjunctionMaxQuery() throws IOException {
  IndexSearcher searcher = newSearcher(new MultiReader());
  Query[] clausesQueryArray = new Query[1050];

  for(int i = 0; i < 1049; i++) {
    clausesQueryArray[i] = new TermQuery(new Term("field", "a"));
  }

  PhraseQuery pq = new PhraseQuery("field", new String[0]);

  clausesQueryArray[1049] = pq;

  DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(
      Arrays.asList(clausesQueryArray),
      0.5f);

  expectThrows(IndexSearcher.TooManyClauses.class, () -> {
    searcher.rewrite(dmq);
  });
}
 
Example 18
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromRedisOnZrevrangeCommandWithDefaultParams() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("zrevrange");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.zrevrangeWithScores(anyString(), anyLong(), anyLong()))
      .thenReturn(new HashSet<>(Arrays.asList(new Tuple("123", (double) 1.0f), new Tuple("321", (double) 1.0f))));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).zrevrangeWithScores("simpleKey", 0, -1);
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 19
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddMultipleTermsFromRedisOnSrandmemberCommandWithExplicitCount() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("srandmember");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("count")).thenReturn("2");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.srandmember(anyString(), anyInt())).thenReturn(Arrays.asList("123", "321"));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).srandmember("simpleKey", 2);
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 20
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromSortLimitAndOffset() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sort");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("offset")).thenReturn("100");
  when(localParamsMock.get("limit")).thenReturn("1000");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321"));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class);
  verify(jedisMock).sort(eq("simpleKey"), argument.capture());
  Assert.assertEquals(getSortingParamString(new SortingParams().limit(100, 1000)),
      getSortingParamString(argument.getValue()));
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 21
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromRedisOnSinterCommand() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sinter");
  when(localParamsMock.get("key")).thenReturn("key1");
  when(localParamsMock.get("key1")).thenReturn("key2");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "key1").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sinter(anyString(), anyString())).thenReturn(new HashSet<>(Arrays.asList("123", "321")));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).sinter("key1", "key2");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 22
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnEmptyQueryOnEmptyListOfSinter() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sinter");
  when(localParamsMock.get("key")).thenReturn("key1");
  when(localParamsMock.get("key1")).thenReturn("key2");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "key1").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sinter(anyString(), anyString())).thenReturn(new HashSet<String>());
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).sinter("key1", "key2");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(0, terms.size());
}
 
Example 23
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromRedisOnSdiffCommand() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sdiff");
  when(localParamsMock.get("key")).thenReturn("key1");
  when(localParamsMock.get("key1")).thenReturn("key2");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "key1").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sdiff(anyString(), anyString())).thenReturn(new HashSet<>(Arrays.asList("123", "321")));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).sdiff("key1", "key2");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 24
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnEmptyQueryOnEmptyListOfSdiff() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sdiff");
  when(localParamsMock.get("key")).thenReturn("key1");
  when(localParamsMock.get("key1")).thenReturn("key2");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "key1").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sdiff(anyString(), anyString())).thenReturn(new HashSet<String>());
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).sdiff("key1", "key2");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(0, terms.size());
}
 
Example 25
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromRedisOnSunionCommand() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sunion");
  when(localParamsMock.get("key")).thenReturn("key1");
  when(localParamsMock.get("key1")).thenReturn("key2");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "key1").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sunion(anyString(), anyString())).thenReturn(new HashSet<>(Arrays.asList("123", "321")));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).sunion("key1", "key2");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 26
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnEmptyQueryOnEmptyListOfSunion() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sunion");
  when(localParamsMock.get("key")).thenReturn("key1");
  when(localParamsMock.get("key1")).thenReturn("key2");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "key1").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sunion(anyString(), anyString())).thenReturn(new HashSet<String>());
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).sunion("key1", "key2");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(0, terms.size());
}
 
Example 27
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromRedisOnHgetCommand() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("hget");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("field")).thenReturn("f1");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.hget(anyString(), anyString())).thenReturn("123");
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).hget("simpleKey", "f1");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(1, terms.size());
}
 
Example 28
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnEmptyQueryOnEmptyListOfHget() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("hget");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("field")).thenReturn("f1");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.hget(anyString(), anyString())).thenReturn(null);
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).hget("simpleKey", "f1");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(0, terms.size());
}
 
Example 29
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromRedisOnZrangebyscoreCommandWithDefaultParams() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("zrangebyscore");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.zrangeByScoreWithScores(anyString(), anyString(), anyString()))
      .thenReturn(new HashSet<>(Arrays.asList(new Tuple("123", (double) 1.0f), new Tuple("321", (double) 1.0f))));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).zrangeByScoreWithScores("simpleKey", "-inf", "+inf");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example 30
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnEmptyQueryOnEmptyListOfHmget() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("hmget");
  when(localParamsMock.get("key")).thenReturn("hash");
  when(localParamsMock.get("field")).thenReturn("field1");
  when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "field").iterator());
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.hmget(anyString(), anyString())).thenReturn(new ArrayList<String>());
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).hmget("hash", "field1");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(0, terms.size());
}