2

org.apache.lucene.queryparser.classic.QueryParser在 lucene 6.0.0 中使用 a 来解析查询CustomAnalyzer,如下所示:

public static void testFilmAnalyzer() throws IOException, ParseException {
    CustomAnalyzer nameAnalyzer = CustomAnalyzer.builder()
            .addCharFilter("patternreplace",
                    "pattern", "(movie|film|picture).*",
                    "replacement", "")
            .withTokenizer("standard")
            .build();

    QueryParser qp = new QueryParser("name", nameAnalyzer);
    qp.setDefaultOperator(QueryParser.Operator.AND);
    String[] strs = {"avatar film fiction", "avatar-film fiction", "avatar-film-fiction"};

    for (String str : strs) {
        System.out.println("Analyzing \"" + str + "\":");
        showTokens(str, nameAnalyzer);
        Query q = qp.parse(str);
        System.out.println("Parsed query of \"" + str + "\":");
        System.out.println(q + "\n");
    }
}

private static void showTokens(String text, Analyzer analyzer) throws IOException {
    StringReader reader = new StringReader(text);
    TokenStream stream = analyzer.tokenStream("name", reader);
    CharTermAttribute term = stream.addAttribute(CharTermAttribute.class);
    stream.reset();
    while (stream.incrementToken()) {
        System.out.print("[" + term.toString() + "]");
    }
    stream.close();
    System.out.println();
}

当我调用时,我得到以下输出testFilmAnalyzer

Analyzing "avatar film fiction":
[avatar]
Parsed query of "avatar film fiction":
+name:avatar +name:fiction

Analyzing "avatar-film fiction":
[avatar]
Parsed query of "avatar-film fiction":
+name:avatar +name:fiction

Analyzing "avatar-film-fiction":
[avatar]
Parsed query of "avatar-film-fiction":
name:avatar

分析器似乎PatternReplaceCharFilter以正确的预期顺序使用 (即在标记化之前),而在QueryParser之后使用。有人对此有解释吗?那不是bug吗?

4

1 回答 1

1

不,这不是错误。CharFilters总是在标记化之前应用,无论是在查询时间还是索引时间。

但是,空格在 QueryParser 语法中是有意义的,它完全独立于分析。将查询的单独子句分隔开来,并且每个子句都单独进行分析。如果您不依赖默认字段,这更容易看出,在这种情况下,我们需要将查询:avatar-film fiction,重写为:name:avatar-film name:fiction。“avatar-film”和“fiction”这两个子句中的每一个都被单独分析,导致您看到的结果。

尝试使用短语查询:

String[] strs = {"\"avatar film fiction\"", "\"avatar-film fiction\"", "\"avatar-film-fiction\""};

你应该看到你期望的结果。

于 2016-04-28T15:33:56.197 回答