使用JEST将弹性搜索结果映射到JPA实体

时间:2021-03-21 00:11:29

I'm implementing a search function in my application, where I'm searching for records that are in my database. I'm using ElasticSearch for this and I know that I will have to keep the database and the indices in sync. I'll take care of that on the application layer.

我正在我的应用程序中实现一个搜索功能,我正在搜索数据库中的记录。我用的是弹搜索,我知道我必须保持数据库和索引的同步。我会在应用层上处理。

My question is more about querying ElasticSearch from my J2EE 7 application. I can get the JsonObject or JsonArray from a query and "manually parse" it using the Gson lib and its JsonObject and JsonArray classes, but that's hardly productive.

我的问题是关于从J2EE 7应用程序查询弹性搜索。我可以从查询中获取JsonObject或JsonArray,并使用Gson lib、它的JsonObject和JsonArray类“手动解析”它,但这很难提高效率。

From what I've read, the best practice in parsing with GSON is to have a POJO that has the same structure as you Json objects. I could do that, but this POJO would be very similar to my JPA Entities, so I'd have the same information in two places, which would make maintenance harder. I haven't been able to get this to work seamlessly, but I'd like to try another approach.

根据我所读到的,使用GSON解析的最佳实践是拥有与Json对象具有相同结构的POJO。我可以这样做,但是这个POJO与我的JPA实体非常相似,所以我在两个地方有相同的信息,这将使维护更加困难。我还没能让它无缝地工作,但我想尝试另一种方法。

So my question is, is it possible to map a json object to a JPA entity and more importantly, is this a good practice?

因此,我的问题是,是否可能将json对象映射到JPA实体,更重要的是,这是一个良好的实践吗?

Here's my JSON (variable names can be changed, I know they don't match right now):

这是我的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
        }

And here's my JPA Entity (irrelevant information ommitted):

这是我的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;
}

1 个解决方案

#1


1  

You can use Spring Data Elasticsearch it work with JPA together..

您可以使用Spring Data elasticity search它与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 Example:

实体的例子:

@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;

    ...

}

click here for full example

点击这里查看完整示例

#1


1  

You can use Spring Data Elasticsearch it work with JPA together..

您可以使用Spring Data elasticity search它与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 Example:

实体的例子:

@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;

    ...

}

click here for full example

点击这里查看完整示例