List of usage examples for org.apache.solr.client.solrj SolrQuery setStart
public SolrQuery setStart(Integer start)
From source file:org.segrada.search.solr.SolrSearchEngine.java
License:Apache License
@Override public PaginationInfo<SearchHit> search(String searchTerm, Map<String, String> filters) { // to avoid NPEs if (filters == null) filters = new HashMap<>(); // set defaults int page = 1; int entriesPerPage = 20; try {//from ww w . j a v a 2 s .co m // Parse a simple query that searches for "text": MultiFieldQueryParser parser; String[] containFields; // do we have a filter to contain to certain fields? if (filters.containsKey("fields")) { String fields = filters.get("fields"); if (fields.isEmpty()) containFields = new String[] { this.title, this.subTitles, this.content }; else if (fields.equalsIgnoreCase(this.title)) containFields = new String[] { this.title }; else if (fields.equalsIgnoreCase(this.subTitles)) containFields = new String[] { this.subTitles }; else if (fields.equalsIgnoreCase(this.content)) containFields = new String[] { this.content }; else if (fields.equalsIgnoreCase("allTitles")) containFields = new String[] { this.title, this.subTitles }; else throw new RuntimeException("fields-Filter " + fields + " is not known."); } else containFields = new String[] { this.title, this.subTitles, this.content }; parser = new MultiFieldQueryParser(Version.LUCENE_47, containFields, analyzer); // which operator do we use? parser.setDefaultOperator(QueryParser.Operator.AND); if (filters.containsKey("operator")) { String operator = filters.get("operator"); if (operator.equalsIgnoreCase("or")) parser.setDefaultOperator(QueryParser.Operator.OR); else if (!operator.isEmpty() && !operator.equalsIgnoreCase("and")) throw new RuntimeException("operator-Filter " + operator + " is not and/or."); } // filters for query SolrQuery query = new SolrQuery(); // class filter if (filters.containsKey("class") && !filters.get("class").isEmpty()) { // multiple classes? String[] classes = filters.get("class").split(","); // single class if (classes.length <= 1) { query.addFilterQuery(this.className, filters.get("class")); } else { // multiple classes StringBuilder chained = new StringBuilder("("); for (int i = 0; i < classes.length; i++) { if (i > 0) chained.append(" OR "); chained.append("className:").append(classes[i].trim()); } query.addFilterQuery(this.className, chained + ")"); } } // tag filter if (filters.containsKey("tags") && !filters.get("tags").isEmpty()) { // split tags into array String[] tags = filters.get("tags").split(","); BooleanQuery booleanQuery = new BooleanQuery(); for (String tagLocal : tags) { booleanQuery.add(new TermQuery(new Term("tag", tagLocal.trim())), BooleanClause.Occur.SHOULD); } query.addFilterQuery(this.tag, booleanQuery.toString()); } // define query Query queryTerm = null; if (searchTerm != null) queryTerm = parser.parse(searchTerm); if (queryTerm == null) queryTerm = new MatchAllDocsQuery(); // fallback to match all documents query.setQuery(queryTerm.toString()); // get hits per page if (filters.containsKey("limit")) { try { entriesPerPage = Integer.valueOf(filters.get("limit")); if (entriesPerPage <= 0 || entriesPerPage > 1000) entriesPerPage = 20; } catch (NumberFormatException e) { logger.warn("Could not parse limit " + filters.get("limit") + " to integer", e); } } // get page number if (filters.containsKey("page")) { try { page = Integer.valueOf(filters.get("page")); } catch (NumberFormatException e) { logger.warn("Could not parse page " + filters.get("page") + " to integer", e); } } // calculate start index int startIndex = (page - 1) * entriesPerPage; query.setStart(startIndex); query.setRows(entriesPerPage); query.setFields("*", "score"); // define highlighting query.setHighlight(true); query.addHighlightField(this.content); query.setHighlightFragsize(18); query.setHighlightSnippets(10); query.setHighlightSimplePre("<b>"); query.setHighlightSimplePost("</b>"); // do query QueryResponse response = solr.query(query); SolrDocumentList results = response.getResults(); // how many pages do we have? int pages = (int) (results.getNumFound() / entriesPerPage + 1); // cycle trough hits List<SearchHit> hits = new ArrayList<>(); for (SolrDocument doc : results) { SearchHit searchHit = createHitFromDocument(doc); // add score Object score = doc.get("score"); if (score != null && score instanceof Float) searchHit.setRelevance((float) score); // get highlighted components if (searchTerm != null && response.getHighlighting().get(searchHit.getId()) != null) { List<String> fragments = response.getHighlighting().get(searchHit.getId()).get(this.content); if (fragments != null) { String[] bestFragments = new String[fragments.size() > 10 ? 10 : fragments.size()]; for (int i = 0; i < bestFragments.length; i++) bestFragments[i] = fragments.get(i); searchHit.setHighlightText(bestFragments); } } // add hit hits.add(searchHit); } // return pagination info return new PaginationInfo<>(page, pages, (int) results.getNumFound(), entriesPerPage, hits); } catch (Throwable e) { logger.error("Error in search.", e); } // return empty list result in order to avoid NPEs return new PaginationInfo<>(page, 1, 0, entriesPerPage, new ArrayList<>()); }
From source file:org.shanghai.solr.SolrClient.java
License:Apache License
/** Note: solr is not designed for this use case; looping over all results only makes sense for small cores. If sharding is involved, queue up to go insane. */// w ww . j av a 2 s.c om public String[] getIdentifiers(final int off, final int limit) { String[] result = new String[limit]; int found = 0; int count = 0; int fetchSize = limit; //int fetchSize = 1000; SolrQuery query = new SolrQuery(); query.setQuery("id:*"); query.setRows(fetchSize); try { QueryResponse rsp = solrPost.server.query(query); long offset = 0; long totalResults = rsp.getResults().getNumFound(); while (offset < totalResults) { query.setStart((int) offset); // requires an int? wtf? query.setRows(fetchSize); for (SolrDocument doc : solrPost.server.query(query).getResults()) { count++; if (off < count && found < limit) { result[found] = (String) doc.getFieldValue("id"); if (result[found].length() == 0) { log(" empty : [" + count + "]"); result[found] = null; } found++; } } if (off + limit < count) break; offset += fetchSize; } } catch (SolrServerException e) { log(e); } return result; }
From source file:org.sleuthkit.autopsy.keywordsearch.LuceneQuery.java
License:Open Source License
/** * Perform the query and return results of unique files. * * @param snippets True if results should have a snippet * @return list of ContentHit objects. One per file with hit (ignores * multiple hits of the word in the same doc) * @throws NoOpenCoreException/*from www. j a va 2 s.c om*/ */ private List<KeywordHit> performLuceneQuery(boolean snippets) throws NoOpenCoreException { List<KeywordHit> matches = new ArrayList<>(); boolean allMatchesFetched = false; final Server solrServer = KeywordSearch.getServer(); SolrQuery q = createAndConfigureSolrQuery(snippets); // cycle through results in sets of MAX_RESULTS for (int start = 0; !allMatchesFetched; start = start + MAX_RESULTS) { q.setStart(start); try { QueryResponse response = solrServer.query(q, METHOD.POST); SolrDocumentList resultList = response.getResults(); // objectId_chunk -> "text" -> List of previews Map<String, Map<String, List<String>>> highlightResponse = response.getHighlighting(); // get the unique set of files with hits Set<SolrDocument> uniqueSolrDocumentsWithHits = filterDuplicateSolrDocuments(resultList); allMatchesFetched = start + MAX_RESULTS >= resultList.getNumFound(); SleuthkitCase sleuthkitCase; try { sleuthkitCase = Case.getCurrentCase().getSleuthkitCase(); } catch (IllegalStateException ex) { //no case open, must be just closed return matches; } for (SolrDocument resultDoc : uniqueSolrDocumentsWithHits) { KeywordHit contentHit; try { contentHit = createKeywordtHit(resultDoc, highlightResponse, sleuthkitCase); } catch (TskException ex) { return matches; } matches.add(contentHit); } } catch (NoOpenCoreException ex) { logger.log(Level.WARNING, "Error executing Lucene Solr Query: " + keywordString, ex); //NON-NLS throw ex; } catch (KeywordSearchModuleException ex) { logger.log(Level.WARNING, "Error executing Lucene Solr Query: " + keywordString, ex); //NON-NLS } } return matches; }
From source file:org.springframework.data.solr.core.SolrTemplate.java
License:Apache License
@Override public long count(final SolrDataQuery query) { Assert.notNull(query, "Query must not be 'null'."); return execute(new SolrCallback<Long>() { @Override/*from w w w . j av a 2 s . c o m*/ public Long doInSolr(SolrServer solrServer) throws SolrServerException, IOException { SolrQuery solrQuery = queryParsers.getForClass(query.getClass()).constructSolrQuery(query); solrQuery.setStart(0); solrQuery.setRows(0); return solrServer.query(solrQuery).getResults().getNumFound(); } }); }
From source file:org.teiid.translator.solr.SolrQueryExecution.java
License:Open Source License
public void nextBatch() throws TranslatorException { SolrQuery query = this.visitor.getSolrQuery(); if (!this.visitor.isLimitInUse()) { query.setStart(this.offset); query.setRows(this.executionContext.getBatchSize()); }//from w ww . jav a 2s . c om QueryResponse queryResponse = connection.query(this.visitor.getSolrQuery()); SolrDocumentList docList = queryResponse.getResults(); this.resultSize = docList.getNumFound(); this.resultsItr = docList.iterator(); }
From source file:org.topicquests.solr.Solr3Client.java
License:Apache License
/** * Run a query based on <code>queryString</code> * @param queryString/*from ww w. j a v a 2 s . c om*/ * @param start TODO * @param count TODO * @return NamedList<Object> in result or error string */ public IResult runQuery(String queryString, int start, int count) { System.out.println("Solr3Client.runQuery- " + queryString + " " + start + " " + count); IResult result = new ResultPojo(); SolrQuery parameters = new SolrQuery(); parameters.set("q", queryString); parameters.setStart(start); if (count > -1) parameters.setRows(count); //force result as JSON // parameters.set("wt", "json"); System.out.println("Solr3Client.runQuery-1 " + parameters.toString()); try { QueryResponse x = server.query(parameters); System.out.println("ZZZZ " + x.getResults()); result.setResultObject(x.getResults()); } catch (Exception e) { log.logError("SolrClient3.runQuery " + e.getMessage() + " " + queryString, e); result.addErrorString(e.getMessage()); } return result; }
From source file:org.topicquests.solr.Solr4Client.java
License:Apache License
/** * Run a query based on <code>queryString</code> * @param queryString/*from www . ja v a2 s .c om*/ * @param start TODO * @param count TODO * @return NamedList<Object> in result or error string */ public IResult runQuery(String queryString, int start, int count) { System.out.println("Solr4Client.runQuery- " + queryString + " " + start + " " + count); IResult result = new ResultPojo(); SolrQuery parameters = new SolrQuery(); parameters.set("q", queryString); parameters.setStart(start); if (count > -1) parameters.setRows(count); //force result as JSON // parameters.set("wt", "json"); System.out.println("Solr4Client.runQuery-1 " + parameters.toString()); try { QueryResponse x = server.query(parameters); // System.out.println("Solr3Client.runQuery "+x.getStatus()); // System.out.println("XXXX "+x.getHeader()); // System.out.println("YYYY "+x.getResponse()); System.out.println("ZZZZ " + x.getResults()); result.setResultObject(x.getResults()); } catch (Exception e) { //TODO log the error e.printStackTrace(); result.addErrorString(e.getMessage()); } return result; }
From source file:org.topicquests.solr.SolrCloudClient.java
License:Apache License
/** * Run a query based on <code>queryString</code> * @param queryString//from w w w.j a v a 2 s.c o m * @param start TODO * @param count TODO * @return NamedList<Object> in result or error string */ public IResult runQuery(String queryString, int start, int count) { log.logDebug("SolrCloudClient.runQuery- " + queryString + " " + start + " " + count); IResult result = new ResultPojo(); SolrQuery parameters = new SolrQuery(); parameters.set("q", queryString); parameters.setStart(start); if (count > -1) parameters.setRows(count); log.logDebug("SolrCloudClient.runQuery-1 " + parameters.toString()); try { QueryResponse x = server.query(parameters); log.logDebug("SolrCloudClient.runQuery-2 " + x.getResults()); result.setResultObject(x.getResults()); } catch (Exception e) { log.logError("SolrCloudClient.runQuery " + e.getMessage() + " " + queryString, e); result.addErrorString(e.getMessage()); } return result; }
From source file:org.vroyer.hive.solr.SolrTableCursor.java
License:Open Source License
private void fetchNextDocumentChunk() throws IOException { SolrQuery query = new SolrQuery(); query.setStart(start); if (table.facetType == null) { query.setRows(Math.min(count, this.solrSplitSize)); for (String s : table.fields) { // Don't push solr_query, this is an internal column when a cassandra table is indexed by SOLR. if (!s.equalsIgnoreCase("solr_query")) { query.addField(s);//from w w w. ja v a 2 s.c om } } } else { query.setRows(0); } List<NameValuePair> params = URLEncodedUtils.parse(table.qs, Charset.forName("UTF-8")); for (NameValuePair nvp : params) { query.set(nvp.getName(), nvp.getValue()); } if (table.fq.length() > 0) { query.set("fq", table.fq.toString()); } if (table.q.length() > 0) { query.set("q", table.q.toString()); } if (query.get("q") == null) { query.set("q", "*:*"); } if (log.isInfoEnabled()) { StringBuffer sb = new StringBuffer(""); Map<String, String[]> map = query.toMultiMap(query.toNamedList()); for (String s : map.keySet()) { sb.append(s).append('=').append(Arrays.toString(map.get(s))).append(' '); } log.info("SOLR request: " + sb.toString()); } checkRequiredFilterFields(); QueryResponse response; try { response = server.query(query); } catch (SolrServerException e) { throw new IOException(e); } if (table.facetType != null) { if (table.facetType.equalsIgnoreCase("ranges")) { List<RangeFacet.Count> counts = response.getFacetRanges().get(0).getCounts(); for (RangeFacet.Count rfc : counts) { facets.add(new FacetEntry(rfc.getValue(), rfc.getCount())); } } else if (table.facetType.equalsIgnoreCase("fields")) { List<FacetField.Count> counts = response.getFacetFields().get(0).getValues(); for (FacetField.Count rfc : counts) { facets.add(new FacetEntry(rfc.getName(), rfc.getCount())); } } else if (table.facetType.equalsIgnoreCase("queries")) { Map<String, Integer> queries = response.getFacetQuery(); for (String k : queries.keySet()) { facets.add(new FacetEntry(k, queries.get(k))); } } } else { buffer = response.getResults(); numFound = buffer.getNumFound(); log.info("SOLR response numFound=" + buffer.getNumFound()); start += buffer.size(); count -= buffer.size(); } pos = 0; }
From source file:org.wso2.carbon.registry.indexing.solr.SolrClient.java
License:Open Source License
/** * Method to create the solr query for indexing. * @param keywords content search keyword. * @param tenantId tenant id.//from ww w . j a v a2 s .c o m * @param fields Dynamic fields attribute list. * @return query response result. * @throws SolrException */ public SolrDocumentList query(String keywords, int tenantId, Map<String, String> fields) throws SolrException { try { SolrQuery query; // Get the attribute value for content String contentAttribute = fields.get(IndexingConstants.FIELD_CONTENT); if (contentAttribute != null && StringUtils.isNotEmpty(contentAttribute)) { if (getCharCount(contentAttribute, '"') > 0) { query = new SolrQuery(contentAttribute); } else { // Check for '&&' and replace with AND, Check for ' ' and replace with OR query = new SolrQuery(contentAttribute.replaceAll(" ", " OR ").replaceAll("&&", " AND ")); } fields.remove(IndexingConstants.FIELD_CONTENT); } else if (keywords.equals("[* TO *]")) { query = new SolrQuery("* TO *"); } else { query = new SolrQuery(keywords); } // Set no of rows query.setRows(Integer.MAX_VALUE); // Solr does not allow to search with special characters ,therefore this fix allow // to contain "-" in super tenant id. if (tenantId == MultitenantConstants.SUPER_TENANT_ID) { query.addFilterQuery(IndexingConstants.FIELD_TENANT_ID + ":" + "\\" + tenantId); } else { query.addFilterQuery(IndexingConstants.FIELD_TENANT_ID + ":" + tenantId); } if (fields.get(IndexingConstants.FIELD_MEDIA_TYPE) != null) { // This is for fixing REGISTRY-1695, This is temporary solution until // the default security polices also stored in Governance registry. if (fields.get(IndexingConstants.FIELD_MEDIA_TYPE).equals(RegistryConstants.POLICY_MEDIA_TYPE) || fields.get(IndexingConstants.FIELD_MEDIA_TYPE) .equals(RegistryConstants.WSDL_MEDIA_TYPE)) { query.addFilterQuery( IndexingConstants.FIELD_ID + ":" + SolrConstants.GOVERNANCE_REGISTRY_BASE_PATH + "*"); } } // add filter query for user role filtering addUserRoleFilter(tenantId, query); // Add query filters addQueryFilters(fields, query); QueryResponse queryresponse; MessageContext messageContext = MessageContext.getCurrentMessageContext(); if ((messageContext != null && PaginationUtils.isPaginationHeadersExist(messageContext)) || PaginationContext.getInstance() != null) { try { PaginationContext paginationContext; if (messageContext != null) { paginationContext = PaginationUtils.initPaginationContext(messageContext); } else { paginationContext = PaginationContext.getInstance(); } if (log.isDebugEnabled()) { log.debug("Pagination Context| start: " + paginationContext.getStart() + " | rows:" + paginationContext.getCount() + " | sortBy: " + paginationContext.getSortBy()); } //setting up start and row count for pagination query.setStart(paginationContext.getStart()); query.setRows(paginationContext.getCount()); String sortBy = paginationContext.getSortBy(); if (IndexingConstants.META_CREATED_DATE.equals(sortBy)) { sortBy = IndexingConstants.FIELD_CREATED_DATE; } else if (IndexingConstants.META_LAST_UPDATED_DATE.equals(sortBy)) { sortBy = IndexingConstants.FIELD_LAST_UPDATED_DATE; } if (sortBy.length() > 0) { String sortOrder = paginationContext.getSortOrder(); addSortByQuery(query, sortBy, sortOrder); } queryresponse = server.query(query); if (log.isDebugEnabled()) { log.debug("Solr index queried query: " + query); } //setting up result count in the paginationContext if (messageContext != null) { PaginationUtils.setRowCount(messageContext, Long.toString(queryresponse.getResults().getNumFound())); } else { paginationContext.setLength((int) queryresponse.getResults().getNumFound()); } } finally { if (messageContext != null) { PaginationContext.destroy(); } } } else { queryresponse = server.query(query); if (log.isDebugEnabled()) { log.debug("Solr index queried query: " + query); } } return queryresponse.getResults(); } catch (SolrServerException | IOException e) { String message = "Failure at query "; throw new SolrException(ErrorCode.SERVER_ERROR, message + keywords, e); } }