List of usage examples for org.apache.solr.client.solrj SolrQuery setRows
public SolrQuery setRows(Integer rows)
From source file:org.apache.nutch.searcher.SolrSearchBean.java
License:Apache License
public Hits search(Query query) throws IOException { // filter query string final BooleanQuery bQuery = filters.filter(query); final SolrQuery solrQuery = new SolrQuery(stringify(bQuery)); solrQuery.setRows(query.getParams().getNumHits()); if (query.getParams().getSortField() == null) { solrQuery.setFields(query.getParams().getDedupField(), "score", searchUID); query.getParams().setSortField("score"); } else {/* w w w.j av a 2 s .co m*/ solrQuery.setFields(query.getParams().getDedupField(), query.getParams().getSortField(), searchUID); solrQuery.setSortField(query.getParams().getSortField(), query.getParams().isReverse() ? ORDER.asc : ORDER.desc); } QueryResponse response; try { response = solr.query(solrQuery); } catch (final SolrServerException e) { throw SolrWriter.makeIOException(e); } final SolrDocumentList docList = response.getResults(); final Hit[] hitArr = new Hit[docList.size()]; for (int i = 0; i < hitArr.length; i++) { final SolrDocument solrDoc = docList.get(i); final Object raw = solrDoc.getFirstValue(query.getParams().getSortField()); WritableComparable sortValue; if (raw instanceof Integer) { sortValue = new IntWritable(((Integer) raw).intValue()); } else if (raw instanceof Float) { sortValue = new FloatWritable(((Float) raw).floatValue()); } else if (raw instanceof String) { sortValue = new Text((String) raw); } else if (raw instanceof Long) { sortValue = new LongWritable(((Long) raw).longValue()); } else { throw new RuntimeException("Unknown sort value type!"); } final String dedupValue = (String) solrDoc.getFirstValue(query.getParams().getDedupField()); final String uniqueKey = (String) solrDoc.getFirstValue(searchUID); hitArr[i] = new Hit(uniqueKey, sortValue, dedupValue); } return new Hits(docList.getNumFound(), hitArr); }
From source file:org.apache.ofbiz.solr.SolrProductSearch.java
License:Apache License
/** * Runs a query on the Solr Search Engine and returns the results. * <p>//from w ww. j a va 2s . c om * This function only returns an object of type QueryResponse, so it is probably not a good idea to call it directly from within the * groovy files (As a decent example on how to use it, however, use keywordSearch instead). */ public static Map<String, Object> runSolrQuery(DispatchContext dctx, Map<String, Object> context) { // get Connection HttpSolrClient client = null; String solrIndexName = (String) context.get("indexName"); Map<String, Object> result; try { client = SolrUtil.getInstance().getHttpSolrClient(solrIndexName); // create Query Object SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery((String) context.get("query")); // solrQuery.setQueryType("dismax"); boolean faceted = (Boolean) context.get("facet"); if (faceted) { solrQuery.setFacet(faceted); solrQuery.addFacetField("manu"); solrQuery.addFacetField("cat"); solrQuery.setFacetMinCount(1); solrQuery.setFacetLimit(8); solrQuery.addFacetQuery("listPrice:[0 TO 50]"); solrQuery.addFacetQuery("listPrice:[50 TO 100]"); solrQuery.addFacetQuery("listPrice:[100 TO 250]"); solrQuery.addFacetQuery("listPrice:[250 TO 500]"); solrQuery.addFacetQuery("listPrice:[500 TO 1000]"); solrQuery.addFacetQuery("listPrice:[1000 TO 2500]"); solrQuery.addFacetQuery("listPrice:[2500 TO 5000]"); solrQuery.addFacetQuery("listPrice:[5000 TO 10000]"); solrQuery.addFacetQuery("listPrice:[10000 TO 50000]"); solrQuery.addFacetQuery("listPrice:[50000 TO *]"); } boolean spellCheck = (Boolean) context.get("spellcheck"); if (spellCheck) { solrQuery.setParam("spellcheck", spellCheck); } boolean highLight = (Boolean) context.get("highlight"); if (highLight) { solrQuery.setHighlight(highLight); solrQuery.setHighlightSimplePre("<span class=\"highlight\">"); solrQuery.addHighlightField("description"); solrQuery.setHighlightSimplePost("</span>"); solrQuery.setHighlightSnippets(2); } // Set additional Parameter // SolrQuery.ORDER order = SolrQuery.ORDER.desc; if (context.get("viewIndex") != null && (Integer) context.get("viewIndex") > 0) { solrQuery.setStart((Integer) context.get("viewIndex")); } if (context.get("viewSize") != null && (Integer) context.get("viewSize") > 0) { solrQuery.setRows((Integer) context.get("viewSize")); } // if ((List) context.get("queryFilter") != null && ((ArrayList<SolrDocument>) context.get("queryFilter")).size() > 0) { // List filter = (List) context.get("queryFilter"); // String[] tn = new String[filter.size()]; // Iterator it = filter.iterator(); // for (int i = 0; i < filter.size(); i++) { // tn[i] = (String) filter.get(i); // } // solrQuery.setFilterQueries(tn); // } String queryFilter = (String) context.get("queryFilter"); if (UtilValidate.isNotEmpty(queryFilter)) solrQuery.setFilterQueries(queryFilter.split(" ")); if ((String) context.get("returnFields") != null) { solrQuery.setFields((String) context.get("returnFields")); } // if((Boolean)context.get("sortByReverse"))order.reverse(); if ((String) context.get("sortBy") != null && ((String) context.get("sortBy")).length() > 0) { SolrQuery.ORDER order; if (!((Boolean) context.get("sortByReverse"))) order = SolrQuery.ORDER.asc; else order = SolrQuery.ORDER.desc; solrQuery.setSort(((String) context.get("sortBy")).replaceFirst("-", ""), order); } if ((String) context.get("facetQuery") != null) { solrQuery.addFacetQuery((String) context.get("facetQuery")); } QueryResponse rsp = client.query(solrQuery); result = ServiceUtil.returnSuccess(); result.put("queryResult", rsp); } catch (Exception e) { Debug.logError(e, e.getMessage(), module); result = ServiceUtil.returnError(e.toString()); } finally { if (client != null) { try { client.close(); } catch (IOException e) { // do nothing } } } return result; }
From source file:org.apache.ofbiz.solr.SolrUtil.java
License:Apache License
public static Map<String, Object> categoriesAvailable(String catalogId, String categoryId, String productId, String facetPrefix, boolean displayproducts, int viewIndex, int viewSize, String solrIndexName) { // create the data model Map<String, Object> result = new HashMap<String, Object>(); HttpSolrClient client = null;//from w ww.j a va 2 s . co m QueryResponse returnMap = new QueryResponse(); try { // do the basic query client = getHttpSolrClient(solrIndexName); // create Query Object String query = "inStock[1 TO *]"; if (categoryId != null) query += " +cat:" + categoryId; else if (productId != null) query += " +productId:" + productId; SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(query); if (catalogId != null) solrQuery.setFilterQueries("catalog:" + catalogId); if (displayproducts) { if (viewSize > -1) { solrQuery.setRows(viewSize); } else solrQuery.setRows(50000); if (viewIndex > -1) { solrQuery.setStart(viewIndex); } } else { solrQuery.setFields("cat"); solrQuery.setRows(0); } if (UtilValidate.isNotEmpty(facetPrefix)) { solrQuery.setFacetPrefix(facetPrefix); } solrQuery.setFacetMinCount(0); solrQuery.setFacet(true); solrQuery.addFacetField("cat"); solrQuery.setFacetLimit(-1); Debug.logVerbose("solr: solrQuery: " + solrQuery, module); returnMap = client.query(solrQuery, METHOD.POST); result.put("rows", returnMap); result.put("numFound", returnMap.getResults().getNumFound()); } catch (Exception e) { Debug.logError(e.getMessage(), module); } return result; }
From source file:org.apache.ranger.solr.SolrUtil.java
License:Apache License
public QueryResponse searchResources(SearchCriteria searchCriteria, List<SearchField> searchFields, List<SortField> sortFieldList, SolrClient solrClient) { SolrQuery query = new SolrQuery(); query.setQuery("*:*"); if (searchCriteria.getParamList() != null) { // For now assuming there is only date field where range query will // be done. If we there are more than one, then we should create a // hashmap for each field name Date fromDate = null;//from ww w . ja v a2 s . c om Date toDate = null; String dateFieldName = null; for (SearchField searchField : searchFields) { Object paramValue = searchCriteria.getParamValue(searchField.getClientFieldName()); if (paramValue == null || paramValue.toString().isEmpty()) { continue; } String fieldName = searchField.getFieldName(); if (paramValue instanceof Collection) { String fq = orList(fieldName, (Collection<?>) paramValue); if (fq != null) { query.addFilterQuery(fq); } } else if (searchField.getDataType() == SearchField.DATA_TYPE.DATE) { if (!(paramValue instanceof Date)) { logger.error("Search file is not of java object instanceof Date"); } else { if (searchField.getSearchType() == SEARCH_TYPE.GREATER_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.GREATER_THAN) { fromDate = (Date) paramValue; dateFieldName = fieldName; } else if (searchField.getSearchType() == SEARCH_TYPE.LESS_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.LESS_THAN) { toDate = (Date) paramValue; } } } else if (searchField.getSearchType() == SEARCH_TYPE.GREATER_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.GREATER_THAN || searchField.getSearchType() == SEARCH_TYPE.LESS_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.LESS_THAN) { // TODO: Need to handle range here } else { String fq = setField(fieldName, paramValue); if (searchField.getSearchType() == SEARCH_TYPE.PARTIAL) { fq = setFieldForPartialSearch(fieldName, paramValue); } if (fq != null) { query.addFilterQuery(fq); } } } if (fromDate != null || toDate != null) { String fq = setDateRange(dateFieldName, fromDate, toDate); if (fq != null) { query.addFilterQuery(fq); } } } setSortClause(searchCriteria, sortFieldList, query); query.setStart(searchCriteria.getStartIndex()); query.setRows(searchCriteria.getMaxRows()); // Fields to get // query.setFields("myClassType", "id", "score", "globalId"); if (logger.isDebugEnabled()) { logger.debug("SOLR QUERY=" + query.toString()); } QueryResponse response = runQuery(solrClient, query); if (response == null || response.getStatus() != 0) { logger.error("Error running query. query=" + query.toString() + ", response=" + response); throw restErrorUtil.createRESTException("Error running query", MessageEnums.ERROR_SYSTEM); } return response; }
From source file:org.bigsolr.hadoop.SolrInputFormat.java
License:Apache License
@Override public List<InputSplit> getSplits(JobContext context) throws IOException, InterruptedException { log.info("SolrInputFormat -> getSplits"); Configuration conf = context.getConfiguration(); String collectionName = conf.get(COLLECTION_NAME); int numSplits = context.getNumReduceTasks(); SolrServer solr = SolrOperations.getSolrServer(conf); final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY)); solrQuery.setFields(ID_FIELD);//from w w w . j a v a2 s . c o m solrQuery.setRows(50); solrQuery.set("collection", collectionName); solrQuery.setStart(0); QueryResponse response; try { response = solr.query(solrQuery); } catch (final SolrServerException e) { throw new IOException(e); } int numResults = (int) response.getResults().getNumFound(); int numDocsPerSplit = (numResults / numSplits); int currentDoc = 0; List<InputSplit> splits = new ArrayList<InputSplit>(); for (int i = 0; i < numSplits - 1; i++) { splits.add(new SolrInputSplit(currentDoc, numDocsPerSplit)); currentDoc += numDocsPerSplit; } splits.add(new SolrInputSplit(currentDoc, numResults - currentDoc)); return splits; }
From source file:org.bigsolr.hadoop.SolrInputFormat.java
License:Apache License
@Override public org.apache.hadoop.mapred.InputSplit[] getSplits(org.apache.hadoop.mapred.JobConf conf, int numSplits) throws IOException { log.info("SolrInputFormat -> getSplits"); String collectionName = conf.get(COLLECTION_NAME); SolrServer solr = SolrOperations.getSolrServer(conf); final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY)); solrQuery.setFields(ID_FIELD);/* w ww. j a v a 2 s. c o m*/ solrQuery.setRows(50); solrQuery.set("collection", collectionName); solrQuery.setStart(0); QueryResponse response; try { response = solr.query(solrQuery); } catch (final SolrServerException e) { throw new IOException(e); } int numResults = (int) response.getResults().getNumFound(); int numDocsPerSplit = (numResults / numSplits); int currentDoc = 0; List<InputSplit> splits = new ArrayList<InputSplit>(); for (int i = 0; i < numSplits - 1; i++) { splits.add(new SolrInputSplit(currentDoc, numDocsPerSplit)); currentDoc += numDocsPerSplit; } splits.add(new SolrInputSplit(currentDoc, numResults - currentDoc)); return splits.toArray(new SolrInputSplit[splits.size()]); }
From source file:org.bigsolr.hadoop.SolrInputFormat.java
License:Apache License
@Override public RecordReader<NullWritable, SolrRecord> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException { log.info("SolrInputFormat -> createRecordReader"); Configuration conf = context.getConfiguration(); org.apache.hadoop.mapred.Reporter reporter = null; // Need to implement with heartbeat String collectionName = conf.get(COLLECTION_NAME); String fields = conf.get(FIELDS); SolrServer solr = SolrOperations.getSolrServer(conf); SolrInputSplit solrSplit = (SolrInputSplit) split; final int numDocs = (int) solrSplit.getLength(); final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY)); solrQuery.setFields(fields);//from w w w . j a va 2 s .co m solrQuery.set("collection", collectionName); solrQuery.setStart(solrSplit.getDocBegin()); solrQuery.setRows(numDocs); QueryResponse response; try { response = solr.query(solrQuery); } catch (final SolrServerException e) { throw new IOException(e); } final SolrDocumentList solrDocs = response.getResults(); return new SolrRecordReader(solrDocs, numDocs); }
From source file:org.bigsolr.hadoop.SolrInputFormat.java
License:Apache License
@Override public org.apache.hadoop.mapred.RecordReader<NullWritable, SolrRecord> getRecordReader( org.apache.hadoop.mapred.InputSplit split, org.apache.hadoop.mapred.JobConf conf, org.apache.hadoop.mapred.Reporter reporter) throws IOException { log.info("SolrInputFormat -> getRecordReader"); String collectionName = conf.get(COLLECTION_NAME); String fields = conf.get(FIELDS); SolrServer solr = SolrOperations.getSolrServer(conf); int numDocs = 0; SolrInputSplit solrSplit = (SolrInputSplit) split; try {/* ww w . j a va 2 s. com*/ numDocs = (int) solrSplit.getLength(); } catch (final IOException e) { throw new IOException(e); } final SolrQuery solrQuery = new SolrQuery(conf.get(SOLR_QUERY)); solrQuery.setFields(fields); solrQuery.set("collection", collectionName); // Added solrQuery.setStart(solrSplit.getDocBegin()); solrQuery.setRows(numDocs); QueryResponse response = null; try { response = solr.query(solrQuery); } catch (final SolrServerException e) { throw new IOException(e); } final SolrDocumentList solrDocs = response.getResults(); return new SolrRecordReader(solrDocs, numDocs); }
From source file:org.codelibs.fess.helper.IndexingHelper.java
License:Apache License
protected SolrDocumentList getSolrDocumentList(final SolrGroup solrGroup, final String fq, final String q, final String[] fields, final int row) { final SolrQuery sq = new SolrQuery(); if (fq != null) { sq.setFilterQueries(fq);// w ww .j a v a 2s .com } sq.setQuery(q); if (fields != null) { sq.setFields(fields); } sq.setRows(row); final SolrDocumentList docList = solrGroup.query(sq).getResults(); if (docList.getNumFound() <= row) { return docList; } return getSolrDocumentList(solrGroup, fq, q, fields, (int) docList.getNumFound()); }
From source file:org.codelibs.fess.helper.IndexingHelper.java
License:Apache License
protected SolrDocumentList getChildSolrDocumentList(final SolrGroup solrGroup, final String id, final String[] fields, final int row) { final FieldHelper fieldHelper = ComponentUtil.getFieldHelper(); final SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("{!raw f=" + fieldHelper.parentIdField + " v=\"" + id + "\"}"); if (fields != null) { solrQuery.setFields(fields);/* www . j a va 2 s . c o m*/ } solrQuery.setRows(row); final SolrDocumentList docList = solrGroup.query(solrQuery).getResults(); if (docList.getNumFound() <= row) { return docList; } return getChildSolrDocumentList(solrGroup, id, fields, (int) docList.getNumFound()); }