Java Code Examples for org.apache.solr.client.solrj.response.QueryResponse#getFacetPivot()

The following examples show how to use org.apache.solr.client.solrj.response.QueryResponse#getFacetPivot() . 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
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public BarGraphDataListResponse generateBarGraphDataResponseWithRanges(QueryResponse response, String typeField, boolean typeUppercase) {
  BarGraphDataListResponse dataList = new BarGraphDataListResponse();
  if (response == null) {
    return dataList;
  }
  NamedList<List<PivotField>> facetPivotResponse = response.getFacetPivot();
  if (response.getFacetPivot() == null) {
    return dataList;
  }
  List<PivotField> pivotFields = facetPivotResponse.get(typeField);
  for (int pivotIndex = 0; pivotIndex < pivotFields.size(); pivotIndex++) {
    PivotField pivotField = facetPivotResponse.get(typeField).get(pivotIndex);
    List<NameValueData> nameValues = generateNameValueDataList(pivotField.getFacetRanges());
    BarGraphData barGraphData = new BarGraphData();
    barGraphData.setDataCount(nameValues);
    String typeValue = typeUppercase ? StringUtils.upperCase(pivotField.getValue().toString()) : pivotField.getValue().toString();
    barGraphData.setName(typeValue);
    dataList.getGraphData().add(barGraphData);
  }
  return dataList;
}
 
Example 2
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public BarGraphDataListResponse generateSecondLevelBarGraphDataResponse(QueryResponse response, int val) {
  BarGraphDataListResponse barGraphDataListResponse = new BarGraphDataListResponse();
  NamedList<List<PivotField>> pivotFieldNameList = response.getFacetPivot();
  if (pivotFieldNameList == null) {
    return barGraphDataListResponse;
  }
  List<PivotField> pivotFields = pivotFieldNameList.getVal(val);
  List<BarGraphData> barGraphDataList = new ArrayList<>();
  for (PivotField pivotField : pivotFields) {
    BarGraphData barGraphData = new BarGraphData();
    barGraphData.setName(String.valueOf(pivotField.getValue()));
    List<PivotField> secondLevelPivotFields = pivotField.getPivot();
    List<NameValueData> nameValueDataList = new ArrayList<>();
    for (PivotField sPivotField : secondLevelPivotFields) {
      NameValueData nvD = new NameValueData();
      nvD.setName(String.valueOf(sPivotField.getValue()));
      nvD.setValue(String.valueOf(sPivotField.getCount()));
      nameValueDataList.add(nvD);
    }
    barGraphData.setDataCount(nameValueDataList);
    barGraphDataList.add(barGraphData);
  }
  barGraphDataListResponse.setGraphData(barGraphDataList);
  return barGraphDataListResponse;
}
 
Example 3
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public NodeListResponse generateServiceNodeTreeFromFacetResponse(QueryResponse queryResponse,
                                                                 String firstHierarchy, String secondHierarchy,
                                                                 String firstType, String secondType) {
  NodeListResponse response = new NodeListResponse();
  if (queryResponse == null) {
    return response;
  }
  NamedList<List<PivotField>> namedPivotFieldList = queryResponse.getFacetPivot();
  List<PivotField> firstLevelPivots = namedPivotFieldList.get(firstHierarchy);
  List<PivotField> secondLevelPivots = namedPivotFieldList.get(secondHierarchy);
  if (!CollectionUtils.isNotEmpty(firstLevelPivots) || !CollectionUtils.isNotEmpty(secondLevelPivots)) {
    return response;
  }
  List<NodeData> nodeDataList = buidTreeData(firstLevelPivots, secondLevelPivots, firstType, secondType);
  response.setvNodeList(nodeDataList);
  return response;
}
 
Example 4
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public GraphDataListResponse generateSimpleGraphResponse(QueryResponse response, String hierarchy) {
  GraphDataListResponse graphInfo = new GraphDataListResponse();
  if (response == null) {
    return graphInfo;
  }
  List<List<PivotField>> hirarchicalPivotField = new ArrayList<>();
  List<GraphData> dataList = new ArrayList<>();
  NamedList<List<PivotField>> namedList = response.getFacetPivot();
  if (namedList != null) {
    hirarchicalPivotField = namedList.getAll(hierarchy);
  }
  if (!hirarchicalPivotField.isEmpty()) {
    dataList = buidGraphData(hirarchicalPivotField.get(0));
  }
  if (!dataList.isEmpty()) {
    graphInfo.setGraphData(dataList);
  }

  return graphInfo;
}
 
Example 5
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public HostLogFilesResponse generateHostLogFilesResponse(QueryResponse queryResponse) {
  HostLogFilesResponse response = new HostLogFilesResponse();
  Map<String, List<String>> componentLogFiles = response.getHostLogFiles();
  
  NamedList<List<PivotField>> facetPivot = queryResponse.getFacetPivot();
  List<PivotField> componentFields = facetPivot.get(COMPONENT + "," + PATH);
  for (PivotField componentField : componentFields) {
    String component = (String)componentField.getValue();
    LinkedList<String> logFileList = new LinkedList<>();
    componentLogFiles.put(component, logFileList);
    
    for (PivotField logField : componentField.getPivot()) {
      // the log file names are in increasing order of their cardinality, using addFirst reverses the list
      logFileList.addFirst((String)logField.getValue());
    }
  }
  
  return response;
}
 
Example 6
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void pivotFaceting_mincountSetTwo_shouldReturnFacetsOriginalMincount() throws Exception
{
    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.pivot", "{http://www.alfresco.org/model/content/1.0}content,{http://www.alfresco.org/model/content/1.0}name","facet.pivot.mincount","2"));

    NamedList<List<PivotField>> facetPivot = queryResponse.getFacetPivot();

    List<PivotField> firstLevelValues = facetPivot.getVal(0);
    Assert.assertThat(firstLevelValues.size(), is(1));
    PivotField firstLevelPivot0 = firstLevelValues.get(0);
    Assert.assertThat(firstLevelPivot0.getValue(), is("contenttwo"));
    Assert.assertThat(firstLevelPivot0.getCount(), is(4));

    List<PivotField> firstLevelPivot0Children = firstLevelPivot0.getPivot();
    Assert.assertThat(firstLevelPivot0Children.size(), is(1));
    PivotField secondLevelPivot0 = firstLevelPivot0Children.get(0);
    Assert.assertThat(secondLevelPivot0.getValue(), is("nametwo"));
    Assert.assertThat(secondLevelPivot0.getCount(), is(4));
}
 
Example 7
Source File: ThothServers.java    From thoth with BSD 3-Clause Clear License 6 votes vote down vote up
public ArrayList<ServerDetail> getList(SolrServer realTimeThoth) throws SolrServerException {
  ArrayList<ServerDetail> serverDetails = new ArrayList<ServerDetail>();
  // Using HierarchicalFaceting to fetch server details .http://wiki.apache.org/solr/HierarchicalFaceting
  QueryResponse qr = realTimeThoth.query(new SolrQuery("*:*").addFacetPivotField(FACET_PIVOT_FIELDS).setRows(0).setFacetLimit(FACET_LIMIT));
  NamedList<List<PivotField>> pivots = qr.getFacetPivot();
  System.out.println("Found " + pivots.get(FACET_PIVOT_FIELDS).size()+" servers to monitor. Fetching information for these servers. Please wait");
  for (PivotField pivot: pivots.get(FACET_PIVOT_FIELDS)){
    String hostname = (String) pivot.getValue();
    for (PivotField pf: pivot.getPivot()){
      String coreName = (String) pf.getValue();
      ServerDetail detail = fetchServerDetails(hostname,coreName, realTimeThoth);
      if (detail != null) serverDetails.add(detail);
    }
  }
  return serverDetails;
}
 
Example 8
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public NodeListResponse generateOneLevelServiceNodeTree(QueryResponse queryResponse, String componentLevelHirachy) {
  NodeListResponse response = new NodeListResponse();
  List<NodeData> datatList = new ArrayList<>();
  List<List<PivotField>> listPivotField = new ArrayList<>();
  NamedList<List<PivotField>> namedList = queryResponse.getFacetPivot();
  if (namedList != null) {
    listPivotField = namedList.getAll(componentLevelHirachy);
  }
  List<PivotField> secondHirarchicalPivotFields = null;
  if (listPivotField == null || listPivotField.isEmpty()) {
    return response;
  } else {
    secondHirarchicalPivotFields = listPivotField.get(0);
  }
  for (PivotField singlePivotField : secondHirarchicalPivotFields) {
    if (singlePivotField != null) {
      NodeData comp = new NodeData();
      comp.setName("" + singlePivotField.getValue());
      List<PivotField> levelList = singlePivotField.getPivot();
      List<NameValueData> levelCountList = new ArrayList<>();
      comp.setLogLevelCount(levelCountList);
      if (levelList != null) {
        for (PivotField levelPivot : levelList) {
          NameValueData level = new NameValueData();
          level.setName(("" + levelPivot.getValue()).toUpperCase());
          level.setValue("" + levelPivot.getCount());
          levelCountList.add(level);
        }
      }
      datatList.add(comp);
    }
  }
  response.setvNodeList(datatList);
  return response;
}
 
Example 9
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public ServiceComponentMetadataWrapper generateGroupedComponentMetadataResponse(QueryResponse queryResponse, String pivotFields,
                                                                                Map<String, String> groupLabels,
                                                                                Map<String, String> componentLabels) {
  List<ComponentMetadata> componentMetadata = new ArrayList<>();
  Map<String, String> groupsMetadata = new HashMap<>();

  if (queryResponse == null) {
    return new ServiceComponentMetadataWrapper(componentMetadata, groupsMetadata);
  }
  NamedList<List<PivotField>> facetPivotResponse = queryResponse.getFacetPivot();
  if (facetPivotResponse == null || facetPivotResponse.size() < 1) {
    return new ServiceComponentMetadataWrapper(componentMetadata, groupsMetadata);
  }
  if (CollectionUtils.isEmpty(facetPivotResponse.get(pivotFields))) {
    return new ServiceComponentMetadataWrapper(componentMetadata, groupsMetadata);
  }

  for (PivotField pivotField : facetPivotResponse.get(pivotFields)) {
    if (pivotField != null && pivotField.getValue() != null) {
      String componentName = pivotField.getValue().toString();
      String groupName = null;
      if (CollectionUtils.isNotEmpty(pivotField.getPivot())) {
        Object groupValue = pivotField.getPivot().get(0).getValue();
        if (groupValue != null) {
          groupName = groupValue.toString();
          groupsMetadata.put(groupName, groupLabels.getOrDefault(groupName, null));
        }
      }
      String label = componentLabels.get(componentName);
      String fallbackedLabel = labelFallbackHandler.fallbackIfRequired(componentName, label, true, false, true);
      componentMetadata.add((new ComponentMetadata(componentName, fallbackedLabel, groupName)));

    }
  }

  return new ServiceComponentMetadataWrapper(componentMetadata, groupsMetadata);
}
 
Example 10
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void pivotFaceting_mincountMissing_shouldReturnFacetsMincountOne() throws Exception
{
    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.pivot", "{http://www.alfresco.org/model/content/1.0}content,{http://www.alfresco.org/model/content/1.0}name"));

    NamedList<List<PivotField>> facetPivot = queryResponse.getFacetPivot();
    
    List<PivotField> firstLevelValues = facetPivot.getVal(0);
    Assert.assertThat(firstLevelValues.size(), is(2));
    PivotField firstLevelPivot0 = firstLevelValues.get(0);
    Assert.assertThat(firstLevelPivot0.getValue(), is("contenttwo"));
    Assert.assertThat(firstLevelPivot0.getCount(), is(4));
    
    List<PivotField> firstLevelPivot0Children = firstLevelPivot0.getPivot();
    Assert.assertThat(firstLevelPivot0Children.size(), is(1));
    PivotField secondLevelPivot0 = firstLevelPivot0Children.get(0);
    Assert.assertThat(secondLevelPivot0.getValue(), is("nametwo"));
    Assert.assertThat(secondLevelPivot0.getCount(), is(4));

    PivotField firstLevelPivot1 = firstLevelValues.get(1);
    Assert.assertThat(firstLevelPivot1.getValue(), is("contentone"));
    Assert.assertThat(firstLevelPivot1.getCount(), is(1));
    
    List<PivotField> firstLevelPivot1Children = firstLevelPivot1.getPivot();
    Assert.assertThat(firstLevelPivot1Children.size(), is(1));
    PivotField secondLevelPivot1 = firstLevelPivot1Children.get(0);
    Assert.assertThat(secondLevelPivot1.getValue(), is("nameone"));
    Assert.assertThat(secondLevelPivot1.getCount(), is(1));
}
 
Example 11
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void pivotFaceting_mincountSetZero_shouldReturnFacetsMincountOne() throws Exception
{
    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.pivot", "{http://www.alfresco.org/model/content/1.0}content,{http://www.alfresco.org/model/content/1.0}name","facet.pivot.mincount","0"));

    NamedList<List<PivotField>> facetPivot = queryResponse.getFacetPivot();

    List<PivotField> firstLevelValues = facetPivot.getVal(0);
    Assert.assertThat(firstLevelValues.size(), is(2));
    PivotField firstLevelPivot0 = firstLevelValues.get(0);
    Assert.assertThat(firstLevelPivot0.getValue(), is("contenttwo"));
    Assert.assertThat(firstLevelPivot0.getCount(), is(4));

    List<PivotField> firstLevelPivot0Children = firstLevelPivot0.getPivot();
    Assert.assertThat(firstLevelPivot0Children.size(), is(1));
    PivotField secondLevelPivot0 = firstLevelPivot0Children.get(0);
    Assert.assertThat(secondLevelPivot0.getValue(), is("nametwo"));
    Assert.assertThat(secondLevelPivot0.getCount(), is(4));

    PivotField firstLevelPivot1 = firstLevelValues.get(1);
    Assert.assertThat(firstLevelPivot1.getValue(), is("contentone"));
    Assert.assertThat(firstLevelPivot1.getCount(), is(1));

    List<PivotField> firstLevelPivot1Children = firstLevelPivot1.getPivot();
    Assert.assertThat(firstLevelPivot1Children.size(), is(1));
    PivotField secondLevelPivot1 = firstLevelPivot1Children.get(0);
    Assert.assertThat(secondLevelPivot1.getValue(), is("nameone"));
    Assert.assertThat(secondLevelPivot1.getCount(), is(1));
}
 
Example 12
Source File: Solr.java    From ambari-logsearch with Apache License 2.0 4 votes vote down vote up
public NamedList<List<PivotField>> executeFacetQuery(SolrQuery solrQuery) throws SolrServerException, IOException {
  QueryResponse qResp = server.query(solrQuery);
  return qResp.getFacetPivot();
}
 
Example 13
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testPivotFacetsQueries() throws Exception {
  SolrClient client = getSolrClient();

  // Empty the database...
  client.deleteByQuery("*:*");// delete everything!
  client.commit();
  assertNumFound("*:*", 0); // make sure it got in

  int id = 1;
  ArrayList<SolrInputDocument> docs = new ArrayList<>();
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true, "popularity", 12, "price", .017));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", false, "popularity", 13, "price", 16.04));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true, "popularity", 14, "price", 12.34));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", false, "popularity", 24, "price", 51.39));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", true, "popularity", 28, "price", 131.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", false, "popularity", 32));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", true, "popularity", 31, "price", 131.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 36));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", 37, "price", 1.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 38, "price", 47.98));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", -38));
  docs.add(makeTestDoc("id", id++, "cat", "b")); // something not matching all fields
  client.add(docs);
  client.commit();

  SolrQuery query = new SolrQuery("*:*");
  query.addFacetPivotField("{!query=s1}features,manu");
  query.addFacetQuery("{!key=highPrice tag=s1}price:[100 TO *]");
  query.addFacetQuery("{!tag=s1 key=lowPrice}price:[0 TO 50]");
  query.setFacetMinCount(0);
  query.setRows(0);
  QueryResponse rsp = client.query(query);

  Map<String,Integer> map = rsp.getFacetQuery();
  assertEquals(2, map.get("highPrice").intValue());
  assertEquals(5, map.get("lowPrice").intValue());
  
  NamedList<List<PivotField>> pivots = rsp.getFacetPivot();
  List<PivotField> pivotValues = pivots.get("features,manu");

  PivotField featuresBBBPivot = pivotValues.get(0);
  assertEquals("features", featuresBBBPivot.getField());
  assertEquals("bbb", featuresBBBPivot.getValue());
  assertNotNull(featuresBBBPivot.getFacetQuery());
  assertEquals(2, featuresBBBPivot.getFacetQuery().size());
  assertEquals(1, featuresBBBPivot.getFacetQuery().get("highPrice").intValue());
  assertEquals(2, featuresBBBPivot.getFacetQuery().get("lowPrice").intValue());
  
  PivotField featuresAAAPivot = pivotValues.get(1);
  assertEquals("features", featuresAAAPivot.getField());
  assertEquals("aaa", featuresAAAPivot.getValue());
  assertNotNull(featuresAAAPivot.getFacetQuery());
  assertEquals(2, featuresAAAPivot.getFacetQuery().size());
  assertEquals(1, featuresAAAPivot.getFacetQuery().get("highPrice").intValue());
  assertEquals(3, featuresAAAPivot.getFacetQuery().get("lowPrice").intValue());
}