Elasticsearch搜索Java API没有给出正确的结果

时间:2018-05-14 05:17:10

标签: elasticsearch full-text-search

我有以下ES搜索代码:

    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("localhost", 9200, "http")));

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    qb = QueryBuilders.termQuery("title", "Java");
    searchSourceBuilder.query(qb);
    searchSourceBuilder.from(0);
    searchSourceBuilder.size(5);

    SearchRequest searchRequest = new SearchRequest("myindex");
    searchRequest.types("books");
    searchRequest.source(searchSourceBuilder);

    SearchResponse sr = client.search(searchRequest);
    System.out.println(sr.getHits().totalHits);

即使有符合此标准的文件,也没有给我任何结果。 当我从Kibana运行类似的术语查询时,我得到了结果:

GET /myindex/books/_search
{
   "query":
   {
     "term" : {
       "title" : {
       "value" : "java"
       }
      }
   }
}

1 个答案:

答案 0 :(得分:2)

您的Java代码中的差异为Java(大写),Kibana查询中的差异为java(小写)。

由于您正在进行term查询,因此案例很重要。如果您使用match查询,则案例无关紧要,两者都可以按预期工作。