【问题标题】:Elasticsearch - Query based on text lengthElasticsearch - 基于文本长度的查询
【发布时间】:2017-02-17 22:56:13
【问题描述】:

我正在使用官方的 Elasticsearch NodeJS 客户端库,来查询以下索引结构:

{
  "_index": "articles",
  "_type": "context",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "article": "this is a paragraph",
    "topic": "topic A"
  }
}

{
  "_index": "articles",
  "_type": "context",
  "_id": "2",
  "_version": 1,
  "found": true,
  "_source": {
    "article": "this is a paragraph this is a paragraph this is a paragraph",
    "topic": "topic B"
  }
}

我想使用术语“这是一个段落”查询我的索引,并使用最相似的文本长度提升结果,即:document _id:1

我可以在不重新编制索引并将字段添加到我的索引 (as described here) 的情况下执行此操作吗?

【问题讨论】:

  • 如果您无法更改映射或重新建立索引,那么可能是在查询时使用 Groovy 脚本?
  • 感谢您的回复,我是弹性搜索的新手...您详细说明的颜色。
  • 嗯...让我发布一个示例查询作为示例...
  • 您是否有更详细的示例说明您要实现的目标以及“最相似的文本长度”是什么意思?
  • 感谢回复,基本上是想匹配内容相近,字数相近的文章

标签: node.js elasticsearch filter


【解决方案1】:

下面的查询使用 Groovy 查看索引到 ES 中的实际文本的长度(使用_source.article.length())以及要搜索的文本的长度。作为一个非常简单的基本查询,我使用了match_phrase,然后根据要搜索的文本与原始文本的长度相比对文档进行了重新评分。

GET /articles/context/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_phrase": {
          "article": "this is a paragraph"
        }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "inline": "text_to_search_length=text_to_search.length(); compared_length=_source.article.length();return (compared_length-text_to_search_length).abs()",
              "params": {
                "text_to_search": "this is a paragraph"
              }
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_score": {
        "order": "asc"
      }
    }
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多