我使用了Elastic Search java客户端, 我正在实施一个搜索应用程序。 (索引已经建成)
我想重现Elastic Search java客户端,以下查询。
然而,它无法复制。
如果您熟悉java客户端, 我要你告诉我。
{
"Query": {
"Bool": {
"Must": [{
"Match": {
"Sample": "java"
}},
{"Match": {
"Sample": "php"
}
}]
}
},
"Size": 50
}
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery ().
Must (QueryBuilders.matchQuery ("sample", "test")));
SearchRequestBuilder builder = client.prepareSearch ()
.setSize (50)
.setQuery (boolQuery);
System.out.println (builder);
{
"Size": 50,
"Query": {
"Bool": {
"Must": [
{
"Match": {
"Sample": {
"Query": "test",
"Operator": "OR",
"Prefix_length": 0,
"Max_expansions": 50,
"Fuzzy_transpositions": true,
"Lenient": false,
"Zero_terms_query": "NONE",
"Boost": 1.0
}
}
}
],
"Disable_coord": false,
"Adjust_pure_negative": true,
"Boost": 1.0
}
},
"Ext": {}
}
输出时,匹配中包含额外的属性。 而且,我仍然不知道如何匹配多个单词。
有人,请借给我你的智慧。
答案 0 :(得分:0)
假设我有以下映射:
{
"articles": {
"mappings": {
"article": {
"properties": {
"sample": {
"type": "text"
}
}
}
}
}
}
我索引了三个文件:
{ "sample" : "java php" },
{ "sample" : "java" },
{ "sample" : "php" }
您提供的es-query为我提供了以下结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51623213,
"hits": [
{
"_index": "articles",
"_type": "article",
"_id": "AV0rSbyjpRVEWsG0iTxj",
"_score": 0.51623213,
"_source": {
"sample": "java php"
}
}
]
}
}
要使用Java API获得相同的结果,我使用:
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery ("sample", "java"))
.must(QueryBuilders.matchQuery ("sample", "php"));
SearchRequestBuilder builder = client.prepareSearch()
.setSize(50)
.setQuery(boolQuery);
SearchResponse response;
try {
response = builder.execute().get();
for (SearchHit hit : response.getHits().getHits()) {
LOG.info("Result: " + hit.getSourceAsString());
}
} catch (InterruptedException | ExecutionException e) {
// handle exception
LOG.error("Exception while executing query {}", e);
}
结果:
Result: { "sample" : "java php" }
调试时在查询中看到的所有其他属性只是默认值,Java API会明确设置它们。