Elasticsearch:根据文本字段中搜索字符串的索引值对文档进行排序
Elasticsearch: Sort the Documents on the index value of the search string in a text field
我有这样的 Elasticsearch 数据-
PUT /text/_doc/1
{
"name": "pdf1",
"text":"For the past six weeks. The unemployment crisis has unfolded so suddenly and rapidly."
}
PUT /text/_doc/2
{
"name": "pdf2",
"text":"The unemployment crisis has unfolded so suddenly and rapidly."
}
在这个例子中,我进行了全文搜索,我正在搜索 "text" 字段中具有 "unemployment" 子字符串的所有文档。最后,我希望所有文档按照 "text" 字段中 "unemployment" 字符串的索引值的升序排序。例如 - 子字符串 "unemployment" 在索引“4”的 doc2 中排在第一位,所以我希望该文档在结果中首先返回。
GET /text/_search?pretty
{
"query": {
"match": {
"text": "unemployment"
}
}
}
我尝试了一些类似 term_vector 的方法,这是我使用的映射,但它没有帮助。
PUT text/_mapping
{
"properties": {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"text" : {
"type" : "text",
"term_vector": "with_positions_offsets"
}
}
}
任何人都可以帮助我进行正确的映射和搜索查询吗?
提前致谢!
试试这个查询
GET text/_search
{
"query": {
"function_score": {
"query": {
"match": {
"text": "unemployment"
}
},
"functions": [
{
"script_score": {
"script": {
"source": """
def docval = doc['text.keyword'].value;
def length = docval.length();
def index = (float) docval.indexOf('unemployment');
// the sooner the word appears the better so 'invert' the 'index'
return index > -1 ? (1 / index) : 0;
"""
}
}
}
],
"boost_mode": "sum"
}
}
}
使用自动生成的映射
{
"text" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"text" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
请注意,这是区分大小写的,因此也有一个小写规范化关键字字段然后在脚本评分脚本中访问它是合理的。 可能会让您走上正确的道路。
我有这样的 Elasticsearch 数据-
PUT /text/_doc/1
{
"name": "pdf1",
"text":"For the past six weeks. The unemployment crisis has unfolded so suddenly and rapidly."
}
PUT /text/_doc/2
{
"name": "pdf2",
"text":"The unemployment crisis has unfolded so suddenly and rapidly."
}
在这个例子中,我进行了全文搜索,我正在搜索 "text" 字段中具有 "unemployment" 子字符串的所有文档。最后,我希望所有文档按照 "text" 字段中 "unemployment" 字符串的索引值的升序排序。例如 - 子字符串 "unemployment" 在索引“4”的 doc2 中排在第一位,所以我希望该文档在结果中首先返回。
GET /text/_search?pretty
{
"query": {
"match": {
"text": "unemployment"
}
}
}
我尝试了一些类似 term_vector 的方法,这是我使用的映射,但它没有帮助。
PUT text/_mapping
{
"properties": {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"text" : {
"type" : "text",
"term_vector": "with_positions_offsets"
}
}
}
任何人都可以帮助我进行正确的映射和搜索查询吗?
提前致谢!
试试这个查询
GET text/_search
{
"query": {
"function_score": {
"query": {
"match": {
"text": "unemployment"
}
},
"functions": [
{
"script_score": {
"script": {
"source": """
def docval = doc['text.keyword'].value;
def length = docval.length();
def index = (float) docval.indexOf('unemployment');
// the sooner the word appears the better so 'invert' the 'index'
return index > -1 ? (1 / index) : 0;
"""
}
}
}
],
"boost_mode": "sum"
}
}
}
使用自动生成的映射
{
"text" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"text" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
请注意,这是区分大小写的,因此也有一个小写规范化关键字字段然后在脚本评分脚本中访问它是合理的。