0

我正在尝试in在弹性搜索中复制 SQL 查询......类似于select * from products where id in ('123, '345');

我有类似下面的东西 -

Set<String> searchIds = new HashSet<String>();
searchIds.add("123");
searchIds.add("456");

response = client.prepareSearch("id_index")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.termsQuery("product_id", searchIds))
                .setFrom(0).setSize(50).setExplain(false)
                .execute().actionGet();
hits = response.getHits();
hits.forEach((h) -> {
    h.getSource().entrySet().stream().forEach((e)->{
        System.out.println(e.getKey()+" : "+String.valueOf(e.getValue()).replace("\n", "").replace("\t", ""));
    });
});
System.out.println("Response : "+response.toString());

我从最后得到的回应System.out.println如下,

Response : {
  "took" : 14,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

我确定有两种产品带有产品 ID 123345因为我可以通过 Kibana 看到它们。但是上面的代码没有返回任何命中。我是否IN以正确的方式复制 SQL 查询?

4

0 回答 0