1 package org.apache.lucene.analysis.query;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.io.StringReader;
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.analysis.MockTokenizer;
27 import org.apache.lucene.analysis.TokenStream;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.index.IndexReader;
31 import org.apache.lucene.index.IndexWriter;
32 import org.apache.lucene.index.IndexWriterConfig;
33 import org.apache.lucene.index.Term;
34 import org.apache.lucene.queryParser.ParseException;
35 import org.apache.lucene.queryParser.QueryParser;
36 import org.apache.lucene.search.IndexSearcher;
37 import org.apache.lucene.search.Query;
38 import org.apache.lucene.store.RAMDirectory;
40 public class QueryAutoStopWordAnalyzerTest extends BaseTokenStreamTestCase {
41 String variedFieldValues[] = {"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "boring", "dog"};
42 String repetitiveFieldValues[] = {"boring", "boring", "vaguelyboring"};
46 QueryAutoStopWordAnalyzer protectedAnalyzer;
49 public void setUp() throws Exception {
51 dir = new RAMDirectory();
52 appAnalyzer = new MockAnalyzer(random, MockTokenizer.WHITESPACE, false);
53 IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, appAnalyzer));
55 for (int i = 0; i < numDocs; i++) {
56 Document doc = new Document();
57 String variedFieldValue = variedFieldValues[i % variedFieldValues.length];
58 String repetitiveFieldValue = repetitiveFieldValues[i % repetitiveFieldValues.length];
59 doc.add(new Field("variedField", variedFieldValue, Field.Store.YES, Field.Index.ANALYZED));
60 doc.add(new Field("repetitiveField", repetitiveFieldValue, Field.Store.YES, Field.Index.ANALYZED));
61 writer.addDocument(doc);
64 reader = IndexReader.open(dir, true);
65 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer);
69 public void tearDown() throws Exception {
74 //Helper method to query
75 private int search(Analyzer a, String queryString) throws IOException, ParseException {
76 QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "repetitiveField", a);
77 Query q = qp.parse(queryString);
78 IndexSearcher searcher = newSearcher(reader);
79 int hits = searcher.search(q, null, 1000).totalHits;
84 public void testUninitializedAnalyzer() throws Exception {
85 //Note: no calls to "addStopWord"
86 String query = "variedField:quick repetitiveField:boring";
87 int numHits1 = search(protectedAnalyzer, query);
88 int numHits2 = search(appAnalyzer, query);
89 assertEquals("No filtering test", numHits1, numHits2);
93 * Test method for 'org.apache.lucene.analysis.QueryAutoStopWordAnalyzer.addStopWords(IndexReader)'
95 public void testDefaultAddStopWordsIndexReader() throws Exception {
96 protectedAnalyzer.addStopWords(reader);
97 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
98 assertEquals("Default filter should remove all docs", 0, numHits);
103 * Test method for 'org.apache.lucene.analysis.QueryAutoStopWordAnalyzer.addStopWords(IndexReader, int)'
105 public void testAddStopWordsIndexReaderInt() throws Exception {
106 protectedAnalyzer.addStopWords(reader, 1f / 2f);
107 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
108 assertEquals("A filter on terms in > one half of docs remove boring docs", 0, numHits);
110 numHits = search(protectedAnalyzer, "repetitiveField:vaguelyboring");
111 assertTrue("A filter on terms in > half of docs should not remove vaguelyBoring docs", numHits > 1);
113 protectedAnalyzer.addStopWords(reader, 1f / 4f);
114 numHits = search(protectedAnalyzer, "repetitiveField:vaguelyboring");
115 assertEquals("A filter on terms in > quarter of docs should remove vaguelyBoring docs", 0, numHits);
119 public void testAddStopWordsIndexReaderStringFloat() throws Exception {
120 protectedAnalyzer.addStopWords(reader, "variedField", 1f / 2f);
121 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
122 assertTrue("A filter on one Field should not affect queris on another", numHits > 0);
124 protectedAnalyzer.addStopWords(reader, "repetitiveField", 1f / 2f);
125 numHits = search(protectedAnalyzer, "repetitiveField:boring");
126 assertEquals("A filter on the right Field should affect queries on it", numHits, 0);
129 public void testAddStopWordsIndexReaderStringInt() throws Exception {
130 int numStopWords = protectedAnalyzer.addStopWords(reader, "repetitiveField", 10);
131 assertTrue("Should have identified stop words", numStopWords > 0);
133 Term[] t = protectedAnalyzer.getStopWords();
134 assertEquals("num terms should = num stopwords returned", t.length, numStopWords);
136 int numNewStopWords = protectedAnalyzer.addStopWords(reader, "variedField", 10);
137 assertTrue("Should have identified more stop words", numNewStopWords > 0);
138 t = protectedAnalyzer.getStopWords();
139 assertEquals("num terms should = num stopwords returned", t.length, numStopWords + numNewStopWords);
142 public void testNoFieldNamePollution() throws Exception {
143 protectedAnalyzer.addStopWords(reader, "repetitiveField", 10);
144 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
145 assertEquals("Check filter set up OK", 0, numHits);
147 numHits = search(protectedAnalyzer, "variedField:boring");
148 assertTrue("Filter should not prevent stopwords in one field being used in another ", numHits > 0);
153 * analyzer that does not support reuse
154 * it is LetterTokenizer on odd invocations, WhitespaceTokenizer on even.
156 private class NonreusableAnalyzer extends Analyzer {
157 int invocationCount = 0;
159 public TokenStream tokenStream(String fieldName, Reader reader) {
160 if (++invocationCount % 2 == 0)
161 return new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
163 return new MockTokenizer(reader, MockTokenizer.SIMPLE, false);
167 public void testWrappingNonReusableAnalyzer() throws Exception {
168 QueryAutoStopWordAnalyzer a = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, new NonreusableAnalyzer());
169 a.addStopWords(reader, 10);
170 int numHits = search(a, "repetitiveField:boring");
171 assertTrue(numHits == 0);
172 numHits = search(a, "repetitiveField:vaguelyboring");
173 assertTrue(numHits == 0);
176 public void testTokenStream() throws Exception {
177 QueryAutoStopWordAnalyzer a = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false));
178 a.addStopWords(reader, 10);
179 TokenStream ts = a.tokenStream("repetitiveField", new StringReader("this boring"));
180 assertTokenStreamContents(ts, new String[] { "this" });