Elasticsearch将找不到最近插入的数据

时间:2017-04-06 08:30:03

标签: node.js elasticsearch

我在玩ElasticSearch时遇到了问题。我想要做的是插入一些虚拟数据然后检索它,但由于某种原因,ElasticSearch不会返回任何内容。

基本上我正在做的是将数据(唯一ID,时间戳和对象)插入索引,然后我等待一段时间(10秒),最后我试图通过唯一ID检索插入的记录(uuid v4)我生成并检查记录是否不太旧(ttl,不包括此检查似乎没有区别)。但对于未知(对我而言)的原因,它似乎并没有起作用。

我一直在敲打我的头一段时间,我无法弄清楚出了什么问题。非常感谢任何帮助或想法。

重现的步骤:

必备软件:

  • NodeJS v6.10.1
  • ElasticSearch 5.2.1

依赖关系:

npm install uuid bodybuilder elasticsearch

test.js

var uuid = require('uuid')
var utils = require('util')
var esbuilder = require('bodybuilder')
var Elasticsearch = require('elasticsearch')

var client = Elasticsearch.Client({
	"host": "localhost:9200",
	"log": "info",
	"apiVersion": "5.0",
	"requestTimeout": 1000
})

function createIndex (index, structure) {
	return client.indices.create({ index: index, body: structure })
}

function deleteIndex (index) {
	return client.indices.delete({ index: index })
}

function now () {
	return parseInt(Date.now() / 1000)
}

function search (index_name, type, key, ttl) {
	var a = now() - ttl

	var query = esbuilder()
		.filter('term', 'cachekey', key)
		.filter('range', 'created', { gt: a })
		.build()

	var data = {
		index: index_name,
		type: type,
		body: query
	}

	console.log("\r\n#search():", utils.inspect(data, { depth: null }))

	return client.search(data)
}

function save (index_name, type, key, value) {
	var data = {
		index: index_name,
		type: type,
		body: {
			created: now(),
			cachekey: key,
			result: value
		}
	}

	console.log("\r\n#save():", utils.inspect(data, { depth: null }))
	
	return client.index(data)
}


console.log("start")

var index_name = 'whywhywhy'
var structure = {
	mappings: {
		darkside: {
			properties: {
				created: {
					type: 'date'
				},
				cachekey: {
					type: 'string'
				},
				result: {
					type: 'object',
					enabled: false
				}
			}
		}
	}
}

var type_name = 'darkside'
var ttl = 3600 // 5 minutes
var val = { name: 'John', occupation: 'plumber' }
var key = uuid.v4()

createIndex(index_name, structure).then(function (result) {

	console.log("\r\nIndex created: ", utils.inspect(result))

	return save(index_name, type_name, key, val).then(function (result) {
		console.log("\r\nData saved: ", utils.inspect(result))

		console.log("\r\nWaiting for 10 seconds")

		setTimeout(function () {
			return search(index_name, type_name, key, ttl).then(function (result) {

				console.log("\r\nGot this: ", utils.inspect(result))

				setTimeout(function () {
					return deleteIndex(index_name).then(function (result) {
						console.log("\r\nClean up, index deleted: ", utils.inspect(result))
					})
				}, 1500)
			})
		}, 10000)
	})

}).catch(function (err) {

	console.log("\r\nSome fucker failed: ", err)
})

示例输出:

start

Index created:  { acknowledged: true, shards_acknowledged: true }

#save(): { index: 'whywhywhy',
  type: 'darkside',
  body:
   { created: 1491466910,
     cachekey: '55626fcd-9cc9-4ffa-822f-9bebf9652f3d',
     result: { name: 'John', occupation: 'plumber' } } }

Data saved:  { _index: 'whywhywhy',
  _type: 'darkside',
  _id: 'AVtCWvn4O-wYLGwwrqjz',
  _version: 1,
  result: 'created',
  _shards: { total: 2, successful: 1, failed: 0 },
  created: true }

Waiting for 10 seconds

#search(): { index: 'whywhywhy',
  type: 'darkside',
  body:
   { query:
      { bool:
         { filter:
            { bool:
               { must:
                  [ { term: { cachekey: '55626fcd-9cc9-4ffa-822f-9bebf9652f3d' } },
                    { range: { created: { gt: 1491463320 } } } ] } } } } } }

Got this:  { took: 4,
  timed_out: false,
  _shards: { total: 5, successful: 5, failed: 0 },
  hits: { total: 0, max_score: null, hits: [] } }

Clean up, index deleted:  { acknowledged: true }

1 个答案:

答案 0 :(得分:2)

您应该将cachekey字段的数据类型设置为keyword而不是string(或text),否则您的UUID值将被分析并标记化。这就是你之后无法找到它的原因。

var structure = {
    mappings: {
        darkside: {
            properties: {
                created: {
                    type: 'date'
                },
                cachekey: {
                    type: 'keyword'            <--- change this
                },
                result: {
                    type: 'object',
                    enabled: false
                }
            }
        }
    }
}