FileDocCategorySizeDatePackage
TestRemoteSearchable.javaAPI DocApache Lucene 2.2.05319Sat Jun 16 22:20:28 BST 2007org.apache.lucene.search

TestRemoteSearchable

public class TestRemoteSearchable extends TestCase
version
$Id: TestRemoteSearchable.java 514675 2007-03-05 14:28:01Z gsingers $

Fields Summary
Constructors Summary
public TestRemoteSearchable(String name)

    super(name);
  
Methods Summary
private static org.apache.lucene.search.SearchablegetRemote()

    try {
      return lookupRemote();
    } catch (Throwable e) {
      startServer();
      return lookupRemote();
    }
  
private static org.apache.lucene.search.SearchablelookupRemote()

    return (Searchable)Naming.lookup("//localhost/Searchable");
  
private static voidsearch(org.apache.lucene.search.Query query)

    // try to search the published index
    Searchable[] searchables = { getRemote() };
    Searcher searcher = new MultiSearcher(searchables);
    Hits result = searcher.search(query);

    assertEquals(1, result.length());
    Document document = result.doc(0);
    assertTrue("document is null and it shouldn't be", document != null);
    assertEquals("test text", document.get("test"));
    assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 2, document.getFields().size() == 2);
    Set ftl = new HashSet();
    ftl.add("other");
    FieldSelector fs = new SetBasedFieldSelector(ftl, Collections.EMPTY_SET);
    document = searcher.doc(0, fs);
    assertTrue("document is null and it shouldn't be", document != null);
    assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 1, document.getFields().size() == 1);
    fs = new MapFieldSelector(new String[]{"other"});
    document = searcher.doc(0, fs);
    assertTrue("document is null and it shouldn't be", document != null);
    assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 1, document.getFields().size() == 1);
  
private static voidstartServer()

    // construct an index
    RAMDirectory indexStore = new RAMDirectory();
    IndexWriter writer = new IndexWriter(indexStore,new SimpleAnalyzer(),true);
    Document doc = new Document();
    doc.add(new Field("test", "test text", Field.Store.YES, Field.Index.TOKENIZED));
    doc.add(new Field("other", "other test text", Field.Store.YES, Field.Index.TOKENIZED));
    writer.addDocument(doc);
    writer.optimize();
    writer.close();

    // publish it
    LocateRegistry.createRegistry(1099);
    Searchable local = new IndexSearcher(indexStore);
    RemoteSearchable impl = new RemoteSearchable(local);
    Naming.rebind("//localhost/Searchable", impl);
  
public voidtestBooleanQuery()

    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term("test", "test")), BooleanClause.Occur.MUST);
    search(query);
  
public voidtestConstantScoreQuery()

    // try to search the published index
    Searchable[] searchables = { getRemote() };
    Searcher searcher = new MultiSearcher(searchables);
    Hits hits = searcher.search(
          new ConstantScoreQuery(new QueryFilter(
                                   new TermQuery(new Term("test", "test")))));
    assertEquals(1, hits.length());
  
public voidtestPhraseQuery()

    PhraseQuery query = new PhraseQuery();
    query.add(new Term("test", "test"));
    query.add(new Term("test", "text"));
    search(query);
  
public voidtestQueryFilter()

    // try to search the published index
    Searchable[] searchables = { getRemote() };
    Searcher searcher = new MultiSearcher(searchables);
    Hits hits = searcher.search(
          new TermQuery(new Term("test", "text")),
          new QueryFilter(new TermQuery(new Term("test", "test"))));
    assertEquals(1, hits.length());
    Hits nohits = searcher.search(
          new TermQuery(new Term("test", "text")),
          new QueryFilter(new TermQuery(new Term("test", "non-existent-term"))));
    assertEquals(0, nohits.length());
  
public voidtestTermQuery()

    search(new TermQuery(new Term("test", "test")));