因此,在ES中编写(右)查询并使用sense插件对本地ES安装进行测试后,我现在面临的问题是:如何使用ES JAVA API从我的代码中执行相同操作。以下是我要翻译的查询:
{
"size": 5,
"query": {
"multi_match": {
"query": "physics",
"type": "most_fields",
"fields": [
"document.title^10",
"document.title.shingles^2",
"document.title.ngrams",
"person.name^10",
"person.name.shingles^2",
"person.name.ngrams",
"document.topics.name^10",
"document.topics.name.shingles^2",
"document.topics.name.ngrams"
],
"operator": "and"
}
}
}'
我知道它应该是这样的,但我不太确定:
Node node = nodeBuilder().client(true).node();
Client client = node.client();
SearchResponse response = client.prepareSearch("dlsnew")
.setTypes("person", "document")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.multiMatchQuery("physics",
"document.title^10",
"document.title.shingles^2",
"document.title.ngrams",
"person.name^10",
"person.name.shingles^2",
"person.name.ngrams",
"document.topics.name^10",
"document.topics.name.shingles^2",
"document.topics.name.ngrams"))
.setFrom(0).setSize(5).setExplain(true)
.execute()
.actionGet();
SearchHit[] results = response.getHits().getHits();
另外,如何处理查询中的“operator”和“type”:“most_fields”部分?
答案 0 :(得分:1)
你几乎做到了
QueryBuilders.multiMatchQuery("physics",
"document.title^10",
"document.title.shingles^2",
"document.title.ngrams",
"person.name^10",
"person.name.shingles^2",
"person.name.ngrams",
"document.topics.name^10",
"document.topics.name.shingles^2",
"document.topics.name.ngrams")
.operator(MatchQueryBuilder.Operator.AND)
.type(MultiMatchQueryBuilder.Type.MOST_FIELDS);