如何在弹性搜索查询的过滤上下文中获取无痛脚本中的文本体字段?

我构建了一个带过滤器的弹性搜索查询,在过滤器上下文中,我正在编写一个无痛脚本,根据文本字段的正文来过滤一些文档。然而,当我想访问文本字段时,我得到的是一个术语列表,而不是原始文本。我正在寻找一种方法来访问无痛脚本中的原始文本正文,而不是术语列表。另外,如果无法访问文本正文,我希望在这种情况下访问文档的术语频率向量。

例如,如果我运行这个查询。

GET twitter/_search
{
  "query": {
      "bool": { 
      "must":{
        "term" : { "body" : "spark" }
      },
      "filter": [
        {
        "script" : {
                    "script" : {
                        "lang": "painless",
                        "source": """
                          String text = doc['body'].toString();
                          Debug.explain(text);
                         return true;
                      """

                    }
                }
      }
      ]
      } 

    }
}

我得到了这样的响应:

  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 4,
    "skipped" : 0,
    "failed" : 1,
    "failures" : [
      {
        "shard" : 2,
        "index" : "twitter",
        "node" : "AClIunrSRUKb1gbhBz-JoQ",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "painless_class" : "java.lang.String",
          "to_string" : "[and, by, cutting, doug, hadoop, jack, jim, lucene, made, spark, the, was]",
          "java_class" : "java.lang.String",
          "script_stack" : [
            "Debug.explain(text);\n                         ",
            "              ^---- HERE"
          ],
          "script" : """
                          String text = doc['body'].toString();
                          Debug.explain(text);
                         return true;
                      """,
          "lang" : "painless",
          "caused_by" : {
            "type" : "painless_explain_error",
            "reason" : null
          }
        }
      }
    ]
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

正如你所看到的那样,调试结果显示 doc['body'].toString() 实际上是一个术语表 [and, by, cutting, doug, hadoop, jack, jim, lucene, made, spark, the, was]. 我想得到的是访问原文,在这个例子中是"body" : "The Lucene was made by Doug Cutting and the hadoop was made by Jim and Spark was made by jack"

注:我已将 "fielddata": true"store":true 在这个字段上,还将文档编入了一个索引。body.exact 字段,这样术语就不会被分析,但我的问题是,我不能在过滤器上下文中访问脚本中的原始文本,我总是得到独特的术语列表。

非常感谢你的帮助

解决方案:

你可以使用 keyword 数据类型:

PUT twitter
{
  "mappings": {
    "_doc": {
      "properties": {
        "body": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
GET twitter/_search
{
  "query": {
    "bool": {
      "must": {
        "term": {
          "body": "spark"
        }
      },
      "filter": [
        {
          "script": {
            "script": {
              "lang": "painless",
              "source": """
                          String text = doc['body.keyword'].toString();
                          Debug.explain(text);
                         return true;
"""
            }
          }
        }
      ]
    }
  }
}

屈服

"painless_class" : "java.lang.String",
          "to_string" : "[The Lucene was made by Doug Cutting and the hadoop was made by Jim and Spark was made by jack]",
          "java_class" : "java.lang.String",
          "script_stack" : [
            "Debug.explain(text);\n                         ",
            "              ^---- HERE"
          ],
          "script" : """
                          String text = doc['body.keyword'].toString();
                          Debug.explain(text);
                         return true;
""",

给TA打赏
共{{data.count}}人
人已打赏
未分类

太多的方法非常相似

2022-9-8 8:48:41

未分类

如何水平扩展我的页脚div?

2022-9-8 8:59:36

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索