elasticsearch,filter,Node.js,elasticsearch,Filter" /> elasticsearch,filter,Node.js,elasticsearch,Filter" />

Node.js Elasticsearch-基于文本长度的查询

Node.js Elasticsearch-基于文本长度的查询,node.js,elasticsearch,filter,Node.js,elasticsearch,Filter,我使用官方的Elasticsearch NodeJS客户端库来查询以下索引结构: { "_index": "articles", "_type": "context", "_id": "1", "_version": 1, "found": true, "_source": { "article": "this is a paragraph", "topic": "topic A" } } { "_index": "articles", "_

我使用官方的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\u id:1


我可以在不重新索引和向索引()添加字段的情况下执行此操作吗?

下面的查询使用Groovy查看索引到ES的实际文本的长度(使用
\u 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"
      }
    }
  ]
}

如果您无法更改映射或重新编制索引,那么可能在查询时使用Groovy脚本?感谢您的回复,我是elasticsearch的新手。。。你精心设计的颜色。嗯。。。让我发布一个示例查询…您是否有一个更详细的示例,说明您试图实现的目标以及“最相似的文本长度”的含义?感谢您的回复,基本上我希望匹配具有相似内容和词数相似的文章