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

时间:2016-05-28 12:01:05

标签: java curl elasticsearch

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

1 个答案:

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