public void testPostFilterDisablesCountOptimization() throws Exception { TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0); context.setTask(new SearchTask(123L, "", "", "", null)); final AtomicBoolean collected = new AtomicBoolean(); IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) { protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException { collected.set(true); super.search(leaves, weight, collector); } }; QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertFalse(collected.get()); context.parsedPostFilter(new ParsedQuery(new MatchNoDocsQuery())); QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertTrue(collected.get()); }
public void testMinScoreDisablesCountOptimization() throws Exception { TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0); context.setTask(new SearchTask(123L, "", "", "", null)); final AtomicBoolean collected = new AtomicBoolean(); IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) { protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException { collected.set(true); super.search(leaves, weight, collector); } }; QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertFalse(collected.get()); context.minimumScore(1); QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertTrue(collected.get()); }
public void testRewriteEmptyReader() throws Exception { IndexService indexService = createIndex("test"); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties") .startObject("foo") .field("type", "date") .endObject() .endObject() .endObject().endObject().string(); indexService.mapperService().merge("type", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE, false); IndexReader reader = new MultiReader(); QueryRewriteContext context = new QueryShardContext(0, indexService.getIndexSettings(), null, null, indexService.mapperService(), null, null, xContentRegistry(), null, reader, null); RangeQueryBuilder range = new RangeQueryBuilder("foo"); // no values -> DISJOINT assertEquals(Relation.DISJOINT, range.getRelation(context)); }
public void testRangeQuery() throws IOException { Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build(); QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis); MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); String date1 = "2015-10-12T14:10:55"; String date2 = "2016-04-28T11:33:52"; long instant1 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date1).getMillis(); long instant2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime(date2).getMillis() + 999; ft.setIndexOptions(IndexOptions.DOCS); Query expected = new IndexOrDocValuesQuery( LongPoint.newRangeQuery("field", instant1, instant2), SortedNumericDocValuesField.newRangeQuery("field", instant1, instant2)); assertEquals(expected, ft.rangeQuery(date1, date2, true, true, context).rewrite(new MultiReader())); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery(date1, date2, true, true, context)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); }
/** * 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++; }
@Override public List<AtomicReader> getMergeReaders() throws IOException { if (unsortedReaders == null) { unsortedReaders = super.getMergeReaders(); final AtomicReader atomicView; if (unsortedReaders.size() == 1) { atomicView = unsortedReaders.get(0); } else { final IndexReader multiReader = new MultiReader(unsortedReaders.toArray(new AtomicReader[unsortedReaders.size()])); atomicView = SlowCompositeReaderWrapper.wrap(multiReader); } docMap = sorter.sort(atomicView); sortedView = SortingAtomicReader.wrap(atomicView, docMap); } // a null doc map means that the readers are already sorted return docMap == null ? unsortedReaders : Collections.singletonList(sortedView); }
/** * Merges the given taxonomy and index directories and commits the changes to * the given writers. */ public static void merge(Directory srcIndexDir, Directory srcTaxoDir, OrdinalMap map, IndexWriter destIndexWriter, DirectoryTaxonomyWriter destTaxoWriter, FacetsConfig srcConfig) throws IOException { // merge the taxonomies destTaxoWriter.addTaxonomy(srcTaxoDir, map); int ordinalMap[] = map.getMap(); DirectoryReader reader = DirectoryReader.open(srcIndexDir); 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, srcConfig); } destIndexWriter.addIndexes(new MultiReader(wrappedLeaves)); // commit changes to taxonomy and index respectively. destTaxoWriter.commit(); destIndexWriter.commit(); } finally { reader.close(); } }
public void testEmptyIndex() throws Exception { IndexSearcher empty = newSearcher(new MultiReader()); Query query = new TermQuery(new Term("contents", "foo")); Sort sort = new Sort(); sort.setSort(new SortedSetSortField("sortedset", false)); TopDocs td = empty.search(query, null, 10, sort, true, true); assertEquals(0, td.totalHits); // for an empty index, any selector should work for (SortedSetSortField.Selector v : SortedSetSortField.Selector.values()) { sort.setSort(new SortedSetSortField("sortedset", false, v)); td = empty.search(query, null, 10, sort, true, true); assertEquals(0, td.totalHits); } }
public void testEmptyIndex() throws Exception { IndexSearcher empty = newSearcher(new MultiReader()); Query query = new TermQuery(new Term("contents", "foo")); Sort sort = new Sort(); sort.setSort(new SortedNumericSortField("sortednumeric", SortField.Type.LONG)); TopDocs td = empty.search(query, null, 10, sort, true, true); assertEquals(0, td.totalHits); // for an empty index, any selector should work for (SortedNumericSelector.Type v : SortedNumericSelector.Type.values()) { sort.setSort(new SortedNumericSortField("sortednumeric", SortField.Type.LONG, false, v)); td = empty.search(query, null, 10, sort, true, true); assertEquals(0, td.totalHits); } }
public IndexSearcher getIndexSearcher(final ClusterSegment... clusterSegments) { final Set<KCluster> clusters; if (clusterSegments == null || clusterSegments.length == 0) { clusters = new HashSet<KCluster>(indexes.keySet()); } else { clusters = new HashSet<KCluster>(clusterSegments.length); for (final ClusterSegment clusterSegment : clusterSegments) { clusters.add(new KClusterImpl(clusterSegment.getClusterId())); } } final Collection<IndexReader> readers = new ArrayList<IndexReader>(clusters.size()); for (final KCluster cluster : clusters) { final LuceneIndex index = indexes.get(cluster); readers.add(index.nrtReader()); } try { return new SearcherFactory().newSearcher(new MultiReader(readers.toArray(new IndexReader[readers.size()])), null); } catch (IOException e) { throw new RuntimeException(e); } }
@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()); }
@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()); }
@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()); }
@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()); }
@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()); }
@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()); }
@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()); }
@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()); }
@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()); }
@Test public void shouldAddTermsFromRedisOnHmgetCommand() 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(Arrays.asList("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).hmget("hash", "field1"); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(1, terms.size()); }
@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()); }
@Test public void shouldParseJsonTermsFromRedisOnGetCommand() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("get"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("serialization")).thenReturn("json"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.get(any(byte[].class))).thenReturn("[1,2,3]".getBytes()); 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).get("simpleKey".getBytes()); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(3, terms.size()); }
@Test public void shouldDeflateGzipTermsFromRedisOnGetCommand() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("get"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("compression")).thenReturn("gzip"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.get(any(byte[].class))).thenReturn(Compressor.compressGzip("1".getBytes())); 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).get("simpleKey".getBytes()); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(1, terms.size()); }
@Test public void shouldDeflateGzipAndParseJsonTermsFromRedisOnGetCommand() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("get"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("compression")).thenReturn("gzip"); when(localParamsMock.get("serialization")).thenReturn("json"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.get(any(byte[].class))).thenReturn(Compressor.compressGzip("[100,200,300]".getBytes())); 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).get("simpleKey".getBytes()); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(3, terms.size()); }
@Test public void shouldAddTermsFromRedisOnLindexCommand() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lindex"); when(localParamsMock.get("index")).thenReturn("10"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lindex(anyString(), anyLong())).thenReturn("value"); 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).lindex("simpleKey", 10); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(1, terms.size()); }
@Test public void shouldReturnEmptyQueryOnEmptyResultOfLindex() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lindex"); when(localParamsMock.get("index")).thenReturn("10"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lindex(anyString(), anyLong())).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).lindex("simpleKey", 10); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(0, terms.size()); }
@Test public void shouldAddTermsFromRedisOnMgetCommand() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("mget"); 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.mget(anyString(), anyString())).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).mget("key1", "key2"); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldReturnEmptyQueryOnEmptyListOfMget() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("mget"); 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.mget(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).mget("key1", "key2"); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(0, terms.size()); }
@Test public void shouldAddTermsFromRedisOnLrangeCommandCustomMin() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lrange"); when(localParamsMock.get("min")).thenReturn("-1"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lrange(anyString(), anyLong(), anyLong())).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).lrange("simpleKey", -1, -1); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromRedisOnLrangeCommandCustomMax() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lrange"); when(localParamsMock.get("max")).thenReturn("1"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lrange(anyString(), anyLong(), anyLong())).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).lrange("simpleKey", 0, 1); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromRedisOnLrangeCommandCustomMinAndMax() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lrange"); when(localParamsMock.get("min")).thenReturn("2"); when(localParamsMock.get("max")).thenReturn("3"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lrange(anyString(), anyLong(), anyLong())).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).lrange("simpleKey", 2, 3); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromRedisOnLrangeCommandInvalidMinAndMaxFallsBackToDefault() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lrange"); when(localParamsMock.get("min")).thenReturn("-foo"); when(localParamsMock.get("min")).thenReturn("-bar"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lrange(anyString(), anyLong(), anyLong())).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).lrange("simpleKey", 0, -1); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromRedisOnLrangeCommandEmptyStringMinAndMaxFallsBackToDefault() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lrange"); when(localParamsMock.get("min")).thenReturn(" "); when(localParamsMock.get("min")).thenReturn(" "); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lrange(anyString(), anyLong(), anyLong())).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).lrange("simpleKey", 0, -1); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromRedisOnLrangeCommandEmptyMinAndMaxFallsBackToDefault() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("lrange"); when(localParamsMock.get("min")).thenReturn(""); when(localParamsMock.get("max")).thenReturn(""); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.lrange(anyString(), anyLong(), anyLong())).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).lrange("simpleKey", 0, -1); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldReturnEmptyQueryOnEmptyResultOfSort() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).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(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(0, terms.size()); }
@Test public void shouldAddTermsFromSort() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); 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()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortAlgorithmAlpha() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("algorithm")).thenReturn("alpha"); 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().alpha()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortOrderAsc() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("order")).thenReturn("asc"); 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().asc()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortOrderDesc() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("order")).thenReturn("desc"); 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().desc()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortLimit() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("limit")).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(0, 100)), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }