如何使用 ElasticSearch 在一段文本中找到数字范围

How can I find a number range inside a piece of text using ElasticSearch

我一直在通过互联网阅读,试图找出如何使用 Elastic Search 在一段文本中找到数字范围。但是我没有运气。

举个例子,假设我有以下一组文档(请注意,该文档并未拆分为多个字段,它只是一个文本块)。

文档1{ 消息:"I have 7 books" }

文档2{ 消息:"I have 15 books" }

文档3{ 消息:"I have 19 books" }

是否可以使用 ElasticSearch 形成一个查询来查找所有拥有 10 到 20 本书的人?

谢谢 富有

在 ES 1.5 中,keep_types token filter 显然是为这类事情设计的。我在这段代码中设置了它(使用 ES 1.5),它似乎有效:

http://sense.qbox.io/gist/b2c86b748d0c33957df1dcb90a3b405b0a4ca646

但是,我实际上并不需要它来让它工作。 standard analyzer 根据空格将文本划分为标记,因此您可以对字段应用 range 查询(过滤器也有效),它似乎可以执行您想要的操作:

我设置了一个简单的索引:

DELETE /test_index

PUT /test_index

POST /test_index/doc/_bulk
{ "index": { "_id": 1 }}
{ "msg": "I have 7 books" }
{ "index": { "_id": 2 }}
{ "msg": "I have 15 books" }
{ "index": { "_id": 3 }}
{ "msg": "I have 19 books" }

然后使用范围查询:

POST /test_index/_search
{
    "query": {
        "range": {
           "msg": {
              "from": 10,
              "to": 20
           }
        }
    }
}
...
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 1,
            "_source": {
               "msg": "I have 15 books"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "3",
            "_score": 1,
            "_source": {
               "msg": "I have 19 books"
            }
         }
      ]
   }
}

这是第二个例子的代码:

http://sense.qbox.io/gist/0979803673efb5b7ff063c257efd82617a93bd06