Indexes on Maps

235 views
Skip to first unread message

Kai

unread,
Aug 8, 2011, 8:25:43 AM8/8/11
to mor...@googlegroups.com
Hi,

I wanted to get a better understanding of how indexes work on HashMaps with Morphia.

If I have an object like this:

class Job {
  @Indexed
  Map<String, String> taskMap = new HashMap<String, String>();
}

Will each key that gets stored in "taskMap" be indexed for fast retrieval?

Thanks.

Scott Hernandez

unread,
Aug 8, 2011, 8:46:25 AM8/8/11
to mor...@googlegroups.com
Not in the way you think. A map turns into this structure:

taskMap: {
key1 : value,
key2 : value,
...
}

And the index will be created on taskMap, not taskMap.* (which isn't
supported on the server), and indexes the complete embedded doc.

I think you want this structure:

taskMap : [
{name: key1, value: value },
{name: key1, value: value },
...
]

And then you can index the "name" field so you can quickly find the
docs with a certain name (key in the map example).

Also, your terminology "for fast retrieval" is confusing. What do you
mean by that? Is there some query you want to perform well?

Kai

unread,
Aug 8, 2011, 4:00:28 PM8/8/11
to mor...@googlegroups.com
Sorry, am a bit confused. To give a better example,

If I have Traveller Class that wraps hashmap with potential values like this:

site: Grand Canyon
travelMedium: Car
yearVisited: 2010

Class Traveller {
@Indexed
  Map<String, String> taskMap = new HashMap<String, String>();
}


I wanted to have each potential "key" in the Map be indexed, so when I write a query to get all Travellers who have visited Grand Canyon between 2006-2010, the query speed would be fast since each key is indexed.

Can you please describe how the object structure you mentioned above would look like in Java using Morphia?

Thanks!



Kai

unread,
Aug 12, 2011, 8:51:46 PM8/12/11
to mor...@googlegroups.com
Thanks for the tip Scott. I converted my map to a list of objects which essentially contains a key/value field and both fields are index. This way, I can achieve something like a map.* index.

Thanks again!
Reply all
Reply to author
Forward
0 new messages