Example usage for org.springframework.data.solr.core DefaultQueryParser DefaultQueryParser

List of usage examples for org.springframework.data.solr.core DefaultQueryParser DefaultQueryParser

Introduction

In this page you can find the example usage for org.springframework.data.solr.core DefaultQueryParser DefaultQueryParser.

Prototype

DefaultQueryParser

Source Link

Usage

From source file:org.opengeoportal.harvester.api.client.solr.SolrSearchParams.java

/**
 * Transform the record in {@link SolrQuery} executable by an
 * {@link org.apache.solr.client.solrj.impl.HttpSolrServer}.
 * /*from   ww  w. j  av a 2s.c  o m*/
 * @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;
}

From source file:org.opengeoportal.harvester.api.client.solr.SolrSearchParams.java

/**
 * Split wordlist by blank space and create an OR criteria with each
 * resulting word. Words are added using wildcards.
 * /*  w  w  w.j  a v  a  2  s. co m*/
 * @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;
    }
}

From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java

@Before
public void setUp() {
    this.queryParser = new DefaultQueryParser();
}