SOLR-按字段分组,然后通过另一个字段获得不同的值

时间:2018-08-07 11:43:46

标签: database search solr nosql

我正在使用apache solr来搜索记录。就我而言,我的表具有列类别和子类别等。

我想按类别分组,然后从分组结果中获得不同的子类别列表。在Apache Solr中有可能吗?

如果是,请帮助我解决此问题。 预先感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用透视面来实现:

import java.util.Optional;
import org.junit.Assert;
import org.junit.Test;
import org.modelmapper.ModelMapper;

public class TestMappingConditionalGettersWithModelMapper {

  @Test
  public void testConditionalGetter() {
    ModelMapper modelMapper = new ModelMapper();

    modelMapper.createTypeMap(A.class, FlatAB.class).addMappings(mapper -> {

      // mapper.map(a -> a.getB() != null ? a.getB().getDescription() : a.getDescription(), FlatAB::setDescription);

      mapper.map(a -> Optional
          .ofNullable(a.getB())
          .map(B::getDescription)
          .orElseGet(a::getDescription), FlatAB::setDescription);
    });

    // first try A with no relationship
    A a = new A();
    a.setDescription("description of A");
    FlatAB flatAB1 = modelMapper.map(a, FlatAB.class);
    Assert.assertEquals("they should equal", a.getDescription(), flatAB1.getDescription());


    // now try it WITH a relationship
    A a2 = new A();
    a2.setDescription("description of A2");
    B b = new B();
    b.setDescription("description of B");
    a2.setB(b);
    FlatAB flatAB2 = modelMapper.map(a2, FlatAB.class);
    Assert.assertEquals("they should equal", b.getDescription(), flatAB2.getDescription());
  }

}

class A {

  private String description;
  private B b;

  /*
   .....
   .....
   many other properties
   .....
   ....
   */

  public B getB() {
    return b;
  }

  public void setB(B b) {
    this.b = b;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

}

class B {

  private String description;

  /*
   .....
   .....
   many other properties
   .....
   ....
   */

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }
}

这将使您全面了解每个类别的所有子类别。

您也可以使用Facet JSON API。该页面采用的示例:

facet=on&facet.pivot=category,subcategory