# Mapping for versioned K,V pairs

**URL:** <https://discuss.elastic.co/t/mapping-for-versioned-k-v-pairs/275354>\
**Category:** Elasticsearch\
**Created:** [June 8, 2021, 10:10pm UTC](https://discuss.elastic.co/t/mapping-for-versioned-k-v-pairs/275354 "2021-06-08T22:10:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhishekc92](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@abhishekc92](https://discuss.elastic.co/u/abhishekc92)\
**Post date:** [June 8, 2021, 10:10pm UTC](https://discuss.elastic.co/t/mapping-for-versioned-k-v-pairs/275354/1 "2021-06-08T22:10:02Z")

</div>

Hi,  
I'm trying to come up with a mapping for a document storing versioned custom key value pairs. Eg.

```auto
 version : "1" => [{key : "customkey1", value : "val1"}, {key : "customkey2", value : "val2"}..]
version: "2" => [{key : "customkey100", value : "val100"},]..
version: "3" => [{key : "customkey1", value : "valxyz"}]..

```

Problem statement being I want to be able to search on any of these key values, and only return the key and value set associated with the latest version ie. search for key=customkey2 && value=val2 =\> return [{key : "customkey1", value : "valxyz"}].  
It is also acceptable to return this whole document and I can extract the latest version set via application logic. Is the nested type the only way to go?  
TIA

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [June 10, 2021, 9:06pm UTC](https://discuss.elastic.co/t/mapping-for-versioned-k-v-pairs/275354/2 "2021-06-10T21:06:17Z")

</div>

Yes, if you want to search on several fields of an object in a single query, nested field type is the only way to go. I am not very clear how `version` is modelled? Does a single document contains all versions?

---

<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 8, 2021, 9:06pm UTC](https://discuss.elastic.co/t/mapping-for-versioned-k-v-pairs/275354/3 "2021-07-08T21:06:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
