休眠搜索 | Lucene Kuromojo Analyzer 依赖方法名

Hibernate Search | Lucene Kuromojo Analyzer depend on method name

我的实体 class FeatureMeta 使用英语和日语两个分析器进行了注释。

在我的存储库 class 中,我将搜索 FeatureMeta 实体的方法命名为 "findFeatures"。

但是当我尝试访问存储库中的 "findFeatures" 方法时抛出以下异常:

Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.lucene.analysis.ja.JapaneseTokenizer.Mode.FINDFEATURES

请注意,在幕后,"JapaneseTokenizerFactory" 尝试使用我在存储库 (findFeatures) 中使用的方法名称的值创建一个枚举。

我不确定为什么 JapaneseTokenizerFactory 的逻辑与我的存储库名称之间存在关系。

实体class:

@Entity
@Table(name="feature_meta")
@Indexed
@AnalyzerDef(name = "japaneseAnalyzer",
        tokenizer = @TokenizerDef(factory = JapaneseTokenizerFactory.class,
                params = {@Parameter(name = "mode", value = "findFeatures"),
                        }),
        filters = {
                @TokenFilterDef(factory = JapaneseBaseFormFilterFactory.class),
                @TokenFilterDef(factory = JapanesePartOfSpeechStopFilterFactory.class,
                        params = @Parameter(name = "tags", value = "org/apache/lucene/analysis/ja/stoptags.txt")),
                @TokenFilterDef(factory = CJKWidthFilterFactory.class),
                @TokenFilterDef(factory = StopFilterFactory.class,
                        params = @Parameter(name = "words", value = "org/apache/lucene/analysis/ja/stopwords.txt")),
                @TokenFilterDef(factory = JapaneseKatakanaStemFilterFactory.class),
                @TokenFilterDef(factory = LowerCaseFilterFactory.class)
        })
@AnalyzerDef(name = "englishAnalyzer",
        tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
        filters = {
                @TokenFilterDef(factory = LowerCaseFilterFactory.class),
                @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
                        @Parameter(name = "language", value = "English")
                })
        })

public class FeatureMeta implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name="feature_id",unique = true)
    private String featureId;

    @Column(name="category_id")
    @Field(name = "categoryId", analyze=Analyze.NO)
    private String categoryId;

    @Field(name="featureSetId",analyze=Analyze.NO)
    @Column(name = "feature_set_id")
    private String featureSetId;

    @Enumerated(EnumType.STRING)
    @Column(name="feature_type")
    private EnumFeatureType featureType;

    @Fields({@Field(name="name_en", analyzer=@Analyzer(definition="englishAnalyzer"))})
    @Column(name="name_en")
    @Lob
    private String nameEn;

    @Fields({@Field(name="name_ja", analyzer=@Analyzer(definition="japaneseAnalyzer"))})
    @Column(name="name_ja")
    @Lob
    private String nameJa;

    @Fields({@Field(name="desc_en", analyzer=@Analyzer(definition="englishAnalyzer"))})
    @Column(name="desc_en")
    @Lob
    private String descEn;

    ..


}

存储库:

@Repository
public class FeatureSegmentMetaSearchRepository {

    @PersistenceContext
    private EntityManager entityManager;

    public FeatureMetaSearchResult findFeatures(Integer offset, Integer pageSize, String text, String categoryId, String featureSetId, Collection<String> tags) {

        FeatureMetaSearchResult ret = new FeatureMetaSearchResult();
        FullTextEntityManager fullTextEntityManager =
                org.hibernate.search.jpa.Search.
                        getFullTextEntityManager(entityManager);
        //Filtering by keyword
        QueryBuilder queryBuilder =
                fullTextEntityManager.getSearchFactory()
                        .buildQueryBuilder().forEntity(FeatureMeta.class).get();
        org.apache.lucene.search.Query queryKeyword = null;
        if (StringUtils.isEmpty(text)) {
            queryKeyword = queryBuilder.all().createQuery();
        } else {
            queryKeyword = queryBuilder
                    .keyword()
                    .onFields("name_en","name_ja", "desc_en", "desc_ja","business_desc_en","business_desc_ja",
                            "logical_desc_en", "logical_desc_ja")
                    .matching(text)

                    .createQuery();
        }
        BooleanQuery.Builder aggrBuilder = new BooleanQuery.Builder();
        aggrBuilder = aggrBuilder.add(new BooleanClause(queryKeyword, BooleanClause.Occur.MUST));

        if (categoryId != null) {
            TermQuery queryFilter = new TermQuery(new Term("categoryId",categoryId));
            aggrBuilder = aggrBuilder.add(new BooleanClause(queryFilter, BooleanClause.Occur.MUST));
        }
        if (featureSetId != null) {
            String setFilter = new EnumBridge().objectToString(featureSetId);
            TermQuery queryFilter = new TermQuery(new Term("featureSetId", setFilter));
            aggrBuilder = aggrBuilder.add(new BooleanClause(queryFilter, BooleanClause.Occur.MUST));
        }

        if(!CollectionUtils.isEmpty(tags)) {
            for (String tag : tags) {
                TermQuery queryFilter = new TermQuery(new Term("tags", tag));
                aggrBuilder = aggrBuilder.add(new BooleanClause(queryFilter, BooleanClause.Occur.MUST));
            }
        }

        org.hibernate.search.jpa.FullTextQuery jpaQuery =
                fullTextEntityManager.createFullTextQuery(aggrBuilder.build(), FeatureMeta.class);

        if (offset != null) jpaQuery.setFirstResult(offset);
        if (pageSize != null) jpaQuery.setMaxResults(pageSize);

        ret.setTotalCount(jpaQuery.getResultSize());
        ret.setResults(jpaQuery.getResultList());
        return ret;
    }
}

