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.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;
29 import java.util.Collections;
31 import java.util.HashSet;
33 public class TestRemoteSearchable extends RemoteTestCase {
34 private static Directory indexStore;
35 private static Searchable local;
38 public static void beforeClass() throws Exception {
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);
49 local = new IndexSearcher(indexStore, true);
54 public static void afterClass() throws Exception {
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;
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>();
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);
84 public void testTermQuery() throws Exception {
85 search(new TermQuery(new Term("test", "test")));
89 public void testBooleanQuery() throws Exception {
90 BooleanQuery query = new BooleanQuery();
91 query.add(new TermQuery(new Term("test", "test")), BooleanClause.Occur.MUST);
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"));
103 // Tests bug fix at http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20290
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);
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);