예제 #1
0
  /**
   * Split wordlist by blank space and create an OR criteria with each resulting word. Words are
   * added using wildcards.
   *
   * @param wordList a string with a list of words
   * @param fieldName the name of the field where criteria is applied.
   * @return an OR criteria with each word contained in wordlist.
   */
  private Criteria splitAndOrCriteria(String wordList, String fieldName) {
    Criteria orCriteria = null;
    String[] words = StringUtils.split(wordList);
    for (String word : words) {
      if (orCriteria == null) {
        orCriteria = new Criteria(fieldName).contains(word);
      } else {
        orCriteria = orCriteria.or(new Criteria(fieldName).contains(word));
      }
    }

    if (orCriteria != null) {
      SimpleQuery query = new SimpleQuery(orCriteria);
      DefaultQueryParser parser = new DefaultQueryParser();
      String queryString = parser.getQueryString(query);
      return new SimpleStringCriteria("(" + queryString + ")");
    } else {
      return null;
    }
  }
예제 #2
0
  /**
   * Transform the record in {@link SolrQuery} executable by an {@link
   * org.apache.solr.client.solrj.impl.HttpSolrServer}.
   *
   * @return the {@link SolrQuery} built with the data page this.
   */
  public SolrQuery toSolrQuery() {
    SolrQuery solrQuery = new SolrQuery();

    if (StringUtils.isNotBlank(customSolrQuery)) {
      solrQuery.setQuery(customSolrQuery);
    } else {
      solrQuery.setQuery("*:*");
      // data repositories
      if (dataRepositories != null && dataRepositories.size() > 0) {
        Criteria institutionCriteria = null;
        for (String institution : dataRepositories) {
          if (institutionCriteria == null) {
            institutionCriteria = new Criteria(SolrRecord.INSTITUTION).is(institution);
          } else {
            institutionCriteria =
                institutionCriteria.or(new Criteria(SolrRecord.INSTITUTION).is(institution));
          }
        }

        SimpleQuery query = new SimpleQuery(institutionCriteria);
        DefaultQueryParser parser = new DefaultQueryParser();
        String queryString = parser.getQueryString(query);
        solrQuery.addFilterQuery(queryString);
      } else {
        solrQuery.addFilterQuery(SolrRecord.INSTITUTION + ":*");
      }

      // theme keywords
      if (StringUtils.isNotBlank(themeKeyword)) {
        solrQuery.addFilterQuery(SolrRecord.THEME_KEYWORDS + ":" + themeKeyword);
        solrQuery.add(PF, SolrRecord.THEME_KEYWORDS + ":'" + themeKeyword + "'^9.0");
        solrQuery.add(PF, SolrRecord.LAYER_DISPLAY_NAME + ":'" + themeKeyword + "'^9.0");
      }
      if (StringUtils.isNotBlank(placeKeyword)) {
        solrQuery.addFilterQuery(SolrRecord.PLACE_KEYWORDS + ":" + placeKeyword);
        solrQuery.add(PF, SolrRecord.PLACE_KEYWORDS + ":'" + placeKeyword + "'^9.0");
      }
      if (StringUtils.isNotBlank(topicCategory)) {
        solrQuery.addFilterQuery(SolrRecord.ISO_TOPIC_CATEGORY + ":" + this.topicCategory);
      }

      if (dateFrom != null || dateTo != null) {
        Criteria contentDateCriteria =
            Criteria.where(SolrRecord.CONTENT_DATE).between(dateFrom, dateTo);
        SimpleQuery query = new SimpleQuery(contentDateCriteria);
        DefaultQueryParser parser = new DefaultQueryParser();
        String queryString = parser.getQueryString(query);
        solrQuery.addFilterQuery(queryString);
      }
      if (StringUtils.isNotBlank(originator)) {
        String originatorCriteria =
            splitAndConcatenateUsingOperator(Operator.AND, SolrRecord.ORIGINATOR, originator);
        solrQuery.addFilterQuery(originatorCriteria);
        solrQuery.add(PF, SolrRecord.ORIGINATOR + ":" + originator);
      }
      if (dataTypes != null && dataTypes.size() > 0) {
        StringBuilder concatenatedType = new StringBuilder();
        for (DataType dType : dataTypes) {
          concatenatedType.append(dType.toString().replace(" ", "+")).append(" ");
        }
        String dataTypeCriteria =
            splitAndConcatenateUsingOperator(
                Operator.OR, SolrRecord.DATA_TYPE, concatenatedType.toString());
        solrQuery.add("fq", dataTypeCriteria);
      }

      if (excludeRestrictedData) {
        solrQuery.addFilterQuery(SolrRecord.ACCESS + ":" + AccessLevel.Public);
      }

      if (fromSolrTimestamp != null || toSolrTimestamp != null) {
        Criteria solrTimestampCriteria =
            Criteria.where(SolrRecord.TIMESTAMP).between(fromSolrTimestamp, toSolrTimestamp);
        SimpleQuery query = new SimpleQuery(solrTimestampCriteria);
        DefaultQueryParser parser = new DefaultQueryParser();
        String queryString = parser.getQueryString(query);
        solrQuery.addFilterQuery(queryString);
      }
      // Add bbox filter only if user has not specified a custom solr
      // query.
      buildBoundigBoxQuery(solrQuery);

      String synonymsFilter = generateSynonymsQuery();
      if (StringUtils.isNotBlank(synonymsFilter)) {
        solrQuery.addFilterQuery(synonymsFilter);
      }
    }

    solrQuery.setRows(pageSize);
    solrQuery.setStart(page * pageSize);
    solrQuery.addSort(SortClause.desc("score"));

    return solrQuery;
  }