Caused by: org.hibernate.search.exception.SearchException: Unable to instantiate Tokenizer factory class: org.apache.lucene.analysis.ja.JapaneseTokenizerFactory. The implementation class did not recognize the applied parameters. at org.hibernate.search.util.impl.ClassLoaderHelper.instanceFromClass(ClassLoaderHelper.java:156) at org.hibernate.search.analyzer.impl.LuceneAnalyzerBuilder.buildAnalysisComponent(LuceneAnalyzerBuilder.java:147) at org.hibernate.search.analyzer.impl.LuceneAnalyzerBuilder.buildAnalyzer(LuceneAnalyzerBuilder.java:105) at org.hibernate.search.analyzer.impl.LuceneAnalyzerBuilder.buildAnalyzer(LuceneAnalyzerBuilder.java:76) at org.hibernate.search.analyzer.impl.NamedLuceneAnalyzerReference.createAnalyzer(NamedLuceneAnalyzerReference.java:64) at org.hibernate.search.analyzer.impl.NamedLuceneAnalyzerReference.initialize(NamedLuceneAnalyzerReference.java:60) at org.hibernate.search.analyzer.impl.LuceneEmbeddedAnalyzerStrategy.initializeReference(LuceneEmbeddedAnalyzerStrategy.java:234) at org.hibernate.search.analyzer.impl.LuceneEmbeddedAnalyzerStrategy.lambda$initializeReferences[=13=](LuceneEmbeddedAnalyzerStrategy.java:192) at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742) at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) at org.hibernate.search.analyzer.impl.LuceneEmbeddedAnalyzerStrategy.initializeReferences(LuceneEmbeddedAnalyzerStrategy.java:192) at org.hibernate.search.engine.impl.SearchIntegrationConfigContext.initialize(SearchIntegrationConfigContext.java:72) at org.hibernate.search.engine.impl.ConfigContext.initIntegrations(ConfigContext.java:252) at org.hibernate.search.spi.SearchIntegratorBuilder.initDocumentBuilders(SearchIntegratorBuilder.java:469) at org.hibernate.search.spi.SearchIntegratorBuilder.createNewFactoryState(SearchIntegratorBuilder.java:243) at org.hibernate.search.spi.SearchIntegratorBuilder.buildNewSearchFactory(SearchIntegratorBuilder.java:199) at org.hibernate.search.spi.SearchIntegratorBuilder.buildSearchIntegrator(SearchIntegratorBuilder.java:127) at org.hibernate.search.hcore.impl.HibernateSearchSessionFactoryObserver.boot(HibernateSearchSessionFactoryObserver.java:113) at org.hibernate.search.hcore.impl.HibernateSearchSessionFactoryObserver.sessionFactoryCreated(HibernateSearchSessionFactoryObserver.java:79) at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:375) at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889) ... 46 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.hibernate.search.util.impl.ClassLoaderHelper.instanceFromClass(ClassLoaderHelper.java:152) ... 69 more Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.lucene.analysis.ja.JapaneseTokenizer.Mode.FINDFEATURES at java.lang.Enum.valueOf(Enum.java:238) at org.apache.lucene.analysis.ja.JapaneseTokenizer$Mode.valueOf(JapaneseTokenizer.java:78) at org.apache.lucene.analysis.ja.JapaneseTokenizerFactory.(JapaneseTokenizerFactory.java:73) ... 74 more

问题不在于方法名称,而在于您的分析器定义。

你有这个:

tokenizer = @TokenizerDef(factory = JapaneseTokenizerFactory.class,
                params = {@Parameter(name = "mode", value = "findFeatures"),
                        }),

即您正在使用 "findFeatures" 作为模式。您需要使用支持的模式。