【问题标题】:How to create or update the search template of elasticsearch through Java code如何通过Java代码创建或更新elasticsearch的搜索模板
【发布时间】:2020-06-15 12:58:28
【问题描述】:

我正在使用我通过 Postman 使用 POST /_search/template/search 创建的以下弹性搜索查询

{
    "template": {
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "paymentAccounts",
            "query": {
              "bool": {
                "should": [
                  {
                    "nested": {
                      "path": "accounts",
                      "query": {
                        "bool": {
                          "should": [
                           {
                             "wildcard": {
                              "accounts.person.email.keyword": "*{{smartsearchquerystring}}*"
                              }
                            },
                            {
                              "wildcard": {
                                "accounts.description.keyword": "*{{smartsearchquerystring}}*"
                              }
                            },
                            {
                              "wildcard": {
                                "accounts.tradeName.keyword": "*{{smartsearchquerystring}}*"
                              }
                            },
                            {
                              "wildcard": {
                                "accounts.referenceId.keyword": "*{{smartsearchquerystring}}*"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
"from" : "{{offset}}",
"size" : "{{limit}}"
}
}

现在,每次如果我想更新搜索模板,我都会通过 Postman 访问相同的端点。 我想在我的服务中创建一个方法,例如每当我想更新任何模板只为该方法提供参数并创建或更新模板时。

如何使用 Java 做到这一点?或有关此的任何建议。

【问题讨论】:

    标签: java rest elasticsearch elasticsearch-template


    【解决方案1】:

    您必须将您的模板保存为 JSON 格式并向 ES 发送 PUT 请求, 并且您的端点应该如下所示,而不是您用来搜索的端点: /_template/{{模板名称或别名}}

    您可以使用 OkHttpClient 发送您的请求,详情请查看以下内容: OKhttp PUT example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多