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

**URL:** <https://discuss.elastic.co/t/using-a-java-object-as-a-template-for-an-elastic-search-query/18585>\
**Category:** Elasticsearch\
**Created:** [July 10, 2014, 7:02pm UTC](https://discuss.elastic.co/t/using-a-java-object-as-a-template-for-an-elastic-search-query/18585 "2014-07-10T19:02:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![goncalo\_luiz](https://avatars.discourse-cdn.com/v4/letter/g/e99b99/32.png) [@goncalo\_luiz](https://discuss.elastic.co/u/goncalo_luiz)\
**Post date:** [July 10, 2014, 7:02pm UTC](https://discuss.elastic.co/t/using-a-java-object-as-a-template-for-an-elastic-search-query/18585/1 "2014-07-10T19:02:48Z")

</div>

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](mailto: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](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](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:16am UTC](https://discuss.elastic.co/t/using-a-java-object-as-a-template-for-an-elastic-search-query/18585/2 "2017-07-06T01:16:43Z")

</div>

Closed on July 6, 2017, 1:16am UTC


