如何让ShingleFilterFactory与Hibernate Search一起使用?

时间:2012-05-08 11:40:18

标签: hibernate search solr lucene hibernate-search

@AnalyzerDef(
        name = "tags",
        tokenizer = 
            @TokenizerDef(factory = PatternTokenizerFactory.class, 
                params = {
                    @Parameter(name = "pattern", value=",")
                }
            ),
        filters = {
            @TokenFilterDef(factory = StandardFilterFactory.class),
            @TokenFilterDef(factory = LowerCaseFilterFactory.class),
            @TokenFilterDef(
                factory = StopFilterFactory.class,
                params = {
                    @Parameter(name = "words", value = "data/ignorewords.txt"),
                    @Parameter(name = "ignoreCase", value = "true")
                }
            ),
            @TokenFilterDef(
                factory = SynonymFilterFactory.class,
                params = {
                    @Parameter(name = "ignoreCase", value="true"),
                    @Parameter(name = "expand", value="false"),
                    @Parameter(name = "synonyms", value="data/synonyms.txt")
                }
            ),
            @TokenFilterDef(
                factory = SnowballPorterFilterFactory.class,
                params = {
                    @Parameter(name = "language", value="English")
                }
            ),
            @TokenFilterDef(
                factory = ShingleFilterFactory.class,
                params = { 
                    @Parameter(name = "minShingleSize", value="2"),
                    @Parameter(name = "maxShingleSize", value="3"),
                    @Parameter(name = "outputUnigrams", value="true"),
                    @Parameter(name = "outputUnigramsIfNoShingles", value="false")
                }
            ),
            @TokenFilterDef(
                factory = PositionFilterFactory.class,
                params = {
                    @Parameter(name = "positionIncrement", value = "100")
                }
            ),
            @TokenFilterDef(
                factory = PhoneticFilterFactory.class,
                params = { 
                    @Parameter(name = "encoder", value="RefinedSoundex"),
                    @Parameter(name = "inject", value="true")
                }
            )
        }
),
@AnalyzerDef(
    name = "querytime",
    tokenizer = 
        @TokenizerDef(factory = StandardTokenizerFactory.class),
    filters = {
        @TokenFilterDef(factory = StandardFilterFactory.class),
        @TokenFilterDef(factory = LowerCaseFilterFactory.class),
        @TokenFilterDef(factory = StopFilterFactory.class,
            params = {
                @Parameter(name = "words", value = "data/ignorewords.txt"),
                @Parameter(name = "ignoreCase", value = "true")
            }
        ),
        @TokenFilterDef(
            factory = SnowballPorterFilterFactory.class,
            params = { 
                @Parameter(name = "language", value="English")
            }
        ),
        @TokenFilterDef(
            factory = ShingleFilterFactory.class,
            params = { 
                @Parameter(name = "minShingleSize", value="2"),
                @Parameter(name = "maxShingleSize", value="3"),
                @Parameter(name = "outputUnigrams", value="true"),
                @Parameter(name = "outputUnigramsIfNoShingles", value="false")
            }
        ),
        @TokenFilterDef(
            factory = PositionFilterFactory.class,
            params = {
                @Parameter(name = "positionIncrement", value = "100")
            }
        ),
        @TokenFilterDef(
            factory = PhoneticFilterFactory.class,
            params = { 
                @Parameter(name = "encoder", value="RefinedSoundex"),
                @Parameter(name = "inject", value="true")
            }
        )
    }
)
})

上面是我的分析器定义,我在索引时使用 PatternTokenizerFactory 来索引包含多个单词的短语。 还有一个 StandardTokenizerFactory ShingleFilterFactory 将在查询时使用,但是我无法从搜索查询中收到令牌组合。 我的期望是:当搜索查询我的搜索查询时,它应该是我的搜索搜索查询,而是我得到了我的搜索查询

以下是我的功能

FullTextSession fullTextSession = Search.getFullTextSession(session);
            Transaction tx = fullTextSession.beginTransaction();
            // create native Lucene query
            org.apache.lucene.search.Query luceneQuery = null;
            String[] fields = new String[] {"tags"}; 
            MultiFieldQueryParser parser = new MultiFieldQueryParser(
                    Version.LUCENE_31, fields, fullTextSession.getSearchFactory().getAnalyzer("querytime"));
            try {
                luceneQuery = parser.parse(searchQuery);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            // wrap Lucene query in a org.hibernate.Query
            org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery, CityArea.class);

            // execute search
            List result = hibQuery.list();

            tx.commit();
            session.close();
            return result;

问题是我的索引令牌是ex:说 Word A Word B ,我正在搜索哪里是word a (这样它会显示 Word A 的记录但没有结果。这是因为我不希望在搜索查询包含索引的短语之前显示任何结果。

2 个答案:

答案 0 :(得分:0)

任何人都没有回答我自己深入研究问题,并自己找到答案。因为它可以帮助别人,我在这里写它并且解决方案非常简单,只需要将searchQuery包装成引号。

答案 1 :(得分:0)

我使用了:

@TokenizerDef(factory = StandardTokenizerFactory.class),

在您的代码分析器中,您的代码工作正常。

现在我想知道你为什么用一个不同的标记器进行查询而另一个用于商店?