Reputation: 2094
I'm using ElasticSearch 1.4.2 and want to upsert some document in json format to it. When I try to insert a test document I can only find it via id, but not via search of the logName field or others that I tried. Probably I miss a step in the upsert method, it's at the end of the question.
Here the query results:
$ 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" : [ ]
}
}
Searching for the id results in a document where logName is 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"
}
}
]
}
}
The code to upsert the document is in Java and looks like this:
/**
* 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);
}
}
Upvotes: 0
Views: 256
Reputation: 6180
it looks like your _id in your source is messed up:
"_id":{"$oid":"549954143004ba1bf99a56ba"}
if you can get this to be a basic value I'm pretty confident your query will work, e.g.:
"_id":"549954143004ba1bf99a56ba"
Upvotes: 1