Elasticsearch - 基于文本长度的查询

时间:2016-10-09 10:43:25

标签: node.js elasticsearch filter

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

#First rewrite any request to the wrong domain to use the correct one (here www.)

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


#Change to / instead of /path/index.php (Doesn't work.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ path/index.php?/$1 [L]

我想使用术语&#34来查询我的索引;这是一个段落" 并使用最相似的文本长度来提升结果,IE: document _id: 1

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

1 个答案:

答案 0 :(得分: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"
      }
    }
  ]
}