Newer
Older
TalkjaSolr / src / java / solrbook / client / util / SearchUtil.java
@haya4 haya4 on 17 Mar 2019 3 KB 1st
package solrbook.client.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

/**
 * @author $Author$
 * 
 */
public class SearchUtil {
    public static final String ENCODING = "UTF-8";

    private static HashMap<String, String[][]> facetQueries;

    private static Map<String, String> facetLabels;

    private static final String[] SUMMARY_HIGHLIGHT_FIELDS = {
        "contents"
    };

    private static final String TITLE_HIGHLIGHT_FIELDS = "title";

    private static final String[] FIELDS = {
        "path",
        "dir",
        "url",
        "id",
        "title",
        "name",
        "date",
        "contents"
    };

  /*
  static {
    facetQueries = new HashMap<String, String[][]>();
    String[][] yearsArr = { { "pub_date:[20090101 TO *]", "2009年以降" },

    { "pub_date:[20080101 TO 20081231]", "2008年" },
        { "pub_date:[20070101 TO 20071231]", "2007年" },
        { "pub_date:[20060101 TO 20061231]", "2006年" },
        { "pub_date:[20050101 TO 20051231]", "2005年" },
        { "pub_date:[* TO 20041231]", "2004年以前" } };

    //facetQueries.put("発売年", yearsArr);

    facetLabels = new HashMap<String, String>();
    facetLabels.put("date", "投稿日時");
    facetLabels.put("author_facet", "著者");

  }
  */

    /**
     * @return summaryHighlightFields
     */
    public static String[] getSummaryHighlightFields() {
        return SUMMARY_HIGHLIGHT_FIELDS;
    }

    /**
     * @return titleHighlightField
     */
    public static String getTitleHighlightField() {
        return TITLE_HIGHLIGHT_FIELDS;
    }

    /**
     * @return fields
     */
    public static String[] getFields() {
        return FIELDS;
    }

    public static HashMap<String, String[][]> getFacetQueries() {
      return facetQueries;
    }

    public static Map<String, String> getFacetLabels() {
      return facetLabels;
    }

    public static String getFacetLabel(String fieldName) {
        String label = facetLabels.get(fieldName);
        if (label == null)
          label = fieldName;
        return label;
    }

  public static String getLinkStr(
    String query,
    String[] filterQueries,
    String filterValue,
    int start) {
    List<String> link = new ArrayList<>();

    if (!StringUtils.isEmpty(query)) {
      link.add(getUrlEncodedParam("query", query));
    }

    if (filterQueries != null && filterQueries.length > 0) {
      for (String fq : filterQueries) {
        if (!StringUtils.isEmpty(fq)) {
          link.add(getUrlEncodedParam("fq", fq));
        }
      }
    }

    if (!StringUtils.isEmpty(filterValue)) {
      link.add(getUrlEncodedParam("fq", filterValue));
    }

    if (start > 0) {
      link.add("start=" + start);
    }

    String linkStr = "";
    for (int i = 0; i < link.size(); i++) {
      if (i == 0) {
        linkStr += "?";
      } else {
        linkStr += "&";
      }
      linkStr += link.get(i);
    }

    return linkStr;
  }

  private static String getUrlEncodedParam(String paramName, String value) {
    String tmp = "";
    try {
      tmp = paramName + "=" + URLEncoder.encode(value, ENCODING);
    } catch (UnsupportedEncodingException uee) {
    }
    return tmp;
  }

    /*
    public static int getFacetQueryTotalCount(
      String queryLabelName,
      Map<String, Integer> facetQueriesMap) {
      int total = 0;
      if (facetQueriesMap != null) {
        String[][] queriesArray = SearchUtil.getFacetQueries().get(queryLabelName);
        for (int i = 0; i < queriesArray.length; i++) {
          if (facetQueriesMap.containsKey(queriesArray[i][0])) {
            total += facetQueriesMap.get(queriesArray[i][0]).intValue();
          }
        }
      }
      return total;
    }
    */
}