如何使用RestHighLevelClient查找具有特定字段的文档包含字符串

时间:2018-11-18 05:10:30

标签: java amazon-web-services elasticsearch

因此,我可以通过url简化操作

<elastic_host>/indexname/_search?pretty=true&q=text:*str*

我想对RestHighLevelClient做同样的事情,但是不起作用:

 SearchRequest searchRequest = new SearchRequest(indexName);
    searchRequest.types(INDEX_TYPE);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    QueryBuilder queryBuilder = QueryBuilders.matchQuery(IMAGE_TEXT_FIELD_NAME, "*str*" );
    searchSourceBuilder.query(queryBuilder);
    searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

1 个答案:

答案 0 :(得分:0)

等效查询不是匹配查询,而是query_string查询,因此您可以替换以下行

QueryBuilder queryBuilder = QueryBuilders.matchQuery(IMAGE_TEXT_FIELD_NAME, "*str*" );

这个人

QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("*str*").field(IMAGE_TEXT_FIELD_NAME);