uCosminexus Enterprise Search プログラマーズガイド Javaインターフェース編

[目次][索引][前へ][次へ]

3.2 コーディング例

3.1の図のコーディング例を次に示します。コーディング例は次のパスにあります。

コーディング例(1/3)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import jp.co.hitachi_system.iwsearch.lib.client.IwsSearchCond;
import jp.co.hitachi_system.iwsearch.lib.client.IwsSearchResult;
import jp.co.hitachi_system.iwsearch.lib.client.IwsSearchResultEntry;
import jp.co.hitachi_system.iwsearch.lib.client.IwsSearcher;
import jp.co.hitachi_system.iwsearch.lib.client.exception.IwsSearchException;
 
/*
 * All Rights Reserved. Copyright (C) 2013, Hitachi Solutions, Ltd.
 */
 
/**
 * APIサンプルコード
 */
public class ApiSample {
 
    public static void main(String[] args) {
        
        if( args.length != 1){
            System.err.println("検索するキーワードが指定されていません。 ");
            System.err.println("usage :ApiSample keyword ");
        }
 
        /* 検索条件を格納するIwsSearchCondクラスのインスタンスを生成します */
        IwsSearchCond cond = new IwsSearchCond();
 
        /* 引数で指定した検索するキーワードをIwsSearchCondクラスにセットします */
        cond.setKeyword(args[0]);
 
        /* 検索するコンテンツの拡張子を指定する */
        cond.addExtension("doc");
        cond.addExtension("docx");
        
        /* NTFSクローラが集めたコンテンツを検索対象にする */
        cond.addCrawlType(IwsSearchCond.CRAWL_TYPE_NTFS);
        Date searchDate = null;

コーディング例(2/3)

        try {
            /* 更新日付が2013年1月1日以降のコンテンツを検索対象にする */
            searchDate = SimpleDateFormat.getDateInstance().parse("2013/01/01");
            cond.setDate(searchDate);
            cond.setDateFilterType(IwsSearchCond.DATE_TYPE_AFTER);
 
        }catch (ParseException e) {
            System.err.println("日付のフォーマットが不正です。");
            return;
        }
        
        /* 受け取る検索結果の件数を指定します */
        cond.setCount(100);
 
        /* 検索結果のソート順を更新日時の降順に設定します */
        cond.setSort(IwsSearchCond.SORT_KEY_DATE);
        cond.setOrder(IwsSearchCond.SORT_ORDER_DESC);
        
        /* 検索結果のタイトルと本文は、標準でHTML特殊文字がエスケープされ
         * ハイライトタグが埋め込まれてます。
         * このサンプルはコンソールアプリケーションなのでHTML特殊文字の
         * エスケープをOFFにします。ハイライトタグの埋め込みはHTML特殊文字の
         * エスケープがOFFの時は自動的にOFFになります。
         */
        cond.setUseHtmlEscape(false);
        
        /* 検索を行うIwsSearcherクラスのインスタンスを生成します
         * 引数には接続するuCosminexus Enterprise SearchのURLを指定します
         */
        IwsSearcher searcher = new IwsSearcher("http://localhost/iwsearch");
 
        /*
         * 検索する時に必要な認証IDとパスワードを設定します
         */
        searcher.setLoginID("loginid");
        searcher.setPassword("password");
 
        /* 検索結果を受け取るインスタンスを宣言します */
        IwsSearchResult result = null;
        try {
            /* 検索を行い、検索結果を受け取ります */
            result = searcher.search(cond);
        }catch (IwsSearchException e) {
            /* 検索中にエラーが発生した場合は、エラー情報を出力して終了します */
            System.err.println("検索中にエラーが発生しました。");
            System.err.println(e.getMessage());
            return;
        }

コーディング例(3/3)

        /* ヒット件数を画面に表示する */
        System.out.println("ヒット件数:"+result.getHitCount());
 
        /* ヒットしたコンテンツのタイトルを画面に表示する */
        for(IwsSearchResultEntry lEntry : result.getResultList()){
            System.out.println("title:"+lEntry.getTitle());
        }
        
        /* 検索終了 */
        return;
    }
}