Java Code Examples for org.apache.solr.client.solrj.response.QueryResponse

The following examples show how to use org.apache.solr.client.solrj.response.QueryResponse. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void fieldFaceting_mincountSetZero_shouldReturnFacetsMincountOne() throws Exception
{
    String expectedContentFacetField = "{http://www.alfresco.org/model/content/1.0}content:[contenttwo (4), contentone (1)]";
    String expectedNameFacetField = "{http://www.alfresco.org/model/content/1.0}name:[nametwo (4), nameone (1)]";

    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();

    QueryResponse queryResponse = query(getDefaultTestClient(), true, jsonQuery,
        params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "0", "fl", "score,id", "facet", "true",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}content",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}name",
            "facet.mincount", "0"));

    List<FacetField> facetFields = queryResponse.getFacetFields();
    FacetField contentFacetField = facetFields.get(0);
    Assert.assertThat(contentFacetField.toString(), is(expectedContentFacetField));
    FacetField nameFacetField = facetFields.get(1);
    Assert.assertThat(nameFacetField.toString(), is(expectedNameFacetField));
}
 
Example 2
@Test
public void testFacetWithDomainWidenedUsingExcludeTagsToIgnoreFilters() throws Exception {
  final TermsFacetMap inStockFacet = new TermsFacetMap("cat")
      .setLimit(2);
  final TermsFacetMap allProductsFacet = new TermsFacetMap("cat")
      .setLimit(2).withDomain(new DomainMap().withTagsToExclude("on_shelf"));
  final Map<String, Object> taggedFilterMap = new HashMap<>();
  taggedFilterMap.put("#on_shelf", "inStock:true");
  final JsonQueryRequest request = new JsonQueryRequest()
      .setQuery("*:*")
      .withFilter(taggedFilterMap)
      .withFacet("in_stock_only", inStockFacet)
      .withFacet("all", allProductsFacet);

  QueryResponse response = request.process(cluster.getSolrClient(), COLLECTION_NAME);

  assertExpectedDocumentsFoundAndReturned(response, NUM_IN_STOCK, 10);
  final NestableJsonFacet topLevelFacetData = response.getJsonFacetingResponse();
  assertHasFacetWithBucketValues(topLevelFacetData,"in_stock_only",
      new FacetBucket("electronics",8),
      new FacetBucket("currency", 4));
  assertHasFacetWithBucketValues(topLevelFacetData  ,"all",
      new FacetBucket("electronics",12),
      new FacetBucket("currency", 4));
}
 
Example 3
Source Project: lucene-solr   Source File: SolrExampleTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testChineseDefaults() throws Exception {
  SolrClient client = getSolrClient();
  // Empty the database...
  client.deleteByQuery("*:*");// delete everything!
  client.commit();
  assertNumFound( "*:*", 0 ); // make sure it got in

  // Beijing medical University
  UpdateRequest req = new UpdateRequest();
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "42");
  doc.addField("text", "北京医科大学");
  req.add(doc);

  req.setAction(ACTION.COMMIT, true, true );
  req.process( client );

  // Beijing university should match:
  SolrQuery query = new SolrQuery("北京大学");
  QueryResponse rsp = client.query( query );
  assertEquals(1, rsp.getResults().getNumFound());
}
 
Example 4
Source Project: tutorials   Source File: ItemSearchServiceLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void whenSearchingByBasicQuery_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("hm0003", "LED TV 32", "Brand1 Home Appliances", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("brand1");
    query.setStart(0);
    query.setRows(10);

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(3, items.size());

}
 
Example 5
void queryTimestampDescending(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(maxTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (maxTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  query.setRows(1);
  // CommonParams.SEGMENT_TERMINATE_EARLY parameter intentionally absent
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertEquals("numFound", numDocs/2, rsp.getResults().getNumFound());
  // check correctness of the first result
  if (rsp.getResults().getNumFound() > 0) {
    final SolrDocument solrDocument0 = rsp.getResults().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument0.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument0+") is not in maxTimestampDocKeys("+maxTimestampDocKeys+")",
       maxTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, solrDocument0.getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  TestSegmentSorting.assertNull("responseHeader.segmentTerminatedEarly present in "+rsp.getResponseHeader(),
      rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
}
 
Example 6
Source Project: mycore   Source File: MCROAISolrSearcher.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public Optional<Header> getHeader(String mcrId) {
    SolrQuery query = getBaseQuery(CommonParams.FQ);
    query.set(CommonParams.Q, "id:" + MCRSolrUtils.escapeSearchValue(mcrId));
    query.setRows(1);
    // do the query
    SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
    try {
        QueryResponse response = solrClient.query(query);
        SolrDocumentList results = response.getResults();
        if (!results.isEmpty()) {
            return Optional.of(toHeader(results.get(0), getSetResolver(results)));
        }
    } catch (Exception exc) {
        LOGGER.error("Unable to handle solr request", exc);
    }
    return Optional.empty();
}
 
Example 7
Source Project: hbase-indexer   Source File: TestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void validateSolrServerDocumentCount(File solrHomeDir, FileSystem fs, Path outDir, int expectedDocs, int expectedShards)
    throws IOException, SolrServerException {
  
  long actualDocs = 0;
  int actualShards = 0;
  for (FileStatus dir : fs.listStatus(outDir)) { // for each shard
    if (dir.getPath().getName().startsWith("part") && dir.isDirectory()) {
      actualShards++;
      EmbeddedSolrServer solr = createEmbeddedSolrServer(
          solrHomeDir, fs, dir.getPath());
      
      try {
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        QueryResponse resp = solr.query(query);
        long numDocs = resp.getResults().getNumFound();
        actualDocs += numDocs;
      } finally {
        solr.close();
      }
    }
  }
  assertEquals(expectedShards, actualShards);
  assertEquals(expectedDocs, actualDocs);
}
 
Example 8
@Test
public void testRangeFacetWithOtherBucketsRequested() throws Exception {
  final JsonQueryRequest request = new JsonQueryRequest()
      .setQuery("*:*")
      .withFacet("price_range",
          new RangeFacetMap("price", 0, 100, 20)
              .setOtherBuckets(RangeFacetMap.OtherBuckets.ALL)
      );

  QueryResponse response = request.process(cluster.getSolrClient(), COLLECTION_NAME);
  final NestableJsonFacet topLevelFacetData = response.getJsonFacetingResponse();

  assertHasFacetWithBucketValues(topLevelFacetData, "price_range",
      new FacetBucket(0.0f, 5),
      new FacetBucket(20.0f, 0),
      new FacetBucket(40.0f, 0),
      new FacetBucket(60.0f, 1),
      new FacetBucket(80.0f, 1));
  assertEquals(0, topLevelFacetData.getBucketBasedFacets("price_range").getBefore());
  assertEquals(9, topLevelFacetData.getBucketBasedFacets("price_range").getAfter());
  assertEquals(7, topLevelFacetData.getBucketBasedFacets("price_range").getBetween());
}
 
Example 9
Source Project: lucene-solr   Source File: CloudSolrClientTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOverwriteOption() throws Exception {

  CollectionAdminRequest.createCollection("overwrite", "conf", 1, 1)
      .processAndWait(cluster.getSolrClient(), TIMEOUT);
  cluster.waitForActiveCollection("overwrite", 1, 1);
  
  new UpdateRequest()
      .add("id", "0", "a_t", "hello1")
      .add("id", "0", "a_t", "hello2")
      .commit(cluster.getSolrClient(), "overwrite");

  QueryResponse resp = cluster.getSolrClient().query("overwrite", new SolrQuery("*:*"));
  assertEquals("There should be one document because overwrite=true", 1, resp.getResults().getNumFound());

  new UpdateRequest()
      .add(new SolrInputDocument(id, "1", "a_t", "hello1"), /* overwrite = */ false)
      .add(new SolrInputDocument(id, "1", "a_t", "hello2"), false)
      .commit(cluster.getSolrClient(), "overwrite");
    
  resp = getRandomClient().query("overwrite", new SolrQuery("*:*"));
  assertEquals("There should be 3 documents because there should be two id=1 docs due to overwrite=false", 3, resp.getResults().getNumFound());

}
 
Example 10
Source Project: TagRec   Source File: SolrConnector.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public List<Tweet> getTrainTweetObjects(SolrConnector trainConnector, int hours) {
	List<Tweet> tweetObjects = new ArrayList<Tweet>();
	
	SolrQuery solrParams = new SolrQuery();
	solrParams.set("q", "*:*");
	solrParams.set("rows", Integer.MAX_VALUE);
	QueryResponse r = null;
	try {
		r = this.server.query(solrParams);
		SolrDocumentList docs = r.getResults();
		for (SolrDocument d : docs) {
			String userId = (String) d.get("userid");
			String text = trainConnector.getTweetTextOfLastHours(userId, hours);
			tweetObjects.add(new Tweet((String) d.get("id"), userId, text, (String) d.get("timestamp"), new LinkedHashSet<String>((List<String>) d.get("hashtags"))));
		}
	} catch (SolrServerException e) {
		e.printStackTrace();
	}
	
	return tweetObjects;
}
 
Example 11
Source Project: lucene-solr   Source File: JsonRequestApiTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTermsFacet2() throws Exception {
  SolrClient solrClient = cluster.getSolrClient();

  //tag::solrj-json-terms-facet-2[]
  final TermsFacetMap categoryFacet = new TermsFacetMap("cat").setLimit(5);
  final JsonQueryRequest request = new JsonQueryRequest()
      .setQuery("*:*")
      .withFacet("categories", categoryFacet);
  QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
  //end::solrj-json-terms-facet-2[]

  assertEquals(0, queryResponse.getStatus());
  assertEquals(32, queryResponse.getResults().getNumFound());
  assertEquals(10, queryResponse.getResults().size());
  final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
  assertHasFacetWithBucketValues(topLevelFacetingData,"categories",
      new FacetBucket("electronics",12),
      new FacetBucket("currency", 4),
      new FacetBucket("memory", 3),
      new FacetBucket("connector", 2),
      new FacetBucket("graphics card", 2));
}
 
Example 12
@Test
public void transformDocument_noDocTransformer_shouldReturnBasicFields() throws Exception 
{
    putHandleDefaults();
    //Test 1: Running a simple query without invoking AlfrescoFieldMapperTransformer, expected to see id,DBID and _version_
    QueryResponse resp = query(getDefaultTestClient(), true, ALFRESCO_JSON, params("q", "*", "qt", "/afts", "shards.qt", "/afts", "sort", "id asc"));
    assertNotNull(resp);
    SolrDocumentList results = resp.getResults();
    assertEquals("Expecting 5 rows",5, results.size());
    SolrDocument doc = results.get(0);
    assertEquals(3, doc.size());
    assertNotNull(doc);
    String id = (String) doc.get("id");
    assertNotNull(id);
    Long version = (Long) doc.get("_version_");
    assertNotNull(version);
    Long dbid = (Long) doc.get("DBID");
    assertNotNull(dbid);
    //Not expected to see below as part of the solr response.
    String title = (String) doc.get("cm:title");
    assertNull(title);
    String owner = (String) doc.get("OWNER");
    assertNull(owner);
}
 
Example 13
Source Project: skywalking   Source File: CaseController.java    License: Apache License 2.0 6 votes vote down vote up
@GetMapping("/healthcheck")
public String healthcheck() throws Exception {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(CommonParams.Q, "*:*");
    params.set(CommonParams.OMIT_HEADER, true);

    HttpSolrClient client = getClient();
    try {
        QueryResponse response = client.query(collection, params);
        if (response.getStatus() == 0) {
            return "Success";
        }
        throw new Exception(response.toString());
    } catch (Exception e) {
        throw e;
    }
}
 
Example 14
Source Project: vind   Source File: SolrSearchServer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public SuggestionResult execute(ExecutableSuggestionSearch search, DocumentFactory assets,DocumentFactory childFactory) {
    SolrQuery query = buildSolrQuery(search, assets, childFactory);

    try {
        log.debug(">>> query({})", query.toString());
        QueryResponse response = solrClient.query(query, REQUEST_METHOD);
        if(response!=null){
            return SolrUtils.Result.buildSuggestionResult(response, assets, childFactory, search.getSearchContext());
        }else {
            log.error("Null result from SolrClient");
            throw new SolrServerException("Null result from SolrClient");
        }

    } catch (SolrServerException | IOException e) {
        log.error("Cannot execute suggestion query");
        throw new SearchServerException("Cannot execute suggestion query", e);
    }
}
 
Example 15
Source Project: lucene-solr   Source File: SolrExampleTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSetNullUpdates() throws Exception {
  SolrClient solrClient = getSolrClient();
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "testSetNullUpdates");
  doc.addField("single_s", "test-value");
  doc.addField("multi_ss", Arrays.asList("first", "second"));
  solrClient.add(doc);
  solrClient.commit(true, true);
  doc.removeField("single_s");
  doc.removeField("multi_ss");
  Map<String, Object> map = Maps.newHashMap();
  map.put("set", null);
  doc.addField("multi_ss", map);
  solrClient.add(doc);
  solrClient.commit(true, true);
  QueryResponse response = solrClient.query(new SolrQuery("id:testSetNullUpdates"));
  assertNotNull("Entire doc was replaced because null update was not written", response.getResults().get(0).getFieldValue("single_s"));
  assertNull("Null update failed. Value still exists in document", response.getResults().get(0).getFieldValue("multi_ss"));
}
 
Example 16
Source Project: thoth   Source File: DocumentShrinker.java    License: BSD 3-Clause Clear License 6 votes vote down vote up
/**
 * Tag slower documents and add them to the shrank core
 */
private void tagAndAddSlowThothDocuments() throws IOException, SolrServerException {
  // Query to return top MAX_NUMBER_SLOW_THOTH_DOCS slower thoth documents
  QueryResponse qr = realTimeServer.query(
      new SolrQuery()
          .setQuery(createThothDocsAggregationQuery())
          .addSort(QTIME, SolrQuery.ORDER.desc)
          .setRows(MAX_NUMBER_SLOW_THOTH_DOCS)
  );

  for (SolrDocument solrDocument: qr.getResults()){
    SolrInputDocument si = ClientUtils.toSolrInputDocument(solrDocument);
    // Remove old ID and version
    si.removeField(ID);
    si.removeField("_version_");
    // Tag document as slow
    si.addField(SLOW_QUERY_DOCUMENT, true);
    LOG.debug("Adding slow query document for server " + serverDetail.getName());
    shrankServer.add(si);
  }
}
 
Example 17
Source Project: dubbox   Source File: SolrTemplate.java    License: Apache License 2.0 6 votes vote down vote up
private <T> SolrResultPage<T> createSolrResultPage(Query query, Class<T> clazz, QueryResponse response,
		Map<String, Object> objectsName) {
	List<T> beans = convertQueryResponseToBeans(response, clazz);
	SolrDocumentList results = response.getResults();
	long numFound = results == null ? 0 : results.getNumFound();
	Float maxScore = results == null ? null : results.getMaxScore();

	Pageable pageRequest = query.getPageRequest();

	SolrResultPage<T> page = new SolrResultPage<T>(beans, pageRequest, numFound, maxScore);

	page.setFieldStatsResults(
			ResultHelper.convertFieldStatsInfoToFieldStatsResultMap(response.getFieldStatsInfo()));
	page.setGroupResults(
			ResultHelper.convertGroupQueryResponseToGroupResultMap(query, objectsName, response, this, clazz));

	return page;
}
 
Example 18
Source Project: mtas   Source File: MtasSolrTestSearchConsistency.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Cql query parser.
 *
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
@org.junit.Test
public void cqlQueryParser() throws IOException {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("q",
      "{!mtas_cql field=\"mtas\" query=\"[pos=\\\"ADJ\\\"]{2}[pos=\\\"N\\\"]\"}");
  try {
    QueryResponse qResp1 = server.query("collection1", params);
    QueryResponse qResp2 = server.query("collection2", params);
    QueryResponse qResp3 = server.query("collection3", params);
    SolrDocumentList docList1 = qResp1.getResults();
    SolrDocumentList docList2 = qResp2.getResults();
    SolrDocumentList docList3 = qResp3.getResults();
    assertFalse(docList1.isEmpty());
    assertEquals(docList1.size(), (long) docList2.size() + docList3.size());
  } catch (SolrServerException e) {
    throw new IOException(e.getMessage(), e);
  }
}
 
Example 19
protected List<String> executeQuery(SolrQuery solrQuery) throws ApsSystemException {
  	List<String> contentsId = new ArrayList<String>();
  	try {
	HttpClient httpClient = new DefaultHttpClient();
	SolrServer solr = new HttpSolrServer(this.getSolrUrl(), httpClient, new XMLResponseParser());
	QueryResponse rsp = solr.query(solrQuery);
          SolrDocumentList solrDocumentList = rsp.getResults();
          for (SolrDocument doc : solrDocumentList) {
		String id = (String) doc.getFieldValue(CmsSearchEngineManager.CONTENT_ID_FIELD_NAME); //id is the uniqueKey field
              contentsId.add(id);
          }
  	} catch (SolrServerException e) {
	throw new ApsSystemException("Error on solr server", e);
} catch (Throwable t) {
  		throw new ApsSystemException("Error extracting response", t);
  	}
  	return contentsId;
  }
 
Example 20
Source Project: metron   Source File: SolrSearchDaoTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void getFacetCountsShouldProperlyReturnFacetCounts() {
  QueryResponse queryResponse = mock(QueryResponse.class);

  FacetField facetField1 = new FacetField("field1");
  facetField1.add("value1", 1);
  facetField1.add("value2", 2);
  FacetField facetField2 = new FacetField("field2");
  facetField2.add("value3", 3);
  facetField2.add("value4", 4);
  when(queryResponse.getFacetField("field1")).thenReturn(facetField1);
  when(queryResponse.getFacetField("field2")).thenReturn(facetField2);

  Map<String, Map<String, Long>> expectedFacetCounts = new HashMap<String, Map<String, Long>>() {{
    put("field1", new HashMap<String, Long>() {{
      put("value1", 1L);
      put("value2", 2L);
    }});
    put("field2", new HashMap<String, Long>() {{
      put("value3", 3L);
      put("value4", 4L);
    }});
  }};

  assertEquals(expectedFacetCounts,
      solrSearchDao.getFacetCounts(Arrays.asList("field1", "field2"), queryResponse));
}
 
Example 21
Source Project: lucene-solr   Source File: SimClusterStateProvider.java    License: Apache License 2.0 5 votes vote down vote up
public QueryResponse simQuery(QueryRequest req) throws SolrException, InterruptedException, IOException {
  ensureNotClosed();
  String collection = req.getCollection();
  if (collection == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collection not set");
  }
  ensureSystemCollection(collection);
  if (!colShardReplicaMap.containsKey(collection)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collection does not exist");
  }
  String query = req.getParams().get(CommonParams.Q);
  if (query == null || !query.equals("*:*")) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Only '*:*' query is supported");
  }
  ClusterState clusterState = getClusterState();
  DocCollection coll = clusterState.getCollection(collection);
  AtomicLong count = new AtomicLong();
  for (Slice s : coll.getActiveSlicesArr()) {
    Replica r = s.getLeader();
    if (r == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, collection + "/" + s.getName() + " has no leader");
    }
    ReplicaInfo ri = getReplicaInfo(r);
    Number numDocs = (Number)ri.getVariable("SEARCHER.searcher.numDocs", 0L);
    count.addAndGet(numDocs.longValue());
    AtomicLong bufferedUpdates = (AtomicLong)sliceProperties.get(collection).get(s.getName()).get(BUFFERED_UPDATES);
    if (bufferedUpdates != null) {
      count.addAndGet(bufferedUpdates.get());
    }
  }
  QueryResponse rsp = new QueryResponse();
  NamedList<Object> values = new NamedList<>();
  values.add("responseHeader", new NamedList<>());
  SolrDocumentList docs = new SolrDocumentList();
  docs.setNumFound(count.get());
  values.add("response", docs);
  rsp.setResponse(values);
  return rsp;
}
 
Example 22
Source Project: lucene-solr   Source File: RangeFacetCloudTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testInclude_EdgeLower() throws Exception {
  for (boolean doSubFacet : Arrays.asList(false, true)) {
    final Integer subFacetLimit = pickSubFacetLimit(doSubFacet);
    final CharSequence subFacet = makeSubFacet(subFacetLimit);
    for (EnumSet<FacetRangeOther> other : OTHERS) {
      final String otherStr = formatFacetRangeOther(other);
      for (String include : Arrays.asList(", include:'edge,lower'", ", include:[edge,lower]")) { // same
        final SolrQuery solrQuery = new SolrQuery
          ("q", "*:*", "rows", "0", "json.facet",
           // exclude a single low/high value from our ranges
           "{ foo:{ type:range, field:"+INT_FIELD+" start:1, end:4, gap:1"+otherStr+include+subFacet+" } }");
        
        final QueryResponse rsp = cluster.getSolrClient().query(solrQuery);
        try {
          @SuppressWarnings({"unchecked"})
          final NamedList<Object> foo = ((NamedList<NamedList<Object>>)rsp.getResponse().get("facets")).get("foo");
          @SuppressWarnings({"unchecked"})
          final List<NamedList<Object>> buckets = (List<NamedList<Object>>) foo.get("buckets");
          
          assertEquals("num buckets", 3, buckets.size());
          
          assertBucket("bucket#0", 1, modelVals(1), subFacetLimit, buckets.get(0));
          assertBucket("bucket#1", 2, modelVals(2), subFacetLimit, buckets.get(1));
          assertBucket("bucket#2", 3, modelVals(3,4), subFacetLimit, buckets.get(2));
          
          assertBeforeAfterBetween(other, modelVals(0), modelVals(5), modelVals(1,4), subFacetLimit, foo);
          
        } catch (AssertionError|RuntimeException ae) {
          throw new AssertionError(solrQuery.toString() + " -> " + rsp.toString() + " ===> " + ae.getMessage(), ae);
        }
      }
    }
  }
}
 
Example 23
@GET
@Path("link/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response retrieveLinkedObjects(@PathParam("id") String id, @QueryParam("start") Integer start,
    @QueryParam("rows") Integer rows) throws SolrServerException, IOException {
    // do solr query
    SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("start", start != null ? start : 0);
    params.set("rows", rows != null ? rows : 50);
    params.set("fl", "id");
    String configQuery = MCRConfiguration2.getString("MCR.Solr.linkQuery").orElse("category.top:{0}");
    String query = new MessageFormat(configQuery, Locale.ROOT).format(new String[] { id.replaceAll(":", "\\\\:") });
    params.set("q", query);
    QueryResponse solrResponse = solrClient.query(params);
    SolrDocumentList solrResults = solrResponse.getResults();
    // build json response
    JsonObject response = new JsonObject();
    response.addProperty("numFound", solrResults.getNumFound());
    response.addProperty("start", solrResults.getStart());
    JsonArray docList = new JsonArray();
    for (SolrDocument doc : solrResults) {
        docList.add(new JsonPrimitive((String) doc.getFieldValue("id")));
    }
    response.add("docs", docList);
    return Response.ok().entity(response.toString()).build();
}
 
Example 24
/**
 * Cql query parser.
 */
@org.junit.Test
public void cqlQueryParser() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("q",
      "{!mtas_cql field=\"mtas\" query=\"[pos=\\\"ADJ\\\"]{2}[pos=\\\"N\\\"]\"}");
  Map<String, QueryResponse> list = createResults(params, null);
  assertEquals(list.get(COLLECTION_ALL_OPTIMIZED).getResults().size(),
      list.get(COLLECTION_ALL_MULTIPLE_SEGMENTS).getResults().size());
  assertEquals(list.get(COLLECTION_ALL_OPTIMIZED).getResults().size(),
      list.get(COLLECTION_PART1_OPTIMIZED).getResults().size(),
      list.get(COLLECTION_PART2_MULTIPLE_SEGMENTS).getResults().size());
  assertEquals(list.get(COLLECTION_ALL_OPTIMIZED).getResults().size(),
      list.get(COLLECTION_DISTRIBUTED).getResults().size());
}
 
Example 25
Source Project: incubator-sentry   Source File: TestRealTimeGet.java    License: Apache License 2.0 5 votes vote down vote up
private void assertIdVsIds(ExpectedResult expectedResult, ExpectedResult controlExpectedResult)
    throws Exception {
  // test specifying with "id"
  QueryResponse idRsp = getIdResponse(expectedResult);
  QueryResponse idControlRsp = getIdResponse(controlExpectedResult);
  assertExpected(expectedResult, idRsp, idControlRsp);

  // test specifying with "ids"
  QueryResponse idsRsp = getIdsResponse(expectedResult);
  QueryResponse idsControlRsp = getIdsResponse(controlExpectedResult);
  assertExpected(expectedResult, idsRsp, idsControlRsp);
}
 
Example 26
Source Project: lucene-solr   Source File: ReindexCollectionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLossySchema() throws Exception {
  final String sourceCollection = "sourceLossyReindexing";
  final String targetCollection = "targetLossyReindexing";


  createCollection(sourceCollection, "conf2", 2, 2);

  indexDocs(sourceCollection, NUM_DOCS, i ->
    new SolrInputDocument(
        "id", String.valueOf(i),
        "string_s", String.valueOf(i),
        "sind", "this is a test " + i)); // "sind": indexed=true, stored=false, will be lost...

  CollectionAdminRequest.ReindexCollection req = CollectionAdminRequest.reindexCollection(sourceCollection)
      .setTarget(targetCollection)
      .setConfigName("conf3");
  req.process(solrClient);

  CloudUtil.waitForState(cloudManager, "did not finish copying in time", targetCollection, (liveNodes, coll) -> {
    ReindexCollectionCmd.State state = ReindexCollectionCmd.State.get(coll.getStr(ReindexCollectionCmd.REINDEXING_STATE));
    return ReindexCollectionCmd.State.FINISHED == state;
  });
  // verify the target docs exist
  QueryResponse rsp = solrClient.query(targetCollection, params(CommonParams.Q, "*:*"));
  assertEquals("copied num docs", NUM_DOCS, rsp.getResults().getNumFound());
  for (SolrDocument doc : rsp.getResults()) {
    String id = (String)doc.getFieldValue("id");
    assertEquals(id, doc.getFieldValue("string_s"));
    assertFalse(doc.containsKey("sind")); // lost in translation ...
  }
}
 
Example 27
Source Project: lucene-solr   Source File: CloudMLTQParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testHighWLValue() throws Exception {

  // Test out a high value of wl and make sure nothing matches.
  QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION,
      new SolrQuery("{!mlt qf=lowerfilt_u minwl=4 mintf=1}3"));
  SolrDocumentList solrDocuments = queryResponse.getResults();
  assertEquals("Expected to match 0 documents with a minwl of 4 but found more", solrDocuments.size(), 0);

}
 
Example 28
Source Project: ambari-logsearch   Source File: ServiceLogsManager.java    License: Apache License 2.0 5 votes vote down vote up
private <T extends LogData> List<T> getLogDataListByFieldType(Class<T> clazz, QueryResponse response, List<Count> fieldList) {
  List<T> groupList = getComponentBeans(clazz, response);
  for (Count cnt : fieldList) {
    T logData = createNewFieldByType(clazz, cnt);
    groupList.add(logData);
  }
  return groupList;
}
 
Example 29
Source Project: lucene-solr   Source File: TestCloudPivotFacet.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Executes a query and compares the results with the data available in the 
 * {@link PivotField} constraint -- this method is not recursive, and doesn't 
 * check anything about the sub-pivots (if any).
 *
 * @param pivotName pivot name
 * @param constraint filters on pivot
 * @param params base solr parameters
 */
private void assertPivotData(String pivotName, PivotField constraint, SolrParams params)
    throws SolrServerException, IOException {
  
  SolrParams p = SolrParams.wrapDefaults(params("rows","0"), params);
  QueryResponse res = cloudClient.query(p);
  String msg = pivotName + ": " + p;

  assertNumFound(msg, constraint.getCount(), res);

  if ( p.getBool(StatsParams.STATS, false) ) {
    // only check stats if stats expected
    assertPivotStats(msg, constraint, res);
  }
}
 
Example 30
Source Project: lucene-solr   Source File: DistributedDebugComponentTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testCompareWithNonDistributedRequest() throws SolrServerException, IOException {
  SolrQuery query = new SolrQuery();
  query.setQuery("id:1 OR id:2");
  query.setFilterQueries("id:[0 TO 10]", "id:[0 TO 5]");
  query.setRows(1);
  query.setSort("id", SolrQuery.ORDER.asc); // thus only return id:1 since rows 1
  query.set("debug",  "true");
  query.set("distrib", "true");
  query.setFields("id");
  if (random().nextBoolean()) { // can affect rb.onePassDistributedQuery
    query.addField("text");
  }
  query.set(ShardParams.DISTRIB_SINGLE_PASS, random().nextBoolean());
  query.set("shards", shard1 + "," + shard2);
  QueryResponse distribResponse = collection1.query(query);
  
  // same query but not distributed
  query.set("distrib", "false");
  query.remove("shards");
  QueryResponse nonDistribResponse = collection1.query(query);
  
  assertNotNull(distribResponse.getDebugMap().get("track"));
  assertNull(nonDistribResponse.getDebugMap().get("track"));
  assertEquals(distribResponse.getDebugMap().size() - 1, nonDistribResponse.getDebugMap().size());
  
  assertSectionEquals(distribResponse, nonDistribResponse, "explain");
  assertSectionEquals(distribResponse, nonDistribResponse, "rawquerystring");
  assertSectionEquals(distribResponse, nonDistribResponse, "querystring");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsedquery");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsedquery_toString");
  assertSectionEquals(distribResponse, nonDistribResponse, "QParser");
  assertSectionEquals(distribResponse, nonDistribResponse, "filter_queries");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsed_filter_queries");
  
  // timing should have the same sections:
  assertSameKeys((NamedList<?>)nonDistribResponse.getDebugMap().get("timing"), (NamedList<?>)distribResponse.getDebugMap().get("timing"));
}