使用RestHighLevelClient搜索空格分隔的字符串时没有结果(JAVA Spring-boot)

时间:2019-04-03 15:02:33

标签: java spring rest spring-boot elasticsearch

我正在使用RestHighLevelClient来使用弹性API。

RestHighLevelClient版本:6.4.3 弹性版本:6.7

使用RestHighLevelClient传递单个字符串时,我得到正确的结果,但是传递空格分隔的字符串时,没有结果。 但是,在Postman上也可以正常工作。

我的JAVA代码:

search(broker, "merchant_stores", QueryBuilders.fuzzyQuery("search_string", searchString).fuzziness(Fuzziness.TWO), DeviceMerchantStore.class)
protected Flux<T> search(final String index, final String type, final QueryBuilder queryBuilder, final Class<T> clazz) throws IOException {
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(queryBuilder);
        searchRequest.source(searchSourceBuilder);
        final SearchResponse searchResponse = elasticClient.search(searchRequest, RequestOptions.DEFAULT);
        final List<T> l = new ArrayList<>();
        for(SearchHit searchHit: searchResponse.getHits().getHits()) {
            l.add(new ObjectMapper().readValue(searchHit.getSourceAsString(), clazz));
        } return Flux.fromIterable(l);
    }

基于JAVA构建的查询:

{
  "fuzzy" : {
    "search_string" : {
      "value" : "croma Maharashtra",
      "fuzziness" : "2",
      "prefix_length" : 0,
      "max_expansions" : 50,
      "transpositions" : false,
      "boost" : 1.0
    }
  }
}

查询在邮递员上触发

{
  "query": {
    "match": {
      "search_string": {
        "query": "croma Maharashtra",
        "fuzziness": 2,
        "max_expansions" : 50,
        "prefix_length" : 0,
        "boost":1.0
      }
    }
  }
}

让我知道我在这里想念的东西。

2 个答案:

答案 0 :(得分:0)

由于“换位”,您没有相同的结果:false。确实,有了匹配项(假设您有std分析器),就好像换位是正确的(并且您不能将他设置为false)。因此,在匹配查询结果中,将返回ab = ba,就像在模糊查询中一样,它们不会返回。

答案 1 :(得分:0)

查询不同:

更改:

QueryBuilders.fuzzyQuery("search_string", searchString).fuzziness(Fuzziness.TWO)

收件人:

QueryBuilders.fuzzyQuery("search_string", searchString).fuzziness(Fuzziness.TWO).transpositions(false)