📄 blendeddatesorting.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * * $RCSFile$ $Revision: 1.9 $ $Date: 2006/01/25 16:56:17 $ * * Copyright (c) 2002-2003 Autonomy Corp. All Rights Reserved. * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.io.*;import java.util.*;import com.ultraseek.xpa.search.*;import com.ultraseek.xpa.server.UltraseekServer;/** * A simple demo application that searches * several Ultraseek collections * and sorts the results by date and relevance. * * The order of results in the <code>SearchResultList</code> * is different depending on how * Date sorting is blended with Relevance sorting. */public class BlendedDateSorting { public BlendedDateSorting() {} static void printList(String label, SearchResultList searchResultList, int count) { System.out.println(); System.out.println(label); try { System.out.print("size()="+searchResultList.size() + " " + "getResultCount()="+searchResultList.getResultCount()); } catch (IOException e) { System.out.println("Exception on getResultCount()"+ e ); } System.out.println(); int counter = 1; if (!searchResultList.isEmpty()) { Iterator iterator = searchResultList.iterator(); while (iterator.hasNext() && count-- > 0) { SearchResult searchResult = (SearchResult)iterator.next(); System.out.println(); try { System.out.println(counter++ + ": " + searchResult.getURL()); System.out.println(" " + searchResult.getTitle()); System.out.println(" " + searchResult.getDate()); } catch (IOException e) { System.out.println("Exception: "+e); } } } else { System.out.println("There are no search results."); } System.out.println("-------------------------------------------"); }; /** * Display the name of each search collection supported by a server */ static void showCollections(SearchServer searchServer) throws IOException { Collection collections = searchServer.getSearchCollections(); Iterator collection = collections.iterator(); System.out.println( searchServer.toString() + " has the following collections:" ); while (collection.hasNext()) { SearchCollection col = (SearchCollection) collection.next(); System.out.println( col.getID() + " (" + col.getName() + ")" ); } System.out.println(); } /** * Return the first count collections from the given searchServer. */ static SearchCollection[] getCollections(SearchServer searchServer, int count) throws IOException { Collection collections = searchServer.getSearchCollections(); SearchCollection[] result; result = new SearchCollection[Math.min(collections.size(),count)]; Iterator collection = collections.iterator(); int i = 0; while (collection.hasNext() && count-- > 0) { SearchCollection col = (SearchCollection) collection.next(); result[i++] = col; }; return result; }; static final int SORT_SIZE = 5; // Number of results to collect static final int NUM_COLLECTIONS = 3; // Number of collections to search static final int DISPLAY_SIZE = 4; // Number of results to display public static void main(String[] args) throws Exception { InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); System.out.println(); System.out.println("This is a simple demo application that searches"); System.out.println("several Ultraseek collections"); System.out.println("and sorts the results by date and relevance using several methods."); System.out.println(); SearchServer searchServer = new UltraseekServer("software-search.ultraseek.com",80); SearchCollection[] collections = getCollections(searchServer, NUM_COLLECTIONS); System.out.println("Searching the following collections on " + searchServer.toString()); for (int i = 0; i < collections.length; i++) System.out.println( " " + collections[i].getName() ); // Method 1 /* Blend the results from the collections, Sort the blended results by date */ DateSortingSearchable method1; { CompositeSearchable blended = new CompositeSearchable( collections ); method1 = new DateSortingSearchable(blended); method1.setSortSize(SORT_SIZE); } // Method 2 /* Sort the results from each collection by date, blend the results into a single list by relevance. */ CompositeSearchable method2; { DateSortingSearchable[] sorted = new DateSortingSearchable[collections.length]; for (int i = 0; i < collections.length; i++) { sorted[i] = new DateSortingSearchable(collections[i]); sorted[i].setSortSize(SORT_SIZE); } method2 = new CompositeSearchable(sorted); } // Method 3 /* Sort the results from each collection by date. Blend the results into a single list by relevance. Sort the resulting list by date. */ DateSortingSearchable method3; { DateSortingSearchable[] sorted = new DateSortingSearchable[collections.length]; for (int i = 0; i < collections.length; i++) { sorted[i] = new DateSortingSearchable(collections[i]); } CompositeSearchable blended = new CompositeSearchable(sorted); method3 = new DateSortingSearchable(blended); method3.setSortSize(SORT_SIZE); } Searchable searchables[] = new Searchable[] { method1, method2, method3 }; String labels[] = new String[] { "blend, then DateSort", "DateSort each collection, then blend", "DateSort each collection, then blend, then DateSort" }; for (;;) { System.out.println(); System.out.println("Enter a query. (control-c to end)"); System.out.print("search: "); String line = bufferedReader.readLine(); if (line==null) break; Query query = Query.parse(line); for (int i = 0; i < searchables.length; i++) { SearchResultList list = searchables[i].search(query); printList( labels[i], list, DISPLAY_SIZE); }; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -