解析SolrJ Facet Pivot响应

时间:2016-07-12 17:57:50

标签: solr solrj

我有一个Solr Schema,我试图通过在通过SolrJ进行调用时在facet.pivot参数中传递多个字段来对其进行Facet Pivoting。我的Solr响应如下所示:

"facet_pivot": {
  "boolean_value,int_value": [
    {
      "field": "boolean_value",
      "value": false,
      "count": 1,
      "pivot": [
        {
          "field": "int_value",
          "value": 364,
          "count": 1
        }
      ]
    },
    {
      "field": "boolean_value",
      "value": true,
      "count": 2,
      "pivot": [
        {
          "field": "int_value",
          "value": 406,
          "count": 1
        },
        {
          "field": "int_value",
          "value": 409,
          "count": 1
        }
      ]
    }
  ]
}

如何使用SolrJ以嵌套对象的形式解析上述响应,即具有int_value属于boolean_value的关系的PivotField。

SolrJ Version尝试:4.10.4

更新

当您通过SolrJ进行调用时,请检查生成的SolrQuery。在上面的例子中,SolrQuery是:

facet.pivot = boolean_value& facet.pivot = int_value

Solr认为上面的枢轴是两个不同的枢轴,你不会得到嵌套的旋转。对于嵌套透视,你的SolrQuery应该有

facet.pivot = boolean_value,int_value

1 个答案:

答案 0 :(得分:0)

我有同样的问题,写了一个简单的测试应用程序。它查询Solr并将透视值作为分层字符串写入StdOut。如果有任何建议可以更好地实施,请告诉我。这是代码:

public class Tester {

    public static final String HIERARCHICAL_FACET_SEPARATOR = "/";

    public static void main(String[] args) throws SolrServerException, IOException {
        CloudSolrClient solr = ... ;
        SolrQuery query = new SolrQuery(...);
        query.setFacet(true);
        query.addFacetPivotField(new String[]{"field1,field2,field3"});
        QueryResponse result = solr.query(query);
        NamedList<List<PivotField>> facetPivot = result.getFacetPivot();
        List<String> parsedPivotResult = parsePivotResult(facetPivot);
        parsedPivotResult.forEach((s) -> {
            System.out.println(s);
        });
    }

    private static List<String> parsePivotResult(final NamedList<List<PivotField>> pivotEntryList) {
        final Set<String> outputItems = new HashSet<>();
        for (final Entry<String, List<PivotField>> pivotEntry : pivotEntryList) {
            System.out.println("Key: " + pivotEntry.getKey());
            pivotEntry.getValue().forEach((pivotField) -> {
                renderOutput(new StringBuilder(), pivotField, outputItems);
            });
        }
        final List<String> output = new ArrayList<>(outputItems);
        Collections.sort(output);
        return output;
    }

    private static void renderOutput(final StringBuilder sb, final PivotField field, final Set<String> outputItems) {
        final String fieldValue = field.getValue() != null ? ((String) field.getValue()).trim() : null;
        final StringBuilder outputBuilder = new StringBuilder(sb);
        if (field.getPivot() != null) {
            if (outputBuilder.length() > 0) {
                outputBuilder.append(HIERARCHICAL_FACET_SEPARATOR);
            }
            outputBuilder.append(fieldValue);
            outputItems.add(new StringBuilder(outputBuilder).append(" (").append(field.getCount()).append(")").toString());
            field.getPivot().forEach((subField) -> {
                renderOutput(outputBuilder, subField, outputItems);
            });
        } else {
            if (outputBuilder.length() > 0) {
                outputBuilder.append(HIERARCHICAL_FACET_SEPARATOR);
            }
            outputBuilder.append(fieldValue);
            outputItems.add(outputBuilder.append(" (").append(field.getCount()).append(")").toString());
        }
    }
}