Using a java object as a template for an elastic search query


(goncalo.luiz) #1

Hello all,

First of all I'd like to say I never expected the ES community to be as
vibrant as it is. I've only recently registered in this group and already
had the chance to learn so many things from the today's messages. I'm still
very new to ES and reading other people's questions really help. Thanks for
that !

I know what I'm about to ask is a long shot but...

Context:
I've been playing around with the idea to provide a persistence storage for
a Map. One possible application is to implement Hazelcast's MapStore and
persist the whole HZ across JVM reboots.

The model I've came up with was to persist ES's documents as:

#########
key:{...}
value:{...}
#########

I've stored a few entries like this without any problem as expected.
Narrowing down the example for key objects that only contain primitive
types an entry would like like this:

#########
key:{
oid1:178,
oid2:555,
oid3:4798
}
value:{
school:"MIT",
address: {address:"...",postcode:"..."}
}
#########

I'd use the first chunk of the document to store the key and the rest to
store the value. Disregarding key schema evolution, and the meaning of
missing/null fields for now, I'm finding it unexpectedly hard to produce a
query that represents a key value to search for.

Question:
Building the object to store is straightforward: I have a Entry java object
with a key and a value and use Jackson to produce the json value and use it
as the source of the index call.

To retrieve, i'd like to perform a search on the "key" field. I can easily
search using the server REST API with a query like the following:

{
"query": {
"filtered": {
"filter": {
"and": {
"must": [
{
"term": {
"key.oid1": 178
}
}
]
}
}
}
}
}

My question is, is there a Java method to produce such queries based on the
serialization of the provided key object, like a template? So far I'm not
making any assumptions about the key type (will also probably not attempt
to use nested complex types) so I'll have to dynamically generate the
query. I can always fall back to reflection and add a term for each found
property (as the semantics for this specific case is an AND for all fields)
but I'd rather ask before if there's a more recommended mechanism. Once the
value is found retrieving the object should be easy with the inverse
Jackson operation.

There's still a lot of questions to answer on this approach (what happens
when the key evolves? what to do with nested objects in key objects) but so
far it doesn't seem to be impossible.

Thanks for your comments,
Gonçalo

Gonçalo Luiz

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAMrm09rSa4ADHBKzthMZSS8NS9gWzX%3DMq%2BzQSm%2B4OYg_ETQwYg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(system) #2