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;
22 import java.util.Arrays;
23 import java.util.Collections;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
27 import org.apache.lucene.analysis.MockAnalyzer;
28 import org.apache.lucene.analysis.MockTokenizer;
29 import org.apache.lucene.analysis.TokenStream;
30 import org.apache.lucene.document.Document;
31 import org.apache.lucene.document.Field;
32 import org.apache.lucene.index.IndexReader;
33 import org.apache.lucene.index.IndexWriter;
34 import org.apache.lucene.index.IndexWriterConfig;
35 import org.apache.lucene.index.Term;
36 import org.apache.lucene.queryParser.ParseException;
37 import org.apache.lucene.queryParser.QueryParser;
38 import org.apache.lucene.search.IndexSearcher;
39 import org.apache.lucene.search.Query;
40 import org.apache.lucene.store.RAMDirectory;
42 public class QueryAutoStopWordAnalyzerTest extends BaseTokenStreamTestCase {
43 String variedFieldValues[] = {"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "boring", "dog"};
44 String repetitiveFieldValues[] = {"boring", "boring", "vaguelyboring"};
48 QueryAutoStopWordAnalyzer protectedAnalyzer;
51 public void setUp() throws Exception {
53 dir = new RAMDirectory();
54 appAnalyzer = new MockAnalyzer(random, MockTokenizer.WHITESPACE, false);
55 IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, appAnalyzer));
57 for (int i = 0; i < numDocs; i++) {
58 Document doc = new Document();
59 String variedFieldValue = variedFieldValues[i % variedFieldValues.length];
60 String repetitiveFieldValue = repetitiveFieldValues[i % repetitiveFieldValues.length];
61 doc.add(new Field("variedField", variedFieldValue, Field.Store.YES, Field.Index.ANALYZED));
62 doc.add(new Field("repetitiveField", repetitiveFieldValue, Field.Store.YES, Field.Index.ANALYZED));
63 writer.addDocument(doc);
66 reader = IndexReader.open(dir, true);
70 public void tearDown() throws Exception {
75 //Helper method to query
76 private int search(Analyzer a, String queryString) throws IOException, ParseException {
77 QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "repetitiveField", a);
78 Query q = qp.parse(queryString);
79 IndexSearcher searcher = newSearcher(reader);
80 int hits = searcher.search(q, null, 1000).totalHits;
85 public void testNoStopwords() throws Exception {
86 // Note: an empty list of fields passed in
87 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, Collections.<String>emptyList(), 1);
88 String query = "variedField:quick repetitiveField:boring";
89 int numHits1 = search(protectedAnalyzer, query);
90 int numHits2 = search(appAnalyzer, query);
91 assertEquals("No filtering test", numHits1, numHits2);
94 public void testDefaultStopwordsAllFields() throws Exception {
95 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader);
96 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
97 assertEquals("Default filter should remove all docs", 0, numHits);
100 public void testStopwordsAllFieldsMaxPercentDocs() throws Exception {
101 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, 1f / 2f);
102 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
103 assertEquals("A filter on terms in > one half of docs remove boring docs", 0, numHits);
105 numHits = search(protectedAnalyzer, "repetitiveField:vaguelyboring");
106 assertTrue("A filter on terms in > half of docs should not remove vaguelyBoring docs", numHits > 1);
108 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, 1f / 4f);
109 numHits = search(protectedAnalyzer, "repetitiveField:vaguelyboring");
110 assertEquals("A filter on terms in > quarter of docs should remove vaguelyBoring docs", 0, numHits);
113 public void testStopwordsPerFieldMaxPercentDocs() throws Exception {
114 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, Arrays.asList("variedField"), 1f / 2f);
115 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
116 assertTrue("A filter on one Field should not affect queris on another", numHits > 0);
118 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, Arrays.asList("variedField", "repetitiveField"), 1f / 2f);
119 numHits = search(protectedAnalyzer, "repetitiveField:boring");
120 assertEquals("A filter on the right Field should affect queries on it", numHits, 0);
123 public void testStopwordsPerFieldMaxDocFreq() throws Exception {
124 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, Arrays.asList("repetitiveField"), 10);
125 int numStopWords = protectedAnalyzer.getStopWords("repetitiveField").length;
126 assertTrue("Should have identified stop words", numStopWords > 0);
128 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, Arrays.asList("repetitiveField", "variedField"), 10);
129 int numNewStopWords = protectedAnalyzer.getStopWords("repetitiveField").length + protectedAnalyzer.getStopWords("variedField").length;
130 assertTrue("Should have identified more stop words", numNewStopWords > numStopWords);
133 public void testNoFieldNamePollution() throws Exception {
134 protectedAnalyzer = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, appAnalyzer, reader, Arrays.asList("repetitiveField"), 10);
135 int numHits = search(protectedAnalyzer, "repetitiveField:boring");
136 assertEquals("Check filter set up OK", 0, numHits);
138 numHits = search(protectedAnalyzer, "variedField:boring");
139 assertTrue("Filter should not prevent stopwords in one field being used in another ", numHits > 0);
143 * analyzer that does not support reuse
144 * it is LetterTokenizer on odd invocations, WhitespaceTokenizer on even.
146 private class NonreusableAnalyzer extends Analyzer {
147 int invocationCount = 0;
149 public TokenStream tokenStream(String fieldName, Reader reader) {
150 if (++invocationCount % 2 == 0)
151 return new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
153 return new MockTokenizer(reader, MockTokenizer.SIMPLE, false);
157 public void testWrappingNonReusableAnalyzer() throws Exception {
158 QueryAutoStopWordAnalyzer a = new QueryAutoStopWordAnalyzer(TEST_VERSION_CURRENT, new NonreusableAnalyzer(), reader, 10);
159 int numHits = search(a, "repetitiveField:boring");
160 assertTrue(numHits == 0);
161 numHits = search(a, "repetitiveField:vaguelyboring");
162 assertTrue(numHits == 0);
165 public void testTokenStream() throws Exception {
166 QueryAutoStopWordAnalyzer a = new QueryAutoStopWordAnalyzer(
167 TEST_VERSION_CURRENT,
168 new MockAnalyzer(random, MockTokenizer.WHITESPACE, false), reader, 10);
169 TokenStream ts = a.tokenStream("repetitiveField", new StringReader("this boring"));
170 assertTokenStreamContents(ts, new String[] { "this" });