spring Data JPA 集成solr7(四)

4. Miscellaneous Solr Operation Support

This chapter covers additional support for Solr operations (such as faceting) that cannot be directly accessed via the repository interface. It is recommended to add those operations as custom implementation as described in Custom Implementations for Spring Data Repositories .

4.1. Partial Updates

PartialUpdates can be done using PartialUpdate which implements Update.

PartialUpdate update = new PartialUpdate("id", "123");
update.add("name", "updated-name");
solrTemplate.saveBean("collection-1", update);

4.2. Projection

Projections can be applied via @Query using the fields value.

@Query(fields = { "name", "id" })
List<ProductBean> findByNameStartingWith(String name);

4.3. Faceting

Faceting cannot be directly applied using the SolrRepository but the SolrTemplate holds support for this feature.

FacetQuery query = new SimpleFacetQuery(new Criteria(Criteria.WILDCARD).expression(Criteria.WILDCARD))
  .setFacetOptions(new FacetOptions().addFacetOnField("name").setFacetLimit(5));
FacetPage<Product> page = solrTemplate.queryForFacetPage("collection-1", query, Product.class);

Facets on fields and/or queries can also be defined using @Facet . Please mind that the result will be a FacetPage . NOTE: Using @Facet allows you to define place holders which will use your input parameter as value.

@Query(value = "*:*")
@Facet(fields = { "name" }, limit = 5)
FacetPage<Product> findAllFacetOnName(Pageable page);
@Query(value = "popularity:?0")
@Facet(fields = { "name" }, limit = 5, prefix="?1")
FacetPage<Product> findByPopularityFacetOnName(int popularity, String prefix, Pageable page);

Solr allows definition of facet parameters on a per field basis. In order to add special facet options to defined fields use FieldWithFacetParameters.

// produces: f.name.facet.prefix=spring
FacetOptions options = new FacetOptions();
options.addFacetOnField(new FieldWithFacetParameters("name").setPrefix("spring"));
4.3.1. Range Faceting

Range faceting queries may be done by configure required ranges on FacetOptions. A simple way to request ranges would be by creating a FacetOption, setting this options to a FacetQuery and query for a facet page through SolrTemplate as follows.

FacetOptions facetOptions = new FacetOptions()
  .addFacetByRange(
     new FieldWithNumericRangeParameters("price", 5, 20, 5)
       .setHardEnd(true)
       .setInclude(FacetRangeInclude.ALL)
  )
  .addFacetByRange(
    new FieldWithDateRangeParameters("release", new Date(1420070400), new Date(946684800), "+1YEAR")
      .setInclude(FacetRangeInclude.ALL)
      .setOther(FacetRangeOther.BEFORE)
  );
facetOptions.setFacetMinCount(0);

Criteria criteria = new SimpleStringCriteria("*:*");
SimpleFacetQuery facetQuery = new SimpleFacetQuery(criteria).setFacetOptions(facetOptions);
FacetPage<ExampleSolrBean> statResultPage = solrTemplate.queryForFacetPage("collection-1", facetQuery, ExampleSolrBean.class);

There are two implementations of fields for facet range requests:

  • Numeric Facet Range - used to perform range faceting over numeric fields. To request such range faceting an instance of the class org.springframework.data.solr.core.query.FacetOptions.FieldWithNumericRangeParameters can be used. Its instantiation requires a field name, a start value (number), end value (number) and gap (number);

  • Date Facet Range - used to perform range faceting over date fields. To request such range faceting an instance of the class org.springframework.data.solr.core.query.FacetOptions.FieldWithDateRangeParameters can be used. Its instantiation requires a field name, a start value (date), end value (date) and gap (string). The gap for this kind of field can be defined using org.apache.solr.util.DateMathParser (i.e. +6MONTHS+3DAYS/DAY, that would mean 6 months and 3 days in the future from now, rounded down to nearest day).

Additionally the following properties can be configured for a field with range parameters (org.springframework.data.solr.core.query.FacetOptions.FieldWithRangeParameters):

  • Hard End - setHardEnd(Boolean), defines if the last range should be abruptly ended even if the end doesn’t satisfies: (start - end) % gap = 0;

  • Include - setInclude(org.apache.solr.common.params.FacetParams.FacetRangeInclude), defines how boundaries (lower and upper) shall be handled (exclusive or inclusive) on range facet requests;

  • Other - setOther(org.apache.solr.common.params.FacetParams.FacetRangeOther), defines the additional (other) counts for the range facet, i.e. count of documents that are before start of the range facet, end of range facet or even between start and end.

4.3.2. Pivot Faceting

Pivot faceting (Decision Tree) are also supported, and can be queried using @Facet annotation as follows:

public interface {

	@Facet(pivots = @Pivot({ "category", "dimension" }, pivotMinCount = 0))
	FacetPage<Product> findByTitle(String title, Pageable page);

	@Facet(pivots = @Pivot({ "category", "dimension" }))
	FacetPage<Product> findByDescription(String description, Pageable page);

}

Alternatively it can be queried using SolrTemplate as follows:

FacetQuery facetQuery = new SimpleFacetQuery(new SimpleStringCriteria("title:foo"));
FacetOptions facetOptions = new FacetOptions();
facetOptions.setFacetMinCount(0);
facetOptions.addFacetOnPivot("category","dimension");
facetQuery.setFacetOptions(facetOptions);
FacetPage<Product> facetResult = solrTemplate.queryForFacetPage("collection-1", facetQuery, Product.class);

In order to retrieve the pivot results the method getPivot can be used as follows:

List<FacetPivotFieldEntry> pivot = facetResult.getPivot(new SimplePivotField("categories","available"));

4.4. Terms

Terms Vector cannot directly be used within SolrRepository but can be applied via SolrTemplate. Please mind, that the result will be a TermsPage.

TermsQuery query = SimpleTermsQuery.queryBuilder().fields("name").build();
TermsPage page = solrTemplate.queryForTermsPage("collection-1", query);

4.5. Result Grouping / Field Collapsing

Result grouping cannot directly be used within SolrRepository but can be applied via SolrTemplate. Please mind, that the result will be a GroupPage.

Field field = new SimpleField("popularity");
Function func = ExistsFunction.exists("description");
Query query = new SimpleQuery("inStock:true");

SimpleQuery groupQuery = new SimpleQuery(new SimpleStringCriteria("*:*"));
GroupOptions groupOptions = new GroupOptions()
	.addGroupByField(field)
	.addGroupByFunction(func)
	.addGroupByQuery(query);
groupQuery.setGroupOptions(groupOptions);

GroupPage<Product> page = solrTemplate.queryForGroupPage("collection-1", query, Product.class);

GroupResult<Product> fieldGroup = page.getGroupResult(field);
GroupResult<Product> funcGroup = page.getGroupResult(func);
GroupResult<Product> queryGroup = page.getGroupResult(query);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值