📄 grouping.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * $RCSFile$ $Revision: 1.6 $ $Date: 2006/02/01 00:20:31 $ * Copyright (c) 2003 Autonomy Corp. All Rights Reserved. */import java.io.*;import java.net.*;import java.text.*;import java.util.*;import com.ultraseek.xpa.search.*;import com.ultraseek.xpa.server.*;/** * Grouping of search results. * <p> * This application demonstrates how to use Ultraseek to * group search results. * <p> * The same query has different grouping criteria applied, * and a summary of the groups is printed. * The grouping criteria demonstrated are: * <ul> * <li>Group by Location</li> * <li>Group by site</li> * <li>Group by Publisher</li> * <li>Group by Collection name</li> * <li>Sort by Date, Group by Month/Year</li> * </ul> * <p> * To use this application, edit the file "Sample.properties" * to point to your Ultraseek server. * * @see GroupingSearchable * @see GroupedSearchResult * @since XPA2.1 */public class Grouping { /** * Loads parameters from the file Sample.properties */ static String getString(String key, String def) { try { String resourceName = System.getProperty("Sample"); if (resourceName==null) resourceName = "Sample"; ResourceBundle settings = ResourceBundle.getBundle( resourceName ); return settings.getString(key); } catch (MissingResourceException e) { return def; } catch (Exception e) { System.out.println( "Property file problem: " + e ); return def; } } static final String serverName = getString("UltraseekServer.host", "software-demo.ultraseek.com"); static final int serverPort = Integer.valueOf(getString("UltraseekServer.port", "80")).intValue(); static final String serverProto = getString("UltraseekServer.protocol", "http"); static UltraseekServer server = new UltraseekServer(serverName, serverPort, serverProto); /** * Show the results of the same query, grouped by different criteria. */ static void demoGrouping(String query) throws IOException { Query q = Query.parse(query); try { SearchResultList srl = server.search(q); // Show summary of search results. System.out.println(); System.out.println( "" + srl.getDocCount() + " documents were searched."); System.out.println( "" + srl.getResultCount() + " document(s) match your query."); System.out.println( "" + srl.size() + " search result(s) have been prioritized."); showGrouping( "Group by Location (default)", new GroupingSearchable(server).search(q) ); showGrouping( "Group by Site", new GroupingSearchable(server, new GroupBySite()).search(q) ); showGrouping( "Group by Publisher", new GroupingSearchable(server, new GroupByPublisher()).search(q) ); showGrouping( "Group by Collection", new GroupingSearchable(server, new GroupByCollection()).search(q) ); // This last example resorts the result by date, // then groups by month showGrouping( "Sorted by Date, Grouped by Month", new GroupingSearchable(new DateSortingSearchable(server), new GroupByMonth()).search(q) ); } catch (QueryNotSupportedException e) { System.out.println( "Query problem: " + e ); } } /** * Displays a summary of results that have been grouped. */ static void showGrouping( String msg, SearchResultList srl ) throws XPARuntimeException, IOException { List groupHeaders = new ArrayList(); String thisGroupKey = null; List thisGroup = null; Iterator it = srl.iterator(); int counter = 0; // Walk the search results, and make a list for each group try { while (it.hasNext()) { SearchResult sr = (SearchResult) it.next(); if (!(sr instanceof GroupedSearchResult)) continue; GroupedSearchResult gsr = (GroupedSearchResult) sr; String key = gsr.getKey(); counter++; if (key.equals(thisGroupKey)) { thisGroup.add(gsr); } else { // Start of a new group thisGroup = new ArrayList(); thisGroup.add(gsr); thisGroupKey = gsr.getKey(); groupHeaders.add(thisGroup); } } } catch (NoSuchElementException e) { // Normal end of list - not an error } catch (XPARuntimeException e) { System.out.println( "Problem: " + e ); } // Now give a summary of the groups: System.out.println("\nGrouping criteria: " + msg); System.out.println("The top " + counter + " search results were grouped"); System.out.println("into " + groupHeaders.size() + " groups." ); for (int i = 0; i < groupHeaders.size(); i++) { List group = (List) groupHeaders.get(i); GroupedSearchResult head = (GroupedSearchResult) group.get(0); System.out.println( "" + (i+1) + ". " + head.getKey() + " (" + group.size() + " results)" ); } } static public void main(String args[]) throws IOException { System.out.println("Grouping Search Results Demo"); System.out.println("Using Ultraseek at " + server ); System.out.println(" (version " + server.getVersionString() +")" ); InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); while (true) { System.out.print("\n\nEnter a query (EOF to end): "); String line = bufferedReader.readLine(); if (line==null) break; try { demoGrouping( line ); } catch (IOException e) { System.out.println( "Problem communicating with server: " + e ); } catch (XPARuntimeException e) { System.out.println( "Problem communicating with server: " + e.getCause() ); } } }}/** * Groups search results by Publisher information. */class GroupByPublisher implements SearchResultGrouper{ public GroupByPublisher(){}; public String getGroupingKey(SearchResult searchResult) throws java.io.IOException { return "publisher:\"" + searchResult.getPublisher()+"\""; }}/** * Groups search results by site. * This is different from "group by location", which uses * both the site and path components for grouping. */class GroupBySite implements SearchResultGrouper{ public GroupBySite(){}; public String getGroupingKey(SearchResult searchResult) throws java.io.IOException { URL url = searchResult.getURL(); return "url:" + url.getHost(); }}/** * Groups search results by search collection name. */class GroupByCollection implements SearchResultGrouper{ public GroupByCollection(){}; public String getGroupingKey(SearchResult searchResult) throws java.io.IOException { SearchCollection col = searchResult.getSearchCollection(); if (col==null) return null; return "collection:" + col.getID(); }}/** * Groups search results by Month / Year. * In this example the key cannot be used as a query * to expand the search. */class GroupByMonth implements SearchResultGrouper{ static SimpleDateFormat df = new SimpleDateFormat("MMMMMMMM yyyy"); public GroupByMonth(){}; public String getGroupingKey(SearchResult searchResult) throws java.io.IOException { Date date = searchResult.getDate(); return df.format(date); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -