gpt4 book ai didi

elasticsearch - 如何在 Elasticsearch 查询的筛选器上下文中轻松获取脚本中的文本字段?

转载 作者:行者123 更新时间:2023-12-02 23:09:56 25 4
gpt4 key购买 nike

我使用过滤器并在过滤器上下文中构造了一个 flex 搜索查询,并且我正在编写一个易于编写的脚本,用于基于文本字段的主体过滤某些文档。但是,当我要访问文本字段时,会得到一个术语列表,而不是原始文本。我正在寻找一种在无痛脚本中而不是术语列表中访问原始文本正文的方法。
或者,如果无法访问文本正文,则我想在此上下文中访问术语频率 vector 。

例如,如果我运行此查询:

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 datatype:

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;
""",

关于elasticsearch - 如何在 Elasticsearch 查询的筛选器上下文中轻松获取脚本中的文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61389599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com