1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.util.LuceneTestCase;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.store.Directory;
28 import java.util.ArrayList;
29 import java.util.List;
32 * A basic unit test for FieldCacheTermsFilter
34 * @see org.apache.lucene.search.FieldCacheTermsFilter
36 public class TestFieldCacheTermsFilter extends LuceneTestCase {
37 public void testMissingTerms() throws Exception {
38 String fieldName = "field1";
39 Directory rd = newDirectory();
40 RandomIndexWriter w = new RandomIndexWriter(random, rd);
41 for (int i = 0; i < 100; i++) {
42 Document doc = new Document();
43 int term = i * 10; //terms are units of 10;
44 doc.add(newField(fieldName, "" + term, Field.Store.YES, Field.Index.NOT_ANALYZED));
47 IndexReader reader = w.getReader();
50 IndexSearcher searcher = newSearcher(reader);
51 int numDocs = reader.numDocs();
53 MatchAllDocsQuery q = new MatchAllDocsQuery();
55 List<String> terms = new ArrayList<String>();
57 results = searcher.search(q, new FieldCacheTermsFilter(fieldName, terms.toArray(new String[0])), numDocs).scoreDocs;
58 assertEquals("Must match nothing", 0, results.length);
60 terms = new ArrayList<String>();
62 results = searcher.search(q, new FieldCacheTermsFilter(fieldName, terms.toArray(new String[0])), numDocs).scoreDocs;
63 assertEquals("Must match 1", 1, results.length);
65 terms = new ArrayList<String>();
68 results = searcher.search(q, new FieldCacheTermsFilter(fieldName, terms.toArray(new String[0])), numDocs).scoreDocs;
69 assertEquals("Must match 2", 2, results.length);