Example usage for org.springframework.data.solr.core.query Criteria or

List of usage examples for org.springframework.data.solr.core.query Criteria or

Introduction

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

Prototype

@SuppressWarnings("unchecked")
    public Crotch or(String fieldname) 

Source Link

Usage

From source file:com.nixmash.springdata.solr.repository.custom.CustomProductRepositoryImpl.java

private Criteria createHighlightedNameConditions(String[] words) {
    Criteria conditions = null;

    for (String word : words) {
        if (conditions == null) {
            conditions = new Criteria(Product.NAME_FIELD).contains(word);
        } else {//from w w w . j  a  v  a 2 s. com
            conditions = conditions.or(new Criteria(Product.NAME_FIELD).contains(word));
        }
    }

    return conditions;
}

From source file:com.nixmash.springdata.solr.repository.custom.CustomProductRepositoryImpl.java

private Criteria createSearchConditions(String[] words) {
    Criteria conditions = null;

    for (String word : words) {
        if (conditions == null) {
            conditions = new Criteria(Product.NAME_FIELD).contains(word)
                    .or(new Criteria(Product.CATEGORY_FIELD).contains(word));
        } else {/*w ww . ja  v  a  2s  .c  om*/
            conditions = conditions.or(new Criteria(Product.NAME_FIELD).contains(word))
                    .or(new Criteria(Product.CATEGORY_FIELD).contains(word));
        }
    }

    return conditions;
}

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  www.  j a  va  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  ww.j a  va2 s.c o  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.query.CriteriaTest.java

@Test
public void testOrWithCriteria() {
    Criteria criteria = new Criteria("field_1").startsWith("start");
    Criteria orCriteria = new Criteria("field_2").endsWith("end").startsWith("start2");
    criteria = criteria.or(orCriteria);
    Assert.assertEquals("field_1:start* OR field_2:(*end start2*)", criteria.createQueryString());
}

From source file:org.springframework.data.solr.core.query.CriteriaTests.java

/**
 * @see DATASOLR-105// w  w  w .  j av  a  2 s . com
 */
@Test
public void testCirteriasJoindWithOrShouldBeSiblingsOfCreatedCrotch() {

    Criteria c1 = new Criteria("field_1").startsWith("start").endsWith("end");
    Criteria c2 = new Criteria("field_2").startsWith("2start");
    Crotch crotch = c1.or(c2);

    Assert.assertThat(crotch.getSiblings(), IsIterableContainingInOrder.<Node>contains(c1, c2));
}

From source file:org.springframework.data.solr.core.query.CriteriaTests.java

/**
 * @see DATASOLR-105//from  w  ww.  j  av  a  2s  .  com
 */
@Test
public void testCrotchShouldReturnFieldNameOfMostRecentSibling() {

    Criteria c1 = new Criteria("field_1").startsWith("start").endsWith("end");
    Criteria c2 = new Criteria("field_2").startsWith("2start");
    Crotch crotch = c1.or(c2);
    Assert.assertThat(crotch.getField().getName(), Is.is("field_2"));
}