我使用_id从弹性搜索中搜索记录,并且能够从弹性搜索中获取记录。但现在我想使用通配符基于_source(来自_source的任何字段)进行搜索。我不知道如何为此构建我的查询。有没有关于此的文件?
请在下面找到我的代码,我可以根据_id查询弹性搜索。
使用弹性搜索6.2.3版本。
DT_words$word_count <- vapply(paste0("\\b", DT_words$word, "\\b"), function(x) {
sum(str_count(DT_strings$string, x))
}, 0)
请查找弹性搜索中可用的记录。在这里,我想基于_source字段搜索此记录。例如:基于public Product getProductById(String id){
String[] includes = new String[]{id};
String[] excludes = Strings.EMPTY_ARRAY;
GetRequest getRequest = new GetRequest(INDEX, TYPE, SOURCE);
getRequest.routing(id);
GetResponse getResponse = null;
try {
getResponse = restHighLevelClient.get(getRequest);
} catch (java.io.IOException e){
e.getLocalizedMessage();
}
//GetResponse getResponse = null;
// create the search request
SearchRequest searchRequest = new SearchRequest(INDEX);
searchRequest.types(TYPE);
// create the match query on the author field
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("_id", id);
searchSourceBuilder.query(matchQueryBuilder);
searchRequest.source(searchSourceBuilder);
// send the request
SearchResponse searchResponse = null;
try {
searchResponse = restHighLevelClient.search(searchRequest);
} catch (IOException e) {
e.getLocalizedMessage();
}
// read the response
String productName = null;
Product product = null;
SearchHit[] searchHits = searchResponse.getHits().getHits();
for (SearchHit hit : searchHits) {
// get each hit as a Map
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
product=new Product();
product.setName(sourceAsMap.get("name").toString());
}
Gson gson=new Gson();
JSONObject productJSON = null;
String prodStr=gson.toJson(product);
try {
productJSON=new JSONObject(prodStr);
} catch (JSONException e) {
e.printStackTrace();
}
return product;
}
code
答案 0 :(得分:1)
只需更换此行
即可MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("_id", id);
这一个
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("code", "abc1234");