2012-12-06 3 views
8

Ho provato a definire la proprietà _timestamp in un indice. Quindi, prima, creo l'indiceElasticsearch _timestamp

curl -XPUT 'http://elasticsearch:9200/ppe/'

risposta dal server: {"ok":true,"acknowledged":true}

poi ho cercato di definire la mappatura con una _timestamp

curl -Xput 'http://elasticsearch:9200/ppe/log/_mapping' -d '{ 
    "log": { 
    "properties": { 
     "_ttl": { 
     "enabled": true 
     }, 
     "_timestamp": { 
     "enabled": true, 
     "store": "yes" 
     }, 
     "message": { 
     "type": "string", 
     "store": "yes" 
     }, 
     "appid": { 
     "type": "string", 
     "store": "yes" 
     }, 
     "level": { 
     "type": "integer", 
     "store": "yes" 
     }, 
     "logdate": { 
     "type": "date", 
     "format": "date_time_no_millis", 
     "store": "yes" 
     } 
    } 
    } 
}' 

e ricevo come risposta da the server

{ 
    "error": "MapperParsingException[No type specified for property [_timestamp]]", 
    "status": 400 
} 

Cosa c'è di sbagliato nella mia mappatura?

risposta

16

campi speciali come _ttl e _timestamp devono essere definiti allo stesso livello come oggetto properties:

curl -Xput 'http://elasticsearch:9200/ppe/log/_mapping' -d '{ 
    "log": { 
     "_ttl": { 
      "enabled": true 
     }, 
     "_timestamp": { 
      "enabled": true, 
      "store": "yes" 
     }, 
     "properties": { 
      "message": { 
       "type": "string", 
       "store": "yes" 
      }, 
      "appid": { 
       "type": "string", 
       "store": "yes" 
      }, 
      "level": { 
       "type": "integer", 
       "store": "yes" 
      }, 
      "logdate": { 
       "type": "date", 
       "format": "date_time_no_millis", 
       "store": "yes" 
      } 
     } 
    } 
} 
' 
3

nota però che sebbene _timestamp è definita su livello superiore sarà restituito all'interno fields:

curl 'http://localhost:9200/myindex/mytype/AUqL0PW7YDMmKSIKO1bk?pretty=true&fields=_timestamp' 
{ 
    "_index" : "myindex", 
    "_type" : "mytype", 
    "_id" : "AUqL0PW7YDMmKSIKO1bk", 
    "_version" : 1, 
    "found" : true, 
    "fields" : { 
    "_timestamp" : 1419684935099 
    } 
} 

Si noti che _timestamp deve essere richiesto esplicitamente da fields=_timestamp o fields=_timestamp,_source.

Nota che _timestamp può essere restituito solo quando questo campo è contrassegnato come 'store': true. Ma c'è un modo per accedere a questo valore quando sono ordinati per _timestamp, in questo modo:

curl 'http://localhost:9200/myindex/mytype/_search?pretty=true' -d ' 
    { "sort": [ "_timestamp" ], "size": 1} 
' 

fornisce al risultato:

{ 
    "took" : 1, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 3, 
    "max_score" : null, 
    "hits" : [ { 
     "_index" : "myindex", 
     "_type" : "mytype", 
     "_id" : "AUqL0PDXYDMmKSIKO1bj", 
     "_score" : null, 
     "sort" : [ 1419684933847 ] 
    } ] 
    } 
} 

E ora sort[0] è il valore per il primo (e l'unico in questo caso) valore di ordinamento: _timestamp. _timestamp non deve essere contrassegnato come "store": true se utilizzato in questo modo.

+0

Ho pensato per molto tempo che le mie impostazioni di "_timestamp" sull'indice non erano corrette o qualcosa del genere perché i miei documenti non tornavano con questo campo. Grazie per aver reso nota la nota "Si noti che '_timestamp' deve essere esplicitamente richiesto da 'fields = _timestamp' o 'fields = _timestamp, _source'.", Questo mi ha davvero aiutato! –

+0

@JesseWebb: sei il benvenuto! –