List of usage examples for org.apache.solr.client.solrj SolrQuery setQuery
public SolrQuery setQuery(String query)
From source file:org.apache.nifi.processors.solr.QuerySolr.java
License:Apache License
@Override public void doOnTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final ComponentLog logger = getLogger(); FlowFile flowFileOriginal = session.get(); FlowFile flowFileResponse;// ww w . j a va2s. c om if (flowFileOriginal == null) { if (context.hasNonLoopConnection()) { return; } flowFileResponse = session.create(); } else { flowFileResponse = session.create(flowFileOriginal); } final SolrQuery solrQuery = new SolrQuery(); final boolean isSolrCloud = SOLR_TYPE_CLOUD.equals(context.getProperty(SOLR_TYPE).getValue()); final String collection = context.getProperty(COLLECTION).evaluateAttributeExpressions(flowFileResponse) .getValue(); final StringBuilder transitUri = new StringBuilder("solr://"); transitUri.append(getSolrLocation()); if (isSolrCloud) { transitUri.append(":").append(collection); } final StopWatch timer = new StopWatch(false); try { solrQuery.setQuery(context.getProperty(SOLR_PARAM_QUERY).evaluateAttributeExpressions(flowFileResponse) .getValue()); solrQuery.setRequestHandler(context.getProperty(SOLR_PARAM_REQUEST_HANDLER) .evaluateAttributeExpressions(flowFileResponse).getValue()); if (context.getProperty(SOLR_PARAM_FIELD_LIST).isSet()) { for (final String field : context.getProperty(SOLR_PARAM_FIELD_LIST) .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) { solrQuery.addField(field.trim()); } } // Avoid ArrayIndexOutOfBoundsException due to incorrectly configured sorting try { if (context.getProperty(SOLR_PARAM_SORT).isSet()) { final List<SolrQuery.SortClause> sortings = new ArrayList<>(); for (final String sorting : context.getProperty(SOLR_PARAM_SORT) .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) { final String[] sortEntry = sorting.trim().split(" "); sortings.add(new SolrQuery.SortClause(sortEntry[0], sortEntry[1])); } solrQuery.setSorts(sortings); } } catch (Exception e) { throw new ProcessException("Error while parsing the sort clauses for the Solr query"); } final Integer startParam = context.getProperty(SOLR_PARAM_START).isSet() ? Integer.parseInt(context.getProperty(SOLR_PARAM_START) .evaluateAttributeExpressions(flowFileResponse).getValue()) : CommonParams.START_DEFAULT; solrQuery.setStart(startParam); final Integer rowParam = context.getProperty(SOLR_PARAM_ROWS).isSet() ? Integer.parseInt(context.getProperty(SOLR_PARAM_ROWS) .evaluateAttributeExpressions(flowFileResponse).getValue()) : CommonParams.ROWS_DEFAULT; solrQuery.setRows(rowParam); final Map<String, String[]> additionalSolrParams = SolrUtils.getRequestParams(context, flowFileResponse); final Set<String> searchComponents = extractSearchComponents(additionalSolrParams); solrQuery.add(new MultiMapSolrParams(additionalSolrParams)); final Map<String, String> attributes = new HashMap<>(); attributes.put(ATTRIBUTE_SOLR_CONNECT, getSolrLocation()); if (isSolrCloud) { attributes.put(ATTRIBUTE_SOLR_COLLECTION, collection); } attributes.put(ATTRIBUTE_SOLR_QUERY, solrQuery.toString()); if (flowFileOriginal != null) { flowFileOriginal = session.putAllAttributes(flowFileOriginal, attributes); } flowFileResponse = session.putAllAttributes(flowFileResponse, attributes); final boolean getEntireResults = RETURN_ALL_RESULTS .equals(context.getProperty(AMOUNT_DOCUMENTS_TO_RETURN).getValue()); boolean processFacetsAndStats = true; boolean continuePaging = true; while (continuePaging) { timer.start(); Map<String, String> responseAttributes = new HashMap<>(); responseAttributes.put(ATTRIBUTE_SOLR_START, solrQuery.getStart().toString()); responseAttributes.put(ATTRIBUTE_SOLR_ROWS, solrQuery.getRows().toString()); if (solrQuery.getStart() > UPPER_LIMIT_START_PARAM) { logger.warn( "The start parameter of Solr query {} exceeded the upper limit of {}. The query will not be processed " + "to avoid performance or memory issues on the part of Solr.", new Object[] { solrQuery.toString(), UPPER_LIMIT_START_PARAM }); flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes); timer.stop(); break; } final QueryRequest req = new QueryRequest(solrQuery); if (isBasicAuthEnabled()) { req.setBasicAuthCredentials(getUsername(), getPassword()); } final QueryResponse response = req.process(getSolrClient()); timer.stop(); final Long totalNumberOfResults = response.getResults().getNumFound(); responseAttributes.put(ATTRIBUTE_SOLR_NUMBER_RESULTS, totalNumberOfResults.toString()); responseAttributes.put(ATTRIBUTE_CURSOR_MARK, response.getNextCursorMark()); responseAttributes.put(ATTRIBUTE_SOLR_STATUS, String.valueOf(response.getStatus())); responseAttributes.put(ATTRIBUTE_QUERY_TIME, String.valueOf(response.getQTime())); flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes); if (response.getResults().size() > 0) { if (context.getProperty(RETURN_TYPE).getValue().equals(MODE_XML.getValue())) { flowFileResponse = session.write(flowFileResponse, SolrUtils.getOutputStreamCallbackToTransformSolrResponseToXml(response)); flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(), MIME_TYPE_XML); } else { final RecordSetWriterFactory writerFactory = context.getProperty(RECORD_WRITER) .evaluateAttributeExpressions(flowFileResponse) .asControllerService(RecordSetWriterFactory.class); final RecordSchema schema = writerFactory.getSchema(flowFileResponse.getAttributes(), null); final RecordSet recordSet = SolrUtils.solrDocumentsToRecordSet(response.getResults(), schema); final StringBuffer mimeType = new StringBuffer(); final FlowFile flowFileResponseRef = flowFileResponse; flowFileResponse = session.write(flowFileResponse, out -> { try (final RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out, flowFileResponseRef)) { writer.write(recordSet); writer.flush(); mimeType.append(writer.getMimeType()); } catch (SchemaNotFoundException e) { throw new ProcessException("Could not parse Solr response", e); } }); flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(), mimeType.toString()); } if (processFacetsAndStats) { if (searchComponents.contains(FacetParams.FACET)) { FlowFile flowFileFacets = session.create(flowFileResponse); flowFileFacets = session.write(flowFileFacets, out -> { try (final OutputStreamWriter osw = new OutputStreamWriter(out); final JsonWriter writer = new JsonWriter(osw)) { addFacetsFromSolrResponseToJsonWriter(response, writer); } }); flowFileFacets = session.putAttribute(flowFileFacets, CoreAttributes.MIME_TYPE.key(), MIME_TYPE_JSON); session.getProvenanceReporter().receive(flowFileFacets, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileFacets, FACETS); } if (searchComponents.contains(StatsParams.STATS)) { FlowFile flowFileStats = session.create(flowFileResponse); flowFileStats = session.write(flowFileStats, out -> { try (final OutputStreamWriter osw = new OutputStreamWriter(out); final JsonWriter writer = new JsonWriter(osw)) { addStatsFromSolrResponseToJsonWriter(response, writer); } }); flowFileStats = session.putAttribute(flowFileStats, CoreAttributes.MIME_TYPE.key(), MIME_TYPE_JSON); session.getProvenanceReporter().receive(flowFileStats, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileStats, STATS); } processFacetsAndStats = false; } } if (getEntireResults) { final Integer totalDocumentsReturned = solrQuery.getStart() + solrQuery.getRows(); if (totalDocumentsReturned < totalNumberOfResults) { solrQuery.setStart(totalDocumentsReturned); session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileResponse, RESULTS); flowFileResponse = session.create(flowFileResponse); } else { continuePaging = false; } } else { continuePaging = false; } } } catch (Exception e) { flowFileResponse = session.penalize(flowFileResponse); flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION, e.getClass().getName()); flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION_MESSAGE, e.getMessage()); session.transfer(flowFileResponse, FAILURE); logger.error("Failed to execute query {} due to {}. FlowFile will be routed to relationship failure", new Object[] { solrQuery.toString(), e }, e); if (flowFileOriginal != null) { flowFileOriginal = session.penalize(flowFileOriginal); } } if (!flowFileResponse.isPenalized()) { session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(), timer.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFileResponse, RESULTS); } if (flowFileOriginal != null) { if (!flowFileOriginal.isPenalized()) { session.transfer(flowFileOriginal, ORIGINAL); } else { session.remove(flowFileOriginal); } } }
From source file:org.apache.nutch.indexwriter.solr.TestSolrJ.java
License:Apache License
/** * query the example//w w w .j a v a 2s . c o m */ @Test public void testQuery() throws Exception { SolrClient client = new HttpSolrClient.Builder(serverUrl).build(); // Empty the database... client.deleteByQuery("*:*");// delete everything! // Now add something... SolrInputDocument doc = new SolrInputDocument(); String docID = "1112211111"; doc.addField("id", docID, 1.0f); doc.addField("name", "my name!", 1.0f); assertEquals(null, doc.getField("foo")); assertTrue(doc.getField("name").getValue() != null); UpdateResponse upres = client.add(doc); // System.out.println( "ADD:"+upres.getResponse() ); assertEquals(0, upres.getStatus()); upres = client.commit(true, true); // System.out.println( "COMMIT:"+upres.getResponse() ); assertEquals(0, upres.getStatus()); upres = client.optimize(true, true); // System.out.println( "OPTIMIZE:"+upres.getResponse() ); assertEquals(0, upres.getStatus()); SolrQuery query = new SolrQuery(); query.setQuery("id:" + docID); QueryResponse response = client.query(query); assertEquals(docID, response.getResults().get(0).getFieldValue("id")); // Now add a few docs for facet testing... List<SolrInputDocument> docs = new ArrayList<>(); SolrInputDocument doc2 = new SolrInputDocument(); doc2.addField("id", "2", 1.0f); doc2.addField("inStock", true, 1.0f); doc2.addField("price", 2, 1.0f); doc2.addField("timestamp_dt", new java.util.Date(), 1.0f); docs.add(doc2); SolrInputDocument doc3 = new SolrInputDocument(); doc3.addField("id", "3", 1.0f); doc3.addField("inStock", false, 1.0f); doc3.addField("price", 3, 1.0f); doc3.addField("timestamp_dt", new java.util.Date(), 1.0f); docs.add(doc3); SolrInputDocument doc4 = new SolrInputDocument(); doc4.addField("id", "4", 1.0f); doc4.addField("inStock", true, 1.0f); doc4.addField("price", 4, 1.0f); doc4.addField("timestamp_dt", new java.util.Date(), 1.0f); docs.add(doc4); SolrInputDocument doc5 = new SolrInputDocument(); doc5.addField("id", "5", 1.0f); doc5.addField("inStock", false, 1.0f); doc5.addField("price", 5, 1.0f); doc5.addField("timestamp_dt", new java.util.Date(), 1.0f); docs.add(doc5); upres = client.add(docs); // System.out.println( "ADD:"+upres.getResponse() ); assertEquals(0, upres.getStatus()); upres = client.commit(true, true); // System.out.println( "COMMIT:"+upres.getResponse() ); assertEquals(0, upres.getStatus()); upres = client.optimize(true, true); // System.out.println( "OPTIMIZE:"+upres.getResponse() ); assertEquals(0, upres.getStatus()); query = new SolrQuery("*:*"); query.addFacetQuery("price:[* TO 2]"); query.addFacetQuery("price:[2 TO 4]"); query.addFacetQuery("price:[5 TO *]"); query.addFacetField("inStock"); query.addFacetField("price"); query.addFacetField("timestamp_dt"); query.removeFilterQuery("inStock:true"); response = client.query(query); assertEquals(0, response.getStatus()); assertEquals(5, response.getResults().getNumFound()); assertEquals(3, response.getFacetQuery().size()); assertEquals(2, response.getFacetField("inStock").getValueCount()); assertEquals(4, response.getFacetField("price").getValueCount()); // test a second query, test making a copy of the main query SolrQuery query2 = query.getCopy(); query2.addFilterQuery("inStock:true"); response = client.query(query2); assertEquals(1, query2.getFilterQueries().length); assertEquals(0, response.getStatus()); assertEquals(2, response.getResults().getNumFound()); assertFalse(query.getFilterQueries() == query2.getFilterQueries()); // sanity check round tripping of params... query = new SolrQuery("foo"); query.addFilterQuery("{!field f=inStock}true"); query.addFilterQuery("{!term f=name}hoss"); query.addFacetQuery("price:[* TO 2]"); query.addFacetQuery("price:[2 TO 4]"); response = client.query(query); assertTrue("echoed params are not a NamedList: " + response.getResponseHeader().get("params").getClass(), response.getResponseHeader().get("params") instanceof NamedList); NamedList echo = (NamedList) response.getResponseHeader().get("params"); List values = null; assertEquals("foo", echo.get("q")); assertTrue("echoed fq is not a List: " + echo.get("fq").getClass(), echo.get("fq") instanceof List); values = (List) echo.get("fq"); assertEquals(2, values.size()); assertEquals("{!field f=inStock}true", values.get(0)); assertEquals("{!term f=name}hoss", values.get(1)); assertTrue("echoed facet.query is not a List: " + echo.get("facet.query").getClass(), echo.get("facet.query") instanceof List); values = (List) echo.get("facet.query"); assertEquals(2, values.size()); assertEquals("price:[* TO 2]", values.get(0)); assertEquals("price:[2 TO 4]", values.get(1)); }
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 w w.j a v a 2 s. c o m*/ * 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;/* w w w . ja va 2s .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 w w w. jav a 2s. c o m 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.apache.sentry.tests.e2e.solr.db.integration.TestSolrDocLevelOperations.java
License:Apache License
@Test public void testDocLevelOperations() throws Exception { setupCollectionWithDocSecurity(TEST_COLLECTION_NAME1); createDocument(TEST_COLLECTION_NAME1); CloudSolrServer server = getCloudSolrServer(TEST_COLLECTION_NAME1); try {// w ww . j a va2 s . c om // queries SolrQuery query = new SolrQuery(); query.setQuery("*:*"); // as admin setAuthenticationUser(ADMIN_USER); QueryResponse rsp = server.query(query); SolrDocumentList docList = rsp.getResults(); assertEquals(NUM_DOCS, docList.getNumFound()); // as user0 setAuthenticationUser("user0"); grantCollectionPrivilege(TEST_COLLECTION_NAME1, ADMIN_USER, "role0", SearchConstants.QUERY); rsp = server.query(query); docList = rsp.getResults(); assertEquals(NUM_DOCS / 4, rsp.getResults().getNumFound()); //as user1 setAuthenticationUser("user1"); grantCollectionPrivilege(TEST_COLLECTION_NAME1, ADMIN_USER, "role1", SearchConstants.QUERY); rsp = server.query(query); docList = rsp.getResults(); assertEquals(NUM_DOCS / 4, rsp.getResults().getNumFound()); docList = rsp.getResults(); assertEquals(NUM_DOCS / 4, rsp.getResults().getNumFound()); //as user2 setAuthenticationUser("user2"); grantCollectionPrivilege(TEST_COLLECTION_NAME1, ADMIN_USER, "role2", SearchConstants.QUERY); rsp = server.query(query); docList = rsp.getResults(); assertEquals(NUM_DOCS / 4, rsp.getResults().getNumFound()); //as user3 setAuthenticationUser("user3"); grantCollectionPrivilege(TEST_COLLECTION_NAME1, ADMIN_USER, "role3", SearchConstants.QUERY); rsp = server.query(query); docList = rsp.getResults(); assertEquals(NUM_DOCS / 4, rsp.getResults().getNumFound()); } finally { server.shutdown(); } deleteCollection(TEST_COLLECTION_NAME1); }
From source file:org.apache.sentry.tests.e2e.solr.db.integration.TestSolrDocLevelOperations.java
License:Apache License
@Test public void updateDocsTest() throws Exception { setupCollectionWithDocSecurity(TEST_COLLECTION_NAME1); createDocument(TEST_COLLECTION_NAME1); CloudSolrServer server = getCloudSolrServer(TEST_COLLECTION_NAME1); try {/*from w w w . j a v a2 s.c om*/ setAuthenticationUser("user0"); grantCollectionPrivilege(TEST_COLLECTION_NAME1, ADMIN_USER, "role0", SearchConstants.QUERY); String docIdStr = Long.toString(1); // verify we can't view one of the odd documents SolrQuery query = new SolrQuery(); query.setQuery("id:" + docIdStr); QueryResponse rsp = server.query(query); assertEquals(0, rsp.getResults().getNumFound()); // overwrite the document that we can't see setAuthenticationUser(ADMIN_USER); ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", docIdStr); doc.addField("description", "description" + docIdStr); doc.addField(AUTH_FIELD, "role0"); docs.add(doc); server.add(docs); server.commit(); // verify we can now view the document setAuthenticationUser("user0"); rsp = server.query(query); assertEquals(1, rsp.getResults().getNumFound()); } finally { server.shutdown(); } deleteCollection(TEST_COLLECTION_NAME1); }
From source file:org.codelibs.fess.helper.CrawlingSessionHelper.java
License:Apache License
public List<Map<String, String>> getSessionIdList(final SolrGroup serverGroup) { final List<Map<String, String>> sessionIdList = new ArrayList<Map<String, String>>(); final FieldHelper fieldHelper = ComponentUtil.getFieldHelper(); final SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.setFacet(true);/*from ww w .j ava 2 s. co m*/ query.addFacetField(fieldHelper.segmentField); query.addSort(fieldHelper.segmentField, ORDER.desc); final QueryResponse queryResponse = serverGroup.query(query); final List<FacetField> facets = queryResponse.getFacetFields(); for (final FacetField facet : facets) { final List<FacetField.Count> facetEntries = facet.getValues(); if (facetEntries != null) { for (final FacetField.Count fcount : facetEntries) { final Map<String, String> map = new HashMap<String, String>(2); map.put(fieldHelper.segmentField, fcount.getName()); map.put(FACET_COUNT_KEY, Long.toString(fcount.getCount())); sessionIdList.add(map); } } } return sessionIdList; }
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 w w .j av a 2 s . c om*/ } 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
public SolrDocument getSolrDocument(final SolrGroup solrGroup, final String id, final String[] fields) { final FieldHelper fieldHelper = ComponentUtil.getFieldHelper(); final SolrQuery solrQuery = new SolrQuery(); final StringBuilder queryBuf = new StringBuilder(500); queryBuf.append("{!raw f=").append(fieldHelper.idField).append("}"); queryBuf.append(id);/*from ww w . j av a2 s .c om*/ solrQuery.setQuery(queryBuf.toString()); if (fields != null) { solrQuery.setFields(fields); } final QueryResponse response = solrGroup.query(solrQuery); final SolrDocumentList docList = response.getResults(); if (docList.isEmpty()) { return null; } if (docList.size() > 1) { logger.error("Invalid multiple docs for " + id); for (final SolrDocument doc : docList) { final Object idValue = doc.getFieldValue(fieldHelper.idField); if (idValue != null) { deleteDocument(solrGroup, idValue.toString()); } } return null; } return docList.get(0); }