【问题标题】:How to work on the filter in the elasticsearch template?如何在 elasticsearch 模板中使用过滤器?
【发布时间】:2020-05-06 13:37:04
【问题描述】:

我有类似的数据集可用于 Elasticsearch。

  {
    "id" :1,
    "title" : "Toy Story 1",
    "year" : 1995 ,
    "category" : "Adventure"
    },
    {
    "id" :2,
    "title" : "Jumanji",
    "year" : 1995,
    "category" : "Adventure"
    },
    {
    "id" :3,
    "title" : "Grumpier Old Men",
    "year" : 1996,
    "category" : "Comedy"
    },
    {
    "id" :4,
    "title" : "Toy Story 2",
    "year" : 1996,
    "category" : "Action"
    },
    {
    "id" :5,
    "title" : "Toy Story 3"
    "year" : 1997
    "category" : "Comedy"
    },
    {
    "id" :6,
    "title" : "Toy Story 4"
    "year" : 2019
    "category" : "Comedy"
    }

这是我的索引的映射。

{
    "movies": {
        "mappings": {
            "properties": {
                "category": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "year": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

我正在使用搜索模板来检索数据。 我已经创建了一个模板并通过传递模板的 id 来查询数据。

您可以参考以下链接。 Search-template

我正在使用邮递员来创建模板并使用模板查询数据。

我创建的模板如下:

POST _scripts/测试

{
    "script": {
        "lang": "mustache",
        "source": {
            "query": {
                "match": {
                    "title": "{{query_string}}"
                }
            }
        }
    }
}

POST _search/模板

{
    "id": "test", 
    "params": {
        "query_string": "toy"
    }
}

这工作正常。我得到了预期的结果。

现在我被模板中的过滤选项卡住了。

我想过滤类别。我想在模板中添加类别并将值传递为["Comedy", "Adventure"]

我们如何在模板中添加过滤器并将数组值传递给它?

【问题讨论】:

    标签: elasticsearch elasticsearch-template


    【解决方案1】:

    你可以这样做。首先像这样修改你的脚本模板:

    POST _scripts/test
    {
      "script": {
        "lang": "mustache",
        "source": """
          {
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "title": "{{query_string}}"
                    }
                  }
                ],
                "filter": [
                  {
                    "terms": {
                      "category.keyword": {{#toJson}}categories{{/toJson}}
                    }
                  }
                ]
              }
            }
          }
        """
      }
    }
    

    请注意,以上内容只能在 Kibana Dev Tools 中执行(因为有三引号 (""")。

    如果您在其他地方执行此操作,则需要先对查询进行 JSON 编码:

    POST _scripts/test
    {
      "script": {
        "lang": "mustache",
        "source": "{\n      \"query\": {\n        \"bool\": {\n          \"must\": [\n            {\n              \"match\": {\n                \"title\": \"{{query_string}}\"\n              }\n            }\n          ],\n          \"filter\": [\n            {\n              \"terms\": {\n                \"category.keyword\": {{#toJson}}categories{{\/toJson}}\n              }\n            }\n          ]\n        }\n      }\n    }"
      }
    }
    

    那么你可以这样称呼它:

    POST _search/template
    {
      "id": "test",
      "params": {
        "query_string": "toy",
        "categories": [
          "Comedy",
          "Adventure"
        ]
      }
    }
    

    它将产生这个查询:

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": "toy"
              }
            }
          ],
          "filter": [
            {
              "terms": {
                "category": [
                  "Comedy",
                  "Adventure"
                ]
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 感谢您的回复...我会试试这个并告诉您...
    • 在将模板创建为“原因”时出现错误:“意外字符('\”'(代码 34)):在 [来源:org .elasticsearch.transport.netty4.ByteBufStreamInput@3c919a91;行:4,列:18]"
    • 你的ES是哪个版本的?
    • 我使用的是 elasticsearch-7.2.1
    • 那就用和我展示的类似的服务,网上有很多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    相关资源
    最近更新 更多