pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / remote / src / test / org / apache / lucene / search / TestRemoteSearchable.java
1 package org.apache.lucene.search;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.analysis.SimpleAnalyzer;
21 import org.apache.lucene.document.*;
22 import org.apache.lucene.index.IndexWriter;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.store.Directory;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28
29 import java.util.Collections;
30 import java.util.Set;
31 import java.util.HashSet;
32
33 public class TestRemoteSearchable extends RemoteTestCase {
34   private static Directory indexStore;
35   private static Searchable local;
36   
37   @BeforeClass
38   public static void beforeClass() throws Exception {
39     // construct an index
40     indexStore = newDirectory();
41     IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(
42         TEST_VERSION_CURRENT, new SimpleAnalyzer(TEST_VERSION_CURRENT)));
43     Document doc = new Document();
44     doc.add(newField("test", "test text", Field.Store.YES, Field.Index.ANALYZED));
45     doc.add(newField("other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
46     writer.addDocument(doc);
47     writer.optimize();
48     writer.close();
49     local = new IndexSearcher(indexStore, true);
50     startServer(local);
51   }
52   
53   @AfterClass
54   public static void afterClass() throws Exception {
55     local.close();
56     indexStore.close();
57     indexStore = null;
58   }
59   
60   private static void search(Query query) throws Exception {
61     // try to search the published index
62     Searchable[] searchables = { lookupRemote() };
63     Searcher searcher = new MultiSearcher(searchables);
64     ScoreDoc[] result = searcher.search(query, null, 1000).scoreDocs;
65
66     assertEquals(1, result.length);
67     Document document = searcher.doc(result[0].doc);
68     assertTrue("document is null and it shouldn't be", document != null);
69     assertEquals("test text", document.get("test"));
70     assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 2, document.getFields().size() == 2);
71     Set<String> ftl = new HashSet<String>();
72     ftl.add("other");
73     FieldSelector fs = new SetBasedFieldSelector(ftl, Collections.<String>emptySet());
74     document = searcher.doc(0, fs);
75     assertTrue("document is null and it shouldn't be", document != null);
76     assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 1, document.getFields().size() == 1);
77     fs = new MapFieldSelector(new String[]{"other"});
78     document = searcher.doc(0, fs);
79     assertTrue("document is null and it shouldn't be", document != null);
80     assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 1, document.getFields().size() == 1);
81   }
82
83   @Test
84   public void testTermQuery() throws Exception {
85     search(new TermQuery(new Term("test", "test")));
86   }
87
88   @Test
89   public void testBooleanQuery() throws Exception {
90     BooleanQuery query = new BooleanQuery();
91     query.add(new TermQuery(new Term("test", "test")), BooleanClause.Occur.MUST);
92     search(query);
93   }
94
95   @Test
96   public void testPhraseQuery() throws Exception {
97     PhraseQuery query = new PhraseQuery();
98     query.add(new Term("test", "test"));
99     query.add(new Term("test", "text"));
100     search(query);
101   }
102
103   // Tests bug fix at http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20290
104   @Test
105   public void testQueryFilter() throws Exception {
106     // try to search the published index
107     Searchable[] searchables = { lookupRemote() };
108     Searcher searcher = new MultiSearcher(searchables);
109     ScoreDoc[] hits = searcher.search(
110           new TermQuery(new Term("test", "text")),
111           new QueryWrapperFilter(new TermQuery(new Term("test", "test"))), 1000).scoreDocs;
112     assertEquals(1, hits.length);
113     ScoreDoc[] nohits = searcher.search(
114           new TermQuery(new Term("test", "text")),
115           new QueryWrapperFilter(new TermQuery(new Term("test", "non-existent-term"))), 1000).scoreDocs;
116     assertEquals(0, nohits.length);
117   }
118
119   @Test
120   public void testConstantScoreQuery() throws Exception {
121     // try to search the published index
122     Searchable[] searchables = { lookupRemote() };
123     Searcher searcher = new MultiSearcher(searchables);
124     ScoreDoc[] hits = searcher.search(
125           new ConstantScoreQuery(new TermQuery(new Term("test", "test"))), null, 1000).scoreDocs;
126     assertEquals(1, hits.length);
127   }
128 }