solrj API的使用(增删改查)
注:solrj 为 7.7版本,与solr版本一致
使用了Lombok、Swagger-ui
添加依赖
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.0</version>
</dependency>
实体类
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.solr.client.solrj.beans.Field;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SolrGoodsDto {
@ApiModelProperty("主键")
@Field
private Long id;
@ApiModelProperty("条形码")
@Field
private String barcode;
@ApiModelProperty("销售单价")
@Field
private Double unitPrice;
@ApiModelProperty("主标题")
@Field
private String mainTitle;
@ApiModelProperty("副标题")
@Field
private String subTitle;
@ApiModelProperty("描述")
@Field
private String description;
@ApiModelProperty("图片URL")
@Field
private String imgUrl;
@ApiModelProperty("分类名称")
@Field
private String categoryName;
}
@Field注解是关键,属性名应与与manage-schema文件中的<field name=“xx”>对应,如下:
<field name="id" type="string" indexed="true" stored="true"
required="true" multiValued="false" />
<field name="main_title" type="text_ik" indexed="true"
stored="true" />
<field name="sub_title" type="text_ik" indexed="true"
stored="true" />
<field name="barcode" type="string" indexed="true"
stored="true" />
<field name="unit_price" type="pdouble" indexed="true"
stored="true" />
<field name="category_name" type="string" indexed="true"
stored="true" />
<field name="imgUrl" type="string" indexed="true"
stored="true" />
<field name="description" type="string" indexed="true"
stored="true" />
然后是返回参数封装的类
import lombok.Data;
import java.util.List;
@Data
public class SolrQueryResult {
//商品列表
private List<SolrGoodsDto> productList;
//商品总数
private Long recordCount;
//总页数
private Integer pageCount;
//当前页
private Integer curPage;
}