elasticsearch - Elasticsearch:在文本字段中根据搜索字符串的索引值对文档进行排序

标签 elasticsearch elasticsearch-aggregation elasticsearch-query

我有这样的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”子字符串的文档。最后,我希望所有文档在“文本”字段中按“失业”字符串的索引值的升序排列。例如-子字符串“失业”首先在doc2的索引“4”处出现,因此我希望此文档在结果中首先返回。
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
            }
          }
        }
      }
    }
  }
}

请注意,这是区分大小写的,因此也有一个小写标准化的关键字字段,然后在脚本分数脚本中访问它是合理的。 This可能会让您走上正确的道路。

关于elasticsearch - Elasticsearch:在文本字段中根据搜索字符串的索引值对文档进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62139601/

相关文章:

elasticsearch - Elasticsearch:NEST中具有基数的复合聚合

elasticsearch - 如何将AND条件与 'must'和 'should'结合?

hadoop - 从Hive加载数据到ES时获取 “EsHadoopIllegalArgumentException: Index [petrol/petrolCat] missing and settings”

bash - 如何通过docker compose或swarm模式运行Elasticsearch并使用命令安装插件

elasticsearch - 在 elasticsearch 中返回唯一结果

Elasticsearch:聚合两个字段

elasticsearch - 将动态模板应用于多种类型 - 用于管理排序标记

elasticsearch - Pyspark与Elasticsearch

Elasticsearch SQL 类子查询聚合

elasticsearch - Elasticsearch建议功能的前缀为like