Example usage for org.apache.solr.client.solrj SolrQuery set

List of usage examples for org.apache.solr.client.solrj SolrQuery set

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery set.

Prototype

public ModifiableSolrParams set(String name, String... val) 

Source Link

Document

Replace any existing parameter with the given name.

Usage

From source file:org.sindice.siren.solr.SolrServerWrapper.java

License:Apache License

public String[] searchNTriple(final String q, final String retrievedField)
        throws SolrServerException, IOException {
    final SolrQuery query = new SolrQuery();
    query.setQueryType("siren");
    query.set(SirenParams.NQ, q);

    final QueryResponse response = server.query(query);
    final SolrDocumentList docList = response.getResults();

    final int size = docList.size();
    final String docIDs[] = new String[size];
    SolrDocument doc = null;//from www. j a  v  a 2s . c o m
    for (int i = 0; i < size; i++) {
        doc = docList.get(i);
        docIDs[i] = (String) doc.getFieldValue(retrievedField);
    }
    return docIDs;
}

From source file:org.sindice.siren.solr.TestSirenQParserPlugin.java

License:Apache License

@Test
public void testParams() throws IOException, SolrServerException {
    final SolrQuery query = new SolrQuery();
    query.setQuery("test");
    query.set(SirenParams.KQF, "ntriple^1.0");
    query.set(SirenParams.NQ, "* <name> \"renaud\"");
    query.set(SirenParams.NQF, "ntriple^1.0");
    query.set(SirenParams.NQFO, "scattered");
    query.setQueryType("siren");
    wrapper.search(query, "url");
}

From source file:org.sindice.siren.solr.TestSirenQParserPlugin.java

License:Apache License

@Test
public void testEmptyKeywordQueryParam() throws IOException, SolrServerException {
    this.addNTripleString("id1", "<http://s> <http://p> \"test\" .");
    final SolrQuery query = new SolrQuery();
    query.set(SirenParams.NQF, "ntriple");
    query.set(SirenParams.NQ, "* <http://p> \"test\"");
    query.setQueryType("unittest-siren");

    final String[] results = wrapper.search(query, "url");
    assertEquals(1, results.length);/*from  ww  w  . ja  v a  2  s .  co  m*/
}

From source file:org.sindice.siren.solr.TestSirenQParserPlugin.java

License:Apache License

/**
 * SRN-90: Test keyword query with default operator AND
 *///ww  w. j  ava2s  .  c  om
@Test
public void testKeywordQueryWithDefaultOperator() throws IOException, SolrServerException {
    this.addNTripleString("id1", "<http://s> <http://p> \"test\" .");
    this.addNTripleString("id2", "<http://s> <http://p> \"test\" .");
    final SolrQuery query = new SolrQuery();
    query.setQuery("id1 test");
    query.set(SirenParams.KQF, "ntriple^1.0 url^1.0");
    query.setQueryType("siren");

    final String[] results = wrapper.search(query, "url");
    // Default Operator = AND : Only one document should match
    assertEquals(1, results.length);
}

From source file:org.sindice.siren.solr.TestSirenQParserPlugin.java

License:Apache License

/**
 * SRN-97// w ww. ja  v a 2 s.  c o  m
 */
@Test
public void testDefaultOperatorLiteralPattern() throws SolrServerException, IOException {
    this.addNTripleString("id1", "<http://test.com/1> <http://test.com/p> \"apple orange\"");
    this.addNTripleString("id2", "<http://test.com/1> <http://test.com/p> \"apple banana\"");

    final SolrQuery query = new SolrQuery();
    query.set(SirenParams.NQ, "* * 'apple orange'");
    query.setQueryType("siren");

    final String[] results = wrapper.search(query, "url");
    // Default Operator = AND : Only one document should match
    assertEquals(1, results.length);
}

From source file:org.sindice.siren.solr.TestSirenQParserPlugin.java

License:Apache License

/**
 * SRN-97//from  ww w. java  2s .co m
 */
@Test
public void testDefaultOperatorURIPattern() throws SolrServerException, IOException {
    this.addNTripleString("id1", "<http://test.com/1> <http://test.com/p> <http://test.com/AppleOrange>");
    this.addNTripleString("id2", "<http://test.com/1> <http://test.com/p> <http://test.com/AppleBanana>");

    final SolrQuery query = new SolrQuery();
    query.set(SirenParams.NQ, "* * <apple orange>");
    query.setQueryType("siren");

    final String[] results = wrapper.search(query, "url");
    // Default Operator = AND : Only one document should match
    assertEquals(1, results.length);
}

From source file:org.sindice.siren.solr.TestSirenQParserPlugin.java

License:Apache License

@Test
public void testEmptyNtripleFieldParam() throws IOException, SolrServerException {
    this.addNTripleString("id1", "<http://s> <http://test.org/name> \"renaud\" \"test\".");
    this.addNTripleString("id2", "<http://s> <http://p> \"test\" .");
    final SolrQuery query = new SolrQuery();
    query.setQuery("test");
    query.set(SirenParams.NQ, "* <name> \"renaud\"");
    query.setQueryType("unittest-siren");

    final String[] results = wrapper.search(query, "url");
    assertEquals(1, results.length);/* w  w w  .  j a  va2s.c  om*/
}

From source file:org.sindice.siren.solr.TestSirenQParserPlugin.java

License:Apache License

/**
 * SRN-96//from w ww.j  ava  2  s .  co  m
 */
@Test
public void testASCIIFoldingExpansion() throws IOException, SolrServerException {
    this.addNTripleString("id1", "<http://s> <http://p> \"cafe\" .");
    this.addNTripleString("id2", "<http://s> <http://p> \"caf\" .");
    SolrQuery query = new SolrQuery();
    query.setQuery("cafe");
    query.setQueryType("siren");
    query.setIncludeScore(true);

    // should match the two documents, with same score
    QueryResponse response = wrapper.server.query(query);
    SolrDocumentList docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    float score1 = (Float) docList.get(0).getFieldValue("score");
    float score2 = (Float) docList.get(1).getFieldValue("score");
    Assert.assertTrue("Score should be identical", score1 == score2);

    query = new SolrQuery();
    query.setQueryType("siren");
    query.set(SirenParams.NQ, "* * 'cafe'");
    query.setIncludeScore(true);

    // should match the two documents, with same score
    response = wrapper.server.query(query);
    docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    score1 = (Float) docList.get(0).getFieldValue("score");
    score2 = (Float) docList.get(1).getFieldValue("score");
    Assert.assertTrue("Score should be identical", score1 == score2);

    // should match the two documents, but should assign different score
    // id2 should receive better score than id1
    query = new SolrQuery();
    query.setQuery("caf");
    query.setQueryType("siren");
    query.setIncludeScore(true);

    response = wrapper.server.query(query);
    docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    if (docList.get(0).getFieldValue("url").equals("id1")) {
        score1 = (Float) docList.get(0).getFieldValue("score");
        score2 = (Float) docList.get(1).getFieldValue("score");
    } else {
        score2 = (Float) docList.get(0).getFieldValue("score");
        score1 = (Float) docList.get(1).getFieldValue("score");
    }
    Assert.assertTrue("id2 should get higher score than id1", score1 < score2);

    query = new SolrQuery();
    query.setQueryType("siren");
    query.set(SirenParams.NQ, "* * 'caf'");
    query.setIncludeScore(true);

    response = wrapper.server.query(query);
    System.out.println(response);
    docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    if (docList.get(0).getFieldValue("url").equals("id1")) {
        score1 = (Float) docList.get(0).getFieldValue("score");
        score2 = (Float) docList.get(1).getFieldValue("score");
    } else {
        score2 = (Float) docList.get(0).getFieldValue("score");
        score1 = (Float) docList.get(1).getFieldValue("score");
    }
    Assert.assertTrue("id2 should get higher score than id1", score1 < score2);
}

From source file:org.sparkcommerce.core.search.service.solr.SolrSearchServiceImpl.java

License:Apache License

/**
 * Given a qualified solr query string (such as "category:2002"), actually performs a solr search. It will
 * take into considering the search criteria to build out facets / pagination / sorting.
 *
 * @param qualifiedSolrQuery/*from  w  w  w .  ja  v a  2  s  .  c o  m*/
 * @param facets
 * @param searchCriteria
 * @return the ProductSearchResult of the search
 * @throws ServiceException
 */
protected ProductSearchResult findProducts(String qualifiedSolrQuery, List<SearchFacetDTO> facets,
        ProductSearchCriteria searchCriteria, String defaultSort, String... filterQueries)
        throws ServiceException {
    Map<String, SearchFacetDTO> namedFacetMap = getNamedFacetMap(facets, searchCriteria);

    // Build the basic query
    // Solr queries with a 'start' parameter cannot be a negative number
    int start = (searchCriteria.getPage() <= 0) ? 0 : (searchCriteria.getPage() - 1);
    SolrQuery solrQuery = new SolrQuery().setQuery(qualifiedSolrQuery).setFields(shs.getProductIdFieldName())
            .setRows(searchCriteria.getPageSize()).setStart((start) * searchCriteria.getPageSize());
    if (filterQueries != null) {
        solrQuery.setFilterQueries(filterQueries);
    }
    solrQuery.addFilterQuery(shs.getNamespaceFieldName() + ":(\"" + shs.getCurrentNamespace() + "\")");
    solrQuery.set("defType", "edismax");
    solrQuery.set("qf", buildQueryFieldsString());

    // Attach additional restrictions
    attachSortClause(solrQuery, searchCriteria, defaultSort);
    attachActiveFacetFilters(solrQuery, namedFacetMap, searchCriteria);
    attachFacets(solrQuery, namedFacetMap);

    modifySolrQuery(solrQuery, qualifiedSolrQuery, facets, searchCriteria, defaultSort);

    extensionManager.getProxy().modifySolrQuery(solrQuery, qualifiedSolrQuery, facets, searchCriteria,
            defaultSort);

    if (LOG.isTraceEnabled()) {
        try {
            LOG.trace(URLDecoder.decode(solrQuery.toString(), "UTF-8"));
        } catch (Exception e) {
            LOG.trace("Couldn't UTF-8 URL Decode: " + solrQuery.toString());
        }
    }

    // Query solr
    QueryResponse response;
    List<SolrDocument> responseDocuments;
    int numResults = 0;
    try {
        response = SolrContext.getServer().query(solrQuery);
        responseDocuments = getResponseDocuments(response);
        numResults = (int) response.getResults().getNumFound();

        if (LOG.isTraceEnabled()) {
            LOG.trace(response.toString());

            for (SolrDocument doc : responseDocuments) {
                LOG.trace(doc);
            }
        }
    } catch (SolrServerException e) {
        throw new ServiceException("Could not perform search", e);
    }

    // Get the facets
    setFacetResults(namedFacetMap, response);
    sortFacetResults(namedFacetMap);

    // Get the products
    List<Product> products = getProducts(responseDocuments);

    ProductSearchResult result = new ProductSearchResult();
    result.setFacets(facets);
    result.setProducts(products);
    setPagingAttributes(result, numResults, searchCriteria);
    return result;
}

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

License:Apache License

private boolean enableFaceting(SolrQuery solrQuery, FacetQuery query) {
    FacetOptions facetOptions = query.getFacetOptions();
    if (facetOptions == null || (!facetOptions.hasFields() && !facetOptions.hasFacetQueries()
            && !facetOptions.hasPivotFields())) {
        return false;
    }/*from   ww w  .j  a v a 2  s .  c  o  m*/
    solrQuery.setFacet(true);
    solrQuery.setFacetMinCount(facetOptions.getFacetMinCount());
    solrQuery.setFacetLimit(facetOptions.getPageable().getPageSize());
    if (facetOptions.getPageable().getPageNumber() > 0) {
        int offset = Math.max(0, facetOptions.getPageable().getOffset());
        solrQuery.set(FacetParams.FACET_OFFSET, offset);
    }
    if (FacetOptions.FacetSort.INDEX.equals(facetOptions.getFacetSort())) {
        solrQuery.setFacetSort(FacetParams.FACET_SORT_INDEX);
    }
    return true;
}