1

我有在 Infinispan 中测试搜索引擎的简单代码。

public class InifinispanTest {

    private static class DemoA {
        private Long id;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }
    }

    private static class DemoB extends DemoA {
        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    public static void main(String[] args) throws IOException {
        SearchMapping mapping = new SearchMapping();
        mapping.entity(DemoB.class).indexed().providedId()
               .property("id", ElementType.METHOD).field();

        Properties properties = new Properties();
        properties.put(org.hibernate.search.Environment.MODEL_MAPPING, mapping);
        properties.put("hibernate.search.default.directory_provider", "ram");
        properties.put("hibernate.search.default.indexmanager", "near-real-time");

        Configuration infinispanConfiguration = new ConfigurationBuilder()
                .indexing()
                .enable()
                .indexLocalOnly(true)
                .withProperties(properties)
                .loaders().passivation(true).addFileCacheStore()
                .build();

        DefaultCacheManager cacheManager = new DefaultCacheManager(infinispanConfiguration);

        final Cache<Integer, DemoB> cache = cacheManager.getCache();

        for (int i = 0; i < 10000; i++) {
            final DemoB demo = new DemoB();
            demo.setId((long) i);

            cache.put(i, demo);
        }

        final SearchManager searchManager = Search.getSearchManager(cache);
        final QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(DemoB.class).get();

        final Query query = queryBuilder.keyword().onField("id").matching(1000l).createQuery();

        final CacheQuery query1 = searchManager.getQuery(query, DemoB.class);

        for (Object  result : query1.list()) {
            System.out.println(result);
        }

    }
}

如您所见,有一个基类 DemoA 及其子类 DemoB。我想通过超类归档 id 进行搜索。然而这个演示代码生成org.hibernate.search.SearchException: Unable to find field id in com.genesis.inifispan.InifinispanTest$DemoB

我假设我错过了搜索映射配置中的继承配置,但是查看文档我一无所获。我想要基于 Java 的配置,因为我无法在生产环境中更改实体类。

请您帮我配置或指导我以正确的方式阅读文档。

4

1 回答 1

1

使用 SearchMapping 时,您应该像使用注释一样指定字段:id仅对DemoA有效,因此正确的映射如下所示:

SearchMapping mapping = new SearchMapping()
   .entity(DemoA.class)
      .property("id", ElementType.METHOD).field()
   .entity(DemoB.class).indexed()
   ;

另请注意,我删除了 providedId():在最近的 Infinispan 版本中不再需要。

我认为 SearchMapping 至少应该警告您,甚至可能立即抛出异常:打开 JIRA HSEARCH-1328

于 2013-05-16T21:58:46.620 回答