List of usage examples for org.apache.solr.client.solrj SolrQuery setParam
public SolrQuery setParam(String name, boolean value)
From source file:com.tamingtext.solr.SolrJTest.java
License:Apache License
public void doTest() throws IOException, SolrServerException { log.info("--------------------test()---------------------"); String description = "In a stunning defeat for rabbits " + "everywhere, Fred Q. Tortoise capitalized on John " + "Hare's brazen laziness to win the annual Tortoise-Hare 5K Road Race. " + "The lazy Mr. Hare, in hopes of saving face, immediately " + "acccused Mr. Tortoise of doping, in what has become an " + "all too familiar scene in sporting events today. " + "Fans everywhere were appalled by the revelations, with " + "allegiances falling roughly along species lines."; //<start id="solrj"/> SolrServer solr = new CommonsHttpSolrServer(new URL("http://localhost:" + port + "/solr"));//<co id="co.solrj.server"/> SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "http://tortoisehare5k.tamingtext.com");//<co id="co.solrj.unique"/> doc.addField("mimeType", "text/plain"); doc.addField("title", "Tortoise beats Hare! Hare wants rematch.", 5);//<co id="co.solrj.title"/> Date now = new Date(); doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now));//<co id="co.solrj.date"/> doc.addField("description", description); doc.addField("categories_t", "Fairy Tale, Sports");//<co id="co.solrj.dynamic.field"/> solr.add(doc);//<co id="co.solrj.add"/> solr.commit();//<co id="co.solrj.commit"/> /*/*from w w w . ja v a 2 s. c o m*/ <calloutlist> <callout arearefs="co.solrj.server"><para>Create a HTTP-based Solr Server connection.</para></callout> <callout arearefs="co.solrj.unique"><para>The schema used for this instance of Solr requires a unique field named "id"</para></callout> <callout arearefs="co.solrj.title"><para>Add a Title field to our document and boost it to be 5 times as important as the other fields</para></callout> <callout arearefs="co.solrj.date"><para>Dates must be formatted in a specific way for Solr.</para></callout> <callout arearefs="co.solrj.dynamic.field"><para>A dynamic field allows for the addition of unknown fields to Solr. The "_t" tells Solr this should be treated as a text field.</para></callout> <callout arearefs="co.solrj.add"><para>Send the newly created document to Solr. Solr takes care of creating a correctly formatted XML message and sending it to Solr using Apache Jakarta Commons HTTPClient.</para></callout> <callout arearefs="co.solrj.commit"><para>After you have added all your documents and wish to make them available for searching, send a commit message to Solr</para></callout> </calloutlist> */ //<end id="solrj"/> //Add some more docs doc = new SolrInputDocument();//<co id="co.solrj.doc"/> doc.addField("id", "http://redfox.tamingtext.com");//<co id="co.solrj.unique"/> doc.addField("mimeType", "text/plain");//<co id="co.solrj.mime"/> doc.addField("title", "Red Fox mocks Lazy Brown Dogs!", 5);//<co id="co.solrj.title"/> now = new Date(); doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now)); doc.addField("description", "Once again, the Red Fox outshined the" + " lazy Brown Dogs to win the vaunted Tally Ho Cup, in what" + " has become an annual ritual. The lazy brown " + "dogs just don't seem to have the drive that Red Fox does." + " The lazy Brown Dogs vow to avenge their latest defeat, " + "but the group's supporters claim to have heard it all before."); doc.addField("categories_t", "Fairy Tale, Sports"); solr.add(doc);//<co id="co.solrj.add"/> doc = new SolrInputDocument();//<co id="co.solrj.doc"/> doc.addField("id", "http://maryLambs.tamingtext.com");//<co id="co.solrj.unique"/> doc.addField("mimeType", "text/plain");//<co id="co.solrj.mime"/> now = new Date(10000); doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now)); doc.addField("title", "Mary's Little Lamb Stolen!.", 5);//<co id="co.solrj.title"/> doc.addField("description", "In an affront to all that is good and" + " decent in this world, criminals made off with Mary's " + "little Lamb in a late night raid on Mary's farm." + " Early suspects include a Ms. Bo Peep who weeks earlier " + "reported losing several sheep. Police suspect Ms. Peep " + "was going to use the lamb to bring her flock's numbers back up." + " The spokesman for Ms. Peep declined comment at this " + "time. Mary, however, was outraged and promises revenge " + "on the insensitive clod who stole her precious animals."); doc.addField("categories_t", "Fairy Tale, crime, sheep"); solr.add(doc); //add in some random other docs Collection<SolrInputDocument> docs = new HashSet<SolrInputDocument>(); for (int i = 0; i < 100; i++) { doc = new SolrInputDocument(); doc.addField("id", "http://www.tamingtext.com/" + i + ".html"); doc.addField("mimeType", "text/html"); doc.addField("title", "This is title " + i); now = new Date(150 * i + i + 20);//create some dates doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now)); doc.addField("description", "This is description number: " + i + " of something that is very important."); docs.add(doc); } solr.add(docs); solr.commit(); solr.optimize(); //<start id="solrj-search-1"/> SolrQuery queryParams = new SolrQuery();//<co id="solrj-search.co.query"/> queryParams.setFields("description", "title");//<co id="solrj-search.co.fields"/> queryParams.setQuery("description:win OR description:all");//<co id="solrj-search.co.terms"/> queryParams.setRows(10); QueryResponse response = solr.query(queryParams);//<co id="solrj-search.co.process"/> assertTrue("response is null and it shouldn't be", response != null); SolrDocumentList documentList = response.getResults(); System.out.println("Docs: " + documentList); assertTrue("response.getResults() Size: " + documentList.size() + " is not: " + 3, documentList.size() == 3); /* <calloutlist> <callout arearefs="solrj-search.co.query"><para>A <classname>SolrQuery</classname> is a easy-to-use container for creating the most common types of queries.</para></callout> <callout arearefs="solrj-search.co.fields"><para>Set the names of the Fields to be returned in the documents.</para></callout> <callout arearefs="solrj-search.co.terms"><para>Set the terms to search. The "OR" is a boolean operator which allows one or the other or both to be present in the query.</para></callout> <callout arearefs="solrj-search.co.process"><para>Submit the query to Solr and get back a <classname>QueryResponse</classname> which contains the results of the search.</para></callout> </calloutlist> */ //<end id="solrj-search-1"/> //<start id="solrj-search-dismax"/> queryParams.setQuery("lazy"); queryParams.setParam("defType", "dismax");//<co id="solrj-search.co.dismax"/> queryParams.set("qf", "title^3 description^10");//<co id="sorlj-search.co.qf"/> System.out.println("Query: " + queryParams); response = solr.query(queryParams); assertTrue("response is null and it shouldn't be", response != null); documentList = response.getResults(); assertTrue("documentList Size: " + documentList.size() + " is not: " + 2, documentList.size() == 2); /* <calloutlist> <callout arearefs="solrj-search.co.dismax"><para>Tell Solr to use the <classname>DisMax</classname> Query Parser (named dismax in solrconfig.xml). </para></callout> <callout arearefs="sorlj-search.co.qf"><para>The DisMax parser will search across the fields given by the "qf" parameter and boosts the terms accordingly.</para></callout> </calloutlist> */ //<end id="solrj-search-dismax"/> //<start id="solrj-search-facets"/> queryParams = new SolrQuery(); queryParams.setQuery("description:number"); queryParams.setRows(10); queryParams.setFacet(true);//<co id="solrj-search-facets-facetOn"/> queryParams.set("facet.field", "date");//<co id="solrj-search-facets-date"/> response = solr.query(queryParams); assertTrue("response is null and it shouldn't be", response != null); System.out.println("Query: " + queryParams); documentList = response.getResults(); assertTrue("documentList Size: " + documentList.size() + " is not: " + 10, documentList.size() == 10); System.out.println("Facet Response: " + response.getResponse());//<co id="solrj-search-facets-print"/> /* <calloutlist> <callout arearefs="solrj-search-facets-facetOn"><para>Turn on faceting for this query</para></callout> <callout arearefs="solrj-search-facets-date"><para>Specify the Field to facet on</para></callout> <callout arearefs="solrj-search-facets-print"><para>Print out the facet information</para></callout> </calloutlist> */ //<end id="solrj-search-facets"/> //<start id="solrj-search-more-like-this"/> queryParams = new SolrQuery(); queryParams.setQueryType("/mlt");//<co id="solrj-search.co.mlt"/> queryParams.setQuery("description:number"); queryParams.set("mlt.match.offset", "0");//<co id="solrj-search.co.mlt.off"/> queryParams.setRows(1); queryParams.set("mlt.fl", "description, title");//<co id="solrj-search.co.mlt.fl"/> response = solr.query(queryParams); assertTrue("response is null and it shouldn't be", response != null); SolrDocumentList results = (SolrDocumentList) response.getResponse().get("match"); assertTrue("results Size: " + results.size() + " is not: " + 1, results.size() == 1); /* <calloutlist> <callout arearefs="solrj-search.co.mlt"><para>Create a "MoreLikeThis" search to find similar documents to the specified document.</para></callout> <callout arearefs="solrj-search.co.mlt.fl"><para>Specify the field to use to generate the "MoreLikeThis" query.</para></callout> <callout arearefs="solrj-search.co.mlt.off"><para>Specify which document in the original results to use as the "similar" document. </para></callout> </calloutlist> */ //<end id="solrj-search-more-like-this"/> log.info("--------------------end test()---------------------"); }
From source file:com.temenos.interaction.commands.solr.SolrSearchCommand.java
License:Open Source License
/** * This method will add Shards to the Query * @param query/*from w ww . j a v a2s . c om*/ * @param queryParams */ private void addShards(SolrQuery query, MultivaluedMap<String, String> queryParams) { String shards = queryParams.getFirst(SolrConstants.SOLR_SHARDS_KEY); if (shards != null && !shards.isEmpty()) { query.setParam("shards", shards); // Check if user has specified shards.tolerant, add if available String shardsTolerant = queryParams.getFirst(SolrConstants.SOLR_SHARDS_TOLERANT_KEY); if (shardsTolerant != null && !shardsTolerant.isEmpty()) { query.setParam(SolrConstants.SOLR_SHARDS_TOLERANT_KEY, shardsTolerant); } } }
From source file:com.thinkbiganalytics.search.SolrSearchService.java
License:Apache License
private QueryResponse executeSearch(String query, int size, int start) { final String COLLECTION_LIST = "kylo-data," + SearchIndex.DATASOURCES; //Solr admin to configure beforehand final String ALL_FIELDS = "*"; final String BOLD_HIGHLIGHT_START = "<font style='font-weight:bold'>"; final String BOLD_HIGHLIGHT_END = "</font>"; SolrQuery solrQuery = new SolrQuery(); solrQuery.set("q", query); solrQuery.setRows(size);/*from w ww .j a v a2 s .co m*/ solrQuery.setStart(start); solrQuery.setParam("collection", COLLECTION_LIST); solrQuery.setHighlight(true); solrQuery.set("hl.fl", ALL_FIELDS); solrQuery.setHighlightSimplePre(BOLD_HIGHLIGHT_START); solrQuery.setHighlightSimplePost(BOLD_HIGHLIGHT_END); solrQuery.setHighlightRequireFieldMatch(false); try { return client.query("kylo-data", solrQuery); } catch (SolrServerException | IOException e) { throw new RuntimeException("Error encountered during search."); } }
From source file:com.tripod.solr.query.StandardSolrQueryTransformer.java
License:Apache License
@Override public SolrQuery transform(final Q query) { final SolrQuery solrQuery = new SolrQuery(query.getQuery()); solrQuery.setStart(query.getOffset()); solrQuery.setRows(query.getRows());/* www . ja v a 2s. c o m*/ solrQuery.setParam("q.op", query.getDefaultOperator().name()); if (query.getReturnFields() != null) { query.getReturnFields().stream().forEach(f -> solrQuery.addField(f.getName())); } if (query.getHighlightFields() != null && !query.getHighlightFields().isEmpty()) { solrQuery.setHighlight(true); query.getHighlightFields().stream().forEach(hf -> solrQuery.addHighlightField(hf.getName())); } if (query.getFacetFields() != null) { query.getFacetFields().stream().forEach(ff -> solrQuery.addFacetField(ff.getName())); } if (query.getSorts() != null) { for (Sort sort : query.getSorts()) { SolrQuery.ORDER solrOrder = sort.getSortOrder() == SortOrder.ASC ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc; SolrQuery.SortClause sortClause = new SolrQuery.SortClause(sort.getField().getName(), solrOrder); solrQuery.addSort(sortClause); } } if (query.getFilterQueries() != null) { query.getFilterQueries().stream().forEach(fq -> solrQuery.addFilterQuery(fq)); } if (query.getParams() != null) { query.getParams().entrySet().stream().forEach(e -> solrQuery.add(e.getKey(), e.getValue())); } return solrQuery; }
From source file:com.villemos.ispace.solr.SolrProducer.java
License:Open Source License
private void configureQuery(SolrQuery query) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { /** Set the default values. May be overridden by later settings. */ query.setRows(endpoint.getRows());/*w w w .j av a 2s .com*/ query.setSortField(endpoint.getSortField(), endpoint.getSortOrder()); /** We per default always set highlighting. */ query.setHighlight(true).setHighlightSnippets(1); query.setParam("hl.fl", "withRawText"); /** Configure facets. */ query.setFacet(endpoint.getFacets()); if (endpoint.getFacets() == true) { query.setQuery(endpoint.getQuery()); query.setFacetSort(endpoint.getFacetsort()); query.setFacetLimit(endpoint.getFacetlimit()); query.setFacetPrefix(endpoint.getFacetprefix()); query.setFacetMinCount(endpoint.getMinCount()); query.setFacetMissing(endpoint.isFacetMissing()); } query.addFacetField(endpoint.getFacetField()); }
From source file:com.yahoo.ycsb.db.solr.SolrClient.java
License:Open Source License
/** * Perform a range scan for a set of records in the database. Each field/value pair from the * result will be stored in a HashMap.//from w ww .jav a 2 s.c o m * * @param table * The name of the table * @param startkey * The record key of the first record to read. * @param recordcount * The number of records to read * @param fields * The list of fields to read, or null for all of them * @param result * A Vector of HashMaps, where each HashMap is a set field/value pairs for one record * @return Zero on success, a non-zero error code on error. See this class's description for a * discussion of error codes. */ @Override public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) { try { Boolean returnFields = false; String[] fieldList = null; if (fields != null) { returnFields = true; fieldList = fields.toArray(new String[fields.size()]); } SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.setParam("fq", "id:[ " + startkey + " TO * ]"); if (returnFields) { query.setFields(fieldList); } query.setRows(recordcount); final QueryResponse response = client.query(table, query); SolrDocumentList results = response.getResults(); HashMap<String, ByteIterator> entry; for (SolrDocument hit : results) { entry = new HashMap<String, ByteIterator>((int) results.getNumFound()); for (String field : hit.getFieldNames()) { entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field)))); } result.add(entry); } return checkStatus(response.getStatus()); } catch (IOException | SolrServerException e) { e.printStackTrace(); } return Status.ERROR; }
From source file:com.yahoo.ycsb.db.solr6.SolrClient.java
License:Open Source License
/** * Perform a range scan for a set of records in the database. Each field/value pair from the * result will be stored in a HashMap.// w w w . j a va 2 s . c om * * @param table * The name of the table * @param startkey * The record key of the first record to read. * @param recordcount * The number of records to read * @param fields * The list of fields to read, or null for all of them * @param result * A Vector of HashMaps, where each HashMap is a set field/value pairs for one record * @return Zero on success, a non-zero error code on error. See this class's description for a * discussion of error codes. */ @Override public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) { try { Boolean returnFields = false; String[] fieldList = null; if (fields != null) { returnFields = true; fieldList = fields.toArray(new String[fields.size()]); } SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.setParam("fq", "id:[ " + startkey + " TO * ]"); if (returnFields) { query.setFields(fieldList); } query.setRows(recordcount); final QueryResponse response = client.query(table, query); SolrDocumentList results = response.getResults(); HashMap<String, ByteIterator> entry; for (SolrDocument hit : results) { entry = new HashMap<>((int) results.getNumFound()); for (String field : hit.getFieldNames()) { entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field)))); } result.add(entry); } return checkStatus(response.getStatus()); } catch (IOException | SolrServerException e) { e.printStackTrace(); } return Status.ERROR; }
From source file:com.zb.app.external.lucene.solr.client.SolrClient.java
License:Open Source License
/** * solr/*ww w . j a v a 2 s . c om*/ * * @param corename ????? * @param returnType Class * @param solrQuery {@link SolrQueryConvert} * @return */ @SuppressWarnings("unchecked") public <T> List<T> querys(String corename, final Class<T> returnType, final SolrQuery solrQuery) { // solrQuery.setParam("hl", "true"); // highlighting solrQuery.setParam("hl.fl", "lTile", "lMode", "lYesItem", "lNoItem", "lChildren", "lShop", "lExpenseItem", "lPreseItem", "rContent", "rCar"); solrQuery.setHighlightSimplePre("<font color=\"red\">"); solrQuery.setHighlightSimplePost("</font>"); final SolrDocumentList list = new SolrDocumentList(); final HttpSolrServer server = getOrCreateSolrServer(corename); exec(new Executor() { public Result exec() throws SolrServerException, IOException { QueryResponse query = null; if (solrQuery.toString().length() > MAX_URL_LENGTH) { query = server.query(solrQuery, SolrRequest.METHOD.POST); } else { query = server.query(solrQuery, SolrRequest.METHOD.GET); } SolrDocumentList documents = query.getResults(); Map<String, Map<String, List<String>>> map = query.getHighlighting(); for (SolrDocument document : documents) { document.setField("lTile", map.get(document.getFieldValue("id")).get("lTile")); list.add(document); } return Result.success(); } }); return (List<T>) toBeanList(list, returnType); }
From source file:cz.incad.vdk.client.tools.Search.java
License:Open Source License
public JSONArray getSuggest() { try {//from w w w.j a va2 s .c o m String q = req.getParameter("term"); SolrQuery query = new SolrQuery(); if (q == null || q.equals("")) { return new JSONArray(); } query.setParam(CommonParams.QT, "/terms"); query.setTerms(true); query.setTermsPrefix(q.toUpperCase()); query.setTermsLowerInclusive(true); query.addTermsField("title_suggest"); JSONArray ja = new JSONObject(IndexerQuery.terms(query)).getJSONObject("terms") .getJSONArray("title_suggest"); JSONArray ret = new JSONArray(); for (int i = 0; i < ja.length(); i = i + 2) { String val = ja.getString(i); ret.put(new JSONObject().put("value", val).put("label", val.substring(val.indexOf("##") + 2))); } return ret; } catch (IOException ex) { Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex); return new JSONArray(); } }
From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java
License:Apache License
/** * Gets values to autocomplete for the input string. * The autocomplete feature works for multivalued fields and is based on a highlighting trick. * See http://solr.pl/en/2013/02/25/autocomplete-on-multivalued-fields-using-highlighting/ * @param keywordStart/*from ww w .jav a2 s.com*/ * @return Autocomplete values. * @throws SolrServerException */ public Set<String> getTextToAutocomplete(String keywordStart) throws SolrServerException { SolrQuery query = new SolrQuery(); query.setQuery(IndexField.AUTOCOMPLETE.getValue() + ":" + keywordStart); query.setFields(IndexField.AUTOCOMPLETE.getValue()); query.setHighlight(true); query.setParam("hl.fl", IndexField.AUTOCOMPLETE.getValue()); query.setHighlightSimplePre(""); query.setHighlightSimplePost(""); query.setRows(FullTextSearchUtils.AUTOCOMPLETE_ROWS); Map<String, Integer> map = new TreeMap<String, Integer>(); QueryResponse response = solrServer.query(query); Set<String> foundIds = response.getHighlighting().keySet(); for (String id : foundIds) { List<String> resultsPerDocument = response.getHighlighting().get(id) .get(IndexField.AUTOCOMPLETE.getValue()); if (resultsPerDocument != null) { for (String result : resultsPerDocument) { String resultValue; int resultFrequency; int delimiterPosition = result.lastIndexOf('#'); if (delimiterPosition == -1) { // autocomplete phrase was copied from title resultValue = result; resultFrequency = 0; } else { resultValue = result.substring(0, delimiterPosition); try { resultFrequency = Integer .valueOf(result.substring(delimiterPosition + 1, result.length())); } catch (NumberFormatException e) { resultFrequency = 0; } } map.put(resultValue.toLowerCase(), resultFrequency); } } if (map.size() == FullTextSearchUtils.AUTOCOMPLETE_ROWS) { break; } } map = sortByValue(map); return map.keySet(); }