简体   繁体   English

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

[英]How to translate an ElasticSearch multimatch search query from cURL into JAVA?

So, having made up the (right) query in ES and tested it against a local ES installation using the sense plugin, I am now facing the problem: How to do the same from my code using the ES JAVA API.因此,在 ES 中编写了(正确的)查询并使用 sense 插件针对本地 ES 安装对其进行了测试后,我现在面临的问题是:如何使用 ES JAVA API 从我的代码中执行相同操作。 Here is the query I am trying to translate:这是我试图翻译的查询:

{
"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"
    }
  }
}'

I know it should be something like this, but I am not quite sure:我知道它应该是这样的,但我不太确定:

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

Also, how to handle the "operator" and "type":"most_fields" parts from the query?另外,如何处理查询中的“operator”和“type”:“most_fields”部分?

You almost did it你几乎做到了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM