我在我的应用程序中实现搜索功能,我在那里搜索数据库中的记录。我正在使用ElasticSearch,我知道我必须保持数据库和索引同步。我将在应用层上处理这个问题。
我的问题更多的是从我的J2EE 7应用程序查询ElasticSearch。我可以从查询中获取JsonObject或JsonArray,并且"手动解析"它使用Gson lib及其JsonObject和JsonArray类,但这很难产生。
根据我的阅读,使用GSON解析的最佳做法是使POJO具有与Json对象相同的结构。我可以这样做,但是这个POJO与我的JPA实体非常相似,所以我在两个地方都有相同的信息,这会使维护变得更难。我无法让它无缝地工作,但我想尝试另一种方法。
所以我的问题是,是否可以将json对象映射到JPA实体,更重要的是,这是一个很好的做法吗?
这是我的JSON(变量名称可以更改,我知道他们现在不匹配):
{
"vendorEcommerce": 1,
"Categories": {
"Sub": [
4,
8,
5
],
"Main": [
1,
2
]
},
"Countries": 1,
"vendorFeatured": 1,
"vendorWebsite": "www.johndoesserviceprovider.com",
"vendorEmail": "johndoe@gmail.com",
"vendorCreated": "2015-02-25T17:00:13.000Z",
"vendorName": "John Doe's Service Provider",
"vendorLocation": 1,
"vendorRating": 1,
"States": [
1,
2
],
"vendorUpdated": "2015-02-25T17:00:13.000Z",
"vendorPhone": "+11235551234",
"vendorDescription": "This is John Doe's Service Provider Description",
"vendorPromotion": "Oh yeah",
"vendorPrice": 3
}
这是我的JPA实体(不相关的信息):
public class VendorEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Integer vendorId;
private Date vendorCreated;
private Date vendorUpdated;
private String vendorName;
private String vendorWebsite;
private String vendorPhone;
private String vendorEmail;
private String vendorDescription;
private Boolean vendorEcommerce;
private Integer vendorRating;
private Integer vendorPrice;
private String vendorPromotion;
private Boolean vendorFeatured;
private LocationEntity location;
private List<ServiceCategoryEntity> categories;
private List<VendorCoverageEntity> coverages;
}
答案 0 :(得分:1)
您可以使用Spring Data Elasticsearch与JPA一起使用..
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchCrudRepository.class))
@EnableElasticsearchRepositories(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchCrudRepository.class))
public class DataConfiguration {
...
}
实体示例:
@Entity
@Table(name="learn_author")
@Document(indexName = "blogging", type = "author", shards = 1, replicas = 0)
public class Author implements Serializable {
@org.springframework.data.annotation.Id
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
@Column(name="id")
private Long id;
@Column(name="username")
private String username;
@Field(type = FieldType.String,
searchAnalyzer = "standard",
indexAnalyzer = "standard",
store = true)
@Column(name="fullname")
private String fullname;
...
}
完整示例