Elasticsearch BoolQuery 获取不同的文件

问题描述 投票:0回答:1

嗨,我正在尝试进行搜索,我可以使用弹性搜索找到特定文件。这是我的java客户端:

<dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.5.3</version>
</dependency>

我准备了一个查询如下:

private static Query getQueryByStatus() {
        BoolQuery boolQuery = BoolQuery.of(b -> b
            .should(qb -> qb.match(m -> m.field("fileType").query("WAIT_EXP")))
            .should(qb -> qb.match(m -> m.field("fileType").query("EXP")))
        );

        return boolQuery._toQuery();
    }
Query testQuery = getQueryByStatus();

                        response = client.search(s -> s
                                .index(fileIndex)
                                .from(filterDto.getPage() * filterDto.getSize())
                                .size(filterDto.getSize())
                                .query(q -> q
                                    .bool(b -> b
                                        .must(matchCompany)
                                        .must(matchByWords)
                                        .should(testQuery) 
                                    )
                                ),
                            SearchData.class
                        );

我的期望是它会找到文件类型为 exp 或 wait_exp 的文件,但 elastic 会带来它们以及不相关的文件。不幸的是,我在我使用的客户文档中看不到任何关于该主题的明确解释。

java spring-boot elasticsearch elastic-stack
1个回答
0
投票

根据上面的代码,我可以看到即使您想根据文件名进行搜索,您也必须进行匹配。

如果您想引入以“WAIT_EXP*”和“EXP*”开头的文件,您可以使用 match_phrase_prefix 类型的查询

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html

© www.soinside.com 2019 - 2024. All rights reserved.