5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaからSolr5.4.0のインデクスを追加、検索する

Last updated at Posted at 2016-02-11

はじめに

Apache SolrはOSSの検索システムですが、非常にパワフルな検索システムです。また、頻繁にアップデートされています。

以前から仕事でSolrを使っていて、Solr4.0くらいからSolrCloudが使えるようになりましたがちょっと億劫で使っていませんでした。
(Zookeeperの設定とか、Zookeeperの設定とか)

Solr5.0からは完全に放置していましたが、今回勉強がてらにSolr5.4使ってみたらだいぶ使いやすくなっていました。
(Zookeeperの設定いらなくなってた)

合わせて、Solrの起動方法も変わっていて、solrconfig.xmlやschema.xmlの設定をしなくても簡単に使えるようになっていて、感動しました。

当然、日本語のインデクスをkuromojiでやりたい場合は、以前と同様にschema.xmlの設定が必要です。個人的にはkuromoji使わなくてもデフォルトの文字Nグラムである程度なんとかなると思ってます。

だいぶタイトルから脱線しましたが、肝心の内容についてはGithubにプロジェクトを作ったのでそっちを見て下さい。

サンプルコード

[https://github.com/keigohtr/Solr-sample]
(https://github.com/keigohtr/Solr-sample)

(さすがに不親切すぎるので、少しコードを載せます)

インデクスの追加
  public static void addApis(String zkHost, String collection, List<DocApi> list){
    LOG.info("Update solr index start.");
    try {
      CloudSolrClient server = new CloudSolrClient(zkHost);
      server.setDefaultCollection(collection);
      Collection<SolrInputDocument> sdocs = new ArrayList<SolrInputDocument>();
      for (DocApi doc: list) {
        SolrInputDocument sdoc = new SolrInputDocument();
        sdoc.addField("id", doc.getId());
        sdoc.addField("title", doc.getTitle());
        sdoc.addField("description", doc.getDescription());
        sdoc.addField("categoryList", doc.getCategoryList().toArray());
        sdoc.addField("enabled", doc.getEnabled());
        sdoc.addField("solrRegisteredAt", new Date());
        sdocs.add(sdoc);
      }
      server.add( sdocs );
      server.commit();
      server.close();
    } catch (SolrServerException e) {
      LOG.error("SolrServerException",e);
    } catch (IOException e) {
      LOG.error("IOException",e);
    }
    LOG.info("Update solr index end.");
  }

これはSolrCloudへの接続です。ポイントはSolrのURLではなくZookeeperのURLとcollection名を入力する点です。

インデクスの追加はSolrInputDocumentを作って、serverに投げればOKです。この例のように複数のドキュメントを一括でコミットできます。

以前のSolrではschema.xmlを設定してやらないといけませんでしたが、Solr5.4では特にこだわりがなければschema.xmlを設定しなくてもSolr側で勝手にfieldの設定をしてくれます。この例ではStringとBooleanとListとDateを扱いました。

インデクスの検索
  public static List<String> searchApis(String zkHost, String collection, SolrQuery query) {
    LOG.info("Search solr start.");
    List<String> apiIds = new ArrayList<String>();
    try {
      CloudSolrClient server = new CloudSolrClient(zkHost);
      server.setDefaultCollection(collection);
      QueryResponse response = server.query(query);
      SolrDocumentList list = response.getResults();
      LOG.debug(list.getNumFound() + " hits.");
      for (SolrDocument doc : list) {
        apiIds.add((String)doc.get("id"));
      }
      server.close();
    } catch (SolrServerException e) {
      LOG.error("SolrServerException",e);
    } catch (IOException e) {
      LOG.error("IOException",e);
    }
    LOG.info("Search solr end.");
    return apiIds;
  }

検索もインデクスの追加と同様です。QueryResponseにSolrQueryを与えて、ServerからSolrDocumentListを受け取ります。

終わりに

本件は私がQiitaとgithubデビューをするための第一歩としてやりました。ググれば出てくる情報ですが、誰かの役に立てば幸いです。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?