gpt4 book ai didi

java - 如何将 ElasticSearch 多重匹配搜索查询从 cURL 转换为 JAVA?

转载 作者:行者123 更新时间:2023-11-30 07:21:26 25 4
gpt4 key购买 nike

因此,在 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”部分?

最佳答案

你差一点就做到了

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);

关于java - 如何将 ElasticSearch 多重匹配搜索查询从 cURL 转换为 JAVA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37498726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com