简体   繁体   English

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

[英]Elasticsearch: Sort the Documents on the index value of the search string in a text field

I have Elasticsearch data like this-我有这样的 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."
}

In this example I am making a full text search, I am searching for all the documents that have "unemployment" sub-string in the "text" field.在此示例中,我正在进行全文搜索,我正在搜索“文本”字段中具有“失业”子字符串的所有文档。 And in the end i want all the documents sorted in the ascending order of the index value of "unemployment" string in the "text" field.最后,我希望所有文档按“文本”字段中“失业”字符串的索引值的升序排序。 For eg - the sub-string "unemployment" comes first in the doc2 at index "4" so i want this document to be returned first in the results.例如 - 子字符串“失业”首先出现在索引“4”的 doc2 中,所以我希望这个文档首先在结果中返回。

GET /text/_search?pretty
{
  "query": {
    "match": {
      "text": "unemployment"
    }
  }
}

I have tried few things like term_vector, here is the mapping that i used but it didn't help.我尝试了一些类似 term_vector 的东西,这是我使用的映射,但没有帮助。

PUT text/_mapping
{
    "properties": {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        },
        "text" : {
          "type" : "text",
          "term_vector": "with_positions_offsets"
        }
      }
}

Can anyone please help me in making the right mapping and search Query?谁能帮助我进行正确的映射和搜索查询?

Thanks in Advance!提前致谢!

Try this query试试这个查询

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"
    }
  }
}

using the auto-generated mapping使用自动生成的映射

{
  "text" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "text" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

Note that this is case-sensitive so it'd be reasonable to have a lowercase-normalized keyword field too and then access it in the script score script.请注意,这是区分大小写的,因此有一个小写规范化的关键字字段也是合理的,然后在脚本得分脚本中访问它。 This might get you on the right path.可能会让你走上正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在索引文档 elasticsearch 中搜索文本 - search a text in index documents elasticsearch 如何在Elasticsearch索引中搜索值为字段为空数组的文档? - How to search for documents in an elasticsearch index having value as empty array for a field? PHP Elasticsearch从索引中的所有文档中获取字段的值 - PHP Elasticsearch GET value of a field from all documents in index 弹性搜索使用其他字段值对具有相同分数的文档进行排序 - Elastic Search sort documents with same score using another field value 在Elasticsearch中获取其文本字段仅包含数字值的文档 - Get documents whose text field contains only a number value in Elasticsearch Elasticsearch - 以任何方式找出所有字段值为文本的文档 - Elasticsearch - Any way to find out all the documents with field value as text 在xml标记值上对ElasticSearch文档进行分组(在字符串字段中) - Grouping ElasticSearch documents on xml tag value (in a string field) ElasticSearch 对所有文档的嵌套字段进行排序 - ElasticSearch Sort on nested field across all documents 如何通过索引文档中字段的部分文本来搜索Elasticsearch索引? - How to search a elasticsearch index by partial text of a field in the indexed document? 刷新索引之前的“ Elasticsearch”搜索文档 - “Elasticsearch” search documents before index refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM