插入文档时索引未更新

时间:2014-12-23 11:57:56

标签: java elasticsearch

我正在使用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的文档中搜索id结果:

$ 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"
        }
      }
    ]
  }
}

用于备份文档的代码是用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 个答案:

答案 0 :(得分:1)

您的来源中的_id似乎搞砸了:

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

如果您能将此作为基本价值我非常有信心您的查询将会起作用,例如:

"_id":"549954143004ba1bf99a56ba"