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({
\t "host": "localhost:9200",
\t "log": "info",
\t "apiVersion": "5.0",
\t "requestTimeout": 1000
})
function createIndex (index, structure) {
\t return client.indices.create({ index: index, body: structure })
}
function deleteIndex (index) {
\t return client.indices.delete({ index: index })
}
function now() {
\t return parseInt(Date.now()/1000)
}
function search (index_name, type, key, ttl) {
\t var a = now() - ttl
\t var query = esbuilder()
\t \t .filter('term', 'cachekey', key)
\t \t .filter('range', 'created', { gt: a })
\t \t .build()
\t var data = {
\t \t index: index_name,
\t \t type: type,
\t \t body: query
\t }
\t console.log("\r\n#search():", utils.inspect(data, { depth: null }))
\t return client.search(data)
}
function save (index_name, type, key, value) {
\t var data = {
\t \t index: index_name,
\t \t type: type,
\t \t body: {
\t \t \t created: now(),
\t \t \t cachekey: key,
\t \t \t result: value
\t \t }
\t }
\t console.log("\r\n#save():", utils.inspect(data, { depth: null }))
\t
\t return client.index(data)
}
console.log("start")
var index_name = 'whywhywhy'
var structure = {
\t mappings: {
\t \t darkside: {
\t \t \t properties: {
\t \t \t \t created: {
\t \t \t \t \t type: 'date'
\t \t \t \t },
\t \t \t \t cachekey: {
\t \t \t \t \t type: 'string'
\t \t \t \t },
\t \t \t \t result: {
\t \t \t \t \t type: 'object',
\t \t \t \t \t enabled: false
\t \t \t \t }
\t \t \t }
\t \t }
\t }
}
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) {
\t console.log("\r\nIndex created: ", utils.inspect(result))
\t return save(index_name, type_name, key, val).then(function (result) {
\t \t console.log("\r\nData saved: ", utils.inspect(result))
\t \t console.log("\r\nWaiting for 10 seconds")
\t \t setTimeout(function() {
\t \t \t return search(index_name, type_name, key, ttl).then(function (result) {
\t \t \t \t console.log("\r\nGot this: ", utils.inspect(result))
\t \t \t \t setTimeout(function() {
\t \t \t \t \t return deleteIndex(index_name).then(function (result) {
\t \t \t \t \t \t console.log("\r\nClean up, index deleted: ", utils.inspect(result))
\t \t \t \t \t })
\t \t \t \t }, 1500)
\t \t \t })
\t \t }, 10000)
\t })
}).catch(function (err) {
\t 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 }
시도는'대신 string''의 keyword'하는 UUID 당신이 그것을 찾을 수 없습니다, 따라서 왜, 분석 및 토큰 화되고있다. – Val
이것은 실제로 맞습니다. 문서에도 있지만 유스 케이스와 관련이없는 섹션에 있습니다. 내가 모든 것을 읽지 않는 것이 틀림 없다고 생각해. 고맙습니다. 답변을 추가 하시겠습니까? – Kristian