2014-12-23 4 views
0

Я использую ElasticSearch 1.4.2 и хочу обновить некоторый документ в формате json. Когда я пытаюсь вставить тестовый документ, я могу найти его только через id, но не через поиск в поле logName или других, которые я пытался. Возможно, я пропустил шаг в методе upsert, это в конце вопроса.Индекс не обновляется при обновлении документа

Вот результаты запроса:

$ curl -XGET http://localhost:9200/pwotest/_search?q=logName:%22pwotest1%22\&pretty=true 
{ 
    "took" : 1, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 0, 
    "max_score" : null, 
    "hits" : [ ] 
    } 
} 

Поиск результатов ид в документе, где LogName является pwotest1:

$ curl -XGET http://localhost:9200/pwotest/_search?q=\$oid:%22549954143004ba1bf99a56ba%22\&pretty=true 
{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 3.1972246, 
    "hits": [ 
     { 
     "_index": "pwotest", 
     "_type": "User", 
     "_id": "549954143004ba1bf99a56ba", 
     "_score": 3.1972246, 
     "_source": { 
      "_id": { 
      "$oid": "549954143004ba1bf99a56ba" 
      }, 
      "logName": "pwotest1", 
      "modifiedBy": "test", 
      "modifiedId": "549954143004ba1bf99a56ba", 
      "modificationDate": 1419334676507, 
      "internalType": "create", 
      "dm_Version": "0.8.2", 
      "creationDate": 1419334676515, 
      "createdId": "549954143004ba1bf99a56ba" 
     } 
     } 
    ] 
    } 
} 

Код для upsert документ в Java и выглядит следующим образом:

/** 
* See <a href="http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/java-update-api.html">ES doc</a> 
* @param o is a representation of PWO object 
* @throws PWOException 
*/ 
public void update(JsonObject o) throws PWOException { 
    Preconditions.checkNotNull(index, "index must not be null for update"); 
    getNode(); 
    Client client = node.client(); 
    // this needs to come from somewhere 
    String type = "User"; 
    String id = GsonHelper.getId(o).get(); 
    String json = new Gson().toJson(o); 
    IndexRequest indexRequest = new IndexRequest(index, type, id). 
      source(json); 
    UpdateRequest upd = new UpdateRequest(index, type, id). 
      doc(json). 
      upsert(indexRequest); 
    Logger.info("Update is %s [%s, %s, %s]", upd, index, type, id); 
    try { 
     client.update(upd).get(); 
    } catch (InterruptedException | ExecutionException e) { 
     throw new PWOException(GsonHelper.createMessageJsonObject("Update in elastic search failed"), e); 
    } 
} 

ответ

1

похоже, что ваш _id в вашем источнике испорчен:

"_id":{"$oid":"549954143004ba1bf99a56ba"} 

, если вы можете получить это, чтобы быть основной ценности я довольно уверен, ваш запрос будет работать, например .:

"_id":"549954143004ba1bf99a56ba" 
+0

Спасибо, я трансформировал объект JSON в обновление сейчас, и это работает! Интересно, почему работают другие документы, поскольку у них есть похожие объекты id ... – TeTeT

Смежные вопросы