List of usage examples for org.apache.solr.client.solrj SolrQuery setParam
public SolrQuery setParam(String name, boolean value)
From source file:com.pearson.openideas.cq5.components.services.solr.SolrHelper.java
License:Open Source License
/** * Add search criteria to a SOLR Query.//from ww w .j av a 2 s . c o m * * @param searchCriteria * the search criteria to add * @param query * the query */ public static void addSearchCriteria(final Map<String, Object> searchCriteria, final SolrQuery query) { for (String key : searchCriteria.keySet()) { Object obj = searchCriteria.get(key); if (obj instanceof String[]) { log.debug("Adding multi-value search criteria {}={}.", key, String.valueOf(obj)); query.setParam(key, (String[]) obj); } else { log.debug("Adding single-value search criteria {}={}.", key, String.valueOf(obj)); query.setParam(key, String.valueOf(obj)); } } }
From source file:com.seajas.search.attender.service.search.SolrSearchService.java
License:Open Source License
/** * {@inheritDoc}/*from www. j ava 2 s. c o m*/ */ @Override public List<SearchResult> performSearch(final String query, final Date startDate, final Date endDate, final Map<String, String> parameters, final Integer maxResults, final String taxonomyLanguage) { List<SearchResult> results = new ArrayList<SearchResult>(maxResults); SolrQuery solrQuery = new SolrQuery(); SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // Combine the shards into a Solr-acceptable list String shardList = ""; for (String shard : shards) shardList += (shardList.length() > 0 ? "," : "") + shard; solrQuery.setQuery(query); solrQuery.setRows(maxResults); solrQuery.setParam("shards", shardList); solrQuery.addFilterQuery("dcterms_created:[" + dateFormatter.format(startDate) + " TO " + dateFormatter.format(endDate) + "]"); // Construct a field list for highlighting String[] fieldList = new String[(searchLanguages.size() * 2) + 2]; fieldList[0] = "content"; fieldList[1] = "title"; for (int i = 2, j = 0; i < fieldList.length; i += 2, j++) { fieldList[i] = "content-" + searchLanguages.get(j); fieldList[i + 1] = "title-" + searchLanguages.get(j); } // Enable highlighting for the content (and leaving the title for now) solrQuery.setHighlight(true); solrQuery.setHighlightSnippets(2); solrQuery.setParam("hl.fl", fieldList); solrQuery.setParam("f.title.hl.fragsize", "0"); solrQuery.setParam("f.content.hl.fragsize", "100"); for (String language : searchLanguages) { solrQuery.setParam("f.title-" + language + ".hl.fragsize", "0"); solrQuery.setParam("f.content-" + language + ".hl.fragsize", "100"); } for (Entry<String, String> parameter : parameters.entrySet()) { if (parameter.getKey().equalsIgnoreCase("dcterms_coverage") || parameter.getKey().equalsIgnoreCase("dcterms_format")) solrQuery.addFilterQuery(parameter.getKey() + ":(" + parameter.getValue() + ")"); else if (parameter.getKey().equalsIgnoreCase("dcterms_type")) solrQuery.addFilterQuery("-" + parameter.getKey() + ":(" + parameter.getValue() + ")"); else solrQuery.addFilterQuery(parameter.getKey() + ":\"" + parameter.getValue() + "\""); } try { QueryResponse response = solrServer.query(solrQuery); Iterator<SolrDocument> iterator = response.getResults().iterator(); for (int i = 0; i < maxResults && iterator.hasNext(); i++) { SolrDocument document = iterator.next(); // Retrieve the (potentially) highlighted summary String id = (String) document.getFieldValue("id"); String language = (String) document.getFieldValue("dcterms_language"); String author = (String) document.getFieldValue("author"); String sourceId = null; // Simply take the last source ID if (document.getFieldValues("dcterms_coverage") != null && document.getFieldValues("dcterms_coverage").size() > 0) for (Object coverageId : document.getFieldValues("dcterms_coverage")) sourceId = coverageId.toString(); String contentField = StringUtils.hasText(language) ? "content-" + language : "content"; String titleField = StringUtils.hasText(language) ? "title-" + language : "title"; String summary = (String) document.getFieldValue(contentField); if (summary.length() > 300) summary = summary.substring(0, 300) + " …"; if (response.getHighlighting().get(id) != null) { List<String> highlightSnippets = response.getHighlighting().get(id).get(contentField); if (highlightSnippets != null && highlightSnippets.size() > 0) { String fragmentPrefixOne = highlightSnippets.get(0).length() > 99 ? " .. " : ""; summary = fragmentPrefixOne + highlightSnippets.get(0) + fragmentPrefixOne; if (highlightSnippets.size() > 1) { String fragmentSuffixTwo = highlightSnippets.get(1).length() > 99 ? " .. " : ""; summary += highlightSnippets.get(1) + fragmentSuffixTwo; } } } results.add(new SearchResult((String) document.get("url"), (String) document.get(titleField), author, sourceId, summary, (Date) document.get("dcterms_created"), (String) document.get("dcterms_alternative"))); } } catch (SolrServerException e) { logger.error("Could not retrieve Solr results for query '" + query + "'", e); } // Now collect and update the source IDs to their actual sources return adjustSourceIds(results, taxonomyLanguage); }
From source file:com.sindicetech.siren.demo.ncpr.NCPRQuery.java
License:Open Source License
/** * A query that shows how to use the nested query parameter to use Solr's * query parsers on Solr's fields.//from ww w .ja v a2 s . c om */ private SolrQuery getNestedQuery() throws QueryNodeException { ConciseQueryBuilder b = new ConciseQueryBuilder(); String q = b.newTwig("DeviceOwner").with(b.newNode("uri(www.sourcelondon.net)").setAttribute("Website")) .toString(); final SolrQuery query = new SolrQuery(); query.setQuery(q); query.setParam("nested", "{!lucene} ChargeDeviceName:university"); return query; }
From source file:com.sindicetech.siren.solr.facet.TestSirenFacetProcessorFactory.java
License:Open Source License
public void testCustomDatatypeField() throws Exception { String json = "{\"rating\": {\"_datatype_\": \"http://www.w3.org/2001/XMLSchema#double\", \"_value_\":\"5.4\"}}"; IndexSchema schema = h.getCore().getLatestSchema(); SolrInputDocument d = processAdd("generate-facets-processor", doc(f("id", "1"), f("json", json))); assertNotNull(d);//from www.j a v a2s . c o m schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("double.json.rating")); assertEquals("tdouble", schema.getFieldType("double.json.rating").getTypeName()); assertTrue((5.4 - (double) d.getFieldValue("double.json.rating")) < 0.01); getWrapper().add(d); // add so that we can test facets by querying this.commit(); SolrQuery q = new SolrQuery(); q.setRequestHandler("keyword"); q.setParam("nested", "{!lucene} *:*"); q.setFacet(true); q.addFacetField("double.json.rating"); QueryResponse r = getWrapper().getServer().query(q); // we know there is only one facet field with one value assertEquals(1, r.getFacetFields().get(0).getValues().get(0).getCount()); json = "{\"rating\": {\"_datatype_\": \"http://www.w3.org/2001/XMLSchema#float\", \"_value_\":\"-8.4\"}}"; schema = h.getCore().getLatestSchema(); d = processAdd("generate-facets-processor", doc(f("id", "2"), f("json", json))); assertNotNull(d); schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("double.json.rating")); assertEquals("tdouble", schema.getFieldType("double.json.rating").getTypeName()); assertTrue((-8.4 + (double) d.getFieldValue("double.json.rating")) < 0.01); getWrapper().add(d); // add so that we can test facets by querying this.commit(); r = getWrapper().getServer().query(q); // there is only one facet field with two different values each with a single count assertEquals(1, r.getFacetFields().get(0).getValues().get(0).getCount()); assertEquals(1, r.getFacetFields().get(0).getValues().get(1).getCount()); }
From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java
License:Open Source License
/** * Check that a UUID is generated for JSON documents with no 'id' attribute. *//*from w ww.ja va 2 s . c o m*/ @Test public void testJsonWithNoId() throws IOException, SolrServerException { String input = "{ \"aaa\" : \"bbb\" }"; this.sendUpdateRequest(input); this.commit(); SolrQuery query = new SolrQuery(); query.setParam("nested", "{!lucene} *:*"); query.setRequestHandler("tree"); query.setFields(IdFieldMapper.INPUT_FIELD); SolrDocumentList results = this.search(query); assertEquals(1, results.getNumFound()); assertNotNull(results.get(0).getFieldValue(IdFieldMapper.INPUT_FIELD)); }
From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java
License:Open Source License
/** * Check that the value type of the id field do not have impact on the indexing. *//*from w ww. j av a 2 s . co m*/ @Test public void testJsonWithNumericId() throws IOException, SolrServerException { String input1 = "{ \"id\" : 1, \"aaa\" : null }"; String input2 = "{ \"id\" : \"2\", \"aaa\" : null }"; this.sendUpdateRequest(input1); this.sendUpdateRequest(input2); this.commit(); SolrQuery query = new SolrQuery(); query.setParam("nested", "{!lucene} id:1"); query.setRequestHandler("tree"); long found = this.search(query).getNumFound(); assertEquals(1, found); query = new SolrQuery(); query.setParam("nested", "{!lucene} id:2"); query.setRequestHandler("tree"); found = this.search(query).getNumFound(); assertEquals(1, found); }
From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java
License:Open Source License
/** * Test the optional mappers. The fields 'bbb' and 'ddd' are not defined, but the String and Boolean * value types are defined. They should be indexed. *//* w ww. ja va2 s.c o m*/ @Test public void testOptionalMapper() throws QueryNodeException, IOException, SolrServerException { String input = "{ \"id\" : \"1\", \"aaa\" : null, \"bbb\" : \"ccc\", \"ddd\" : false }"; this.sendUpdateRequest(input); this.commit(); SolrQuery query = new SolrQuery(); query.setParam("nested", "{!lucene} bbb:ccc"); query.setRequestHandler("tree"); long found = this.search(query).getNumFound(); assertEquals(1, found); query = new SolrQuery(); query.setParam("nested", "{!lucene} ddd:false"); query.setRequestHandler("tree"); found = this.search(query).getNumFound(); assertEquals(1, found); }
From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java
License:Open Source License
/** * Test the default mapper and the null field type. The field 'bbb' with an Integer value should * not match any mapper, and therefore be processed by the default mapper. The default mapper * is configured with the null field type which indicates to ignore the field. Therefore the * field 'bbb' should not be indexed and the search should throw an exception. *///from ww w. j a va2 s . c o m @Test(expected = SolrException.class) public void testDefaultNullMapper() throws QueryNodeException, IOException, SolrServerException { String input = "{ \"id\" : \"1\", \"bbb\" : 1 }"; this.sendUpdateRequest(input); this.commit(); SolrQuery query = new SolrQuery(); query.setParam("nested", "{!lucene} bbb:1"); query.setRequestHandler("tree"); this.search(query); }
From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java
License:Open Source License
/** * Test the null field type. The path aaa.bbb is configured with the null field type which indicates to ignore the * field. Therefore the field 'aaa.bbb' should not be indexed and the search should throw an exception. *//* www . j av a 2s .co m*/ @Test(expected = SolrException.class) public void testNullMapper() throws QueryNodeException, IOException, SolrServerException { String input = "{ \"id\" : \"1\", \"aaa\" : { \"bbb\" : \"ccc\" } }"; this.sendUpdateRequest(input); this.commit(); SolrQuery query = new SolrQuery(); query.setParam("nested", "{!lucene} aaa.bbb:ccc"); query.setRequestHandler("tree"); this.search(query); }
From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java
License:Open Source License
/** * Test mixed array. The String and Double type is defined, while the Integer is undefined. Integer will be ignored * and not indexed. The Double value will be indexed as a string. */// w ww . j a v a 2 s .c om @Test public void testMixedTypeArray() throws QueryNodeException, IOException, SolrServerException { String input = "{ \"id\" : \"1\", \"aaa\" : null, \"bbb\" : [\"ccc\", 2, 3.1] }"; this.sendUpdateRequest(input); this.commit(); SolrQuery query = new SolrQuery(); query.setParam("nested", "{!lucene} bbb:ccc"); query.setRequestHandler("tree"); long found = this.search(query).getNumFound(); assertEquals(1, found); query = new SolrQuery(); query.setParam("nested", "{!lucene} bbb:2"); query.setRequestHandler("tree"); found = this.search(query).getNumFound(); assertEquals(0, found); query = new SolrQuery(); query.setParam("nested", "{!lucene} bbb:3.1"); query.setRequestHandler("tree"); found = this.search(query).getNumFound(); assertEquals(1, found); }