这就像一个魅力:

        Client client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("field", "word_1");
        fuzzyQueryBuilder.fuzziness(Fuzziness.AUTO);

        SearchResponse response = client.prepareSearch("ts_index")
                                        .setTypes("service")
                                        .setQuery(fuzzyQueryBuilder)
                                        .setFrom(0).setSize(60).setExplain(true)
                                        .execute()
                                        .actionGet();
        SearchHit[] results = response.getHits().getHits();

但是,如果我想按多个单词进行搜索,则不会返回任何内容,例如:
FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("field", "word_1 word_2");

当我使用 CURL 时,我已通过将operator添加到我的 JSON 属性中来解决此问题:



如何用Java实现呢?

最佳答案

我相信这可以通过AND的must和OR的should来实现:

 QueryBuilder qb = QueryBuilders.boolQuery()
                .must(QueryBuilders.fuzzyQuery("field", "word_1"))
                .must(QueryBuilders.fuzzyQuery("field", "word_2"));

10-05 23:29