# Simplifying search

**URL:** <https://discuss.elastic.co/t/simplifying-search/5290>\
**Category:** Elasticsearch\
**Created:** [August 31, 2011, 8:18pm UTC](https://discuss.elastic.co/t/simplifying-search/5290 "2011-08-31T20:18:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anna\_2](https://avatars.discourse-cdn.com/v4/letter/a/c77e96/32.png) [@Anna\_2](https://discuss.elastic.co/u/Anna_2)\
**Post date:** [August 31, 2011, 8:18pm UTC](https://discuss.elastic.co/t/simplifying-search/5290/1 "2011-08-31T20:18:55Z")

</div>

Hello,

I am trying to figure out, if there a way to reduce a number of  
queries in my search.  
I use data model like the following:

Person {  
id: ,  
name:   
}

Location {  
id: ,  
name:   
}

Relation {  
personId: ,  
locationId: ,  
type:   
}

Persons and Locations are tied with Relations as many-to-many.  
Relations can be of different types (e.g. "home", "work").  
The documents are indexed into the same index with different types:  
Person, Location, Relation.

My search should find all Persons which are related to a Location with  
Location.name = "someLocation",  
and Relation.type = "someType".

I can do that by running a number of queries sequentially:

1. find Location.id by Location.name (termFilter, filteredQuery);
2. find all matching Relation.personId by Relation.type and  
Relation.locationId (andFilter, filteredQuery);
3. find all matching Persons by Person.id (termFilter, filteredQuery).

Is it possible to simplify this search somehow, is there a way to do  
it in one query?

Thanks,  
Anna

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 31, 2011, 9:23pm UTC](https://discuss.elastic.co/t/simplifying-search/5290/2 "2011-08-31T21:23:34Z")

</div>

Looks like you are trying to apply sql like concepts in a NoSql world.

Not sure I'm right, but I will store Person content and Location content directly in the Relation document. Such as :

> Relation {  
> Person {
> 
> > name:   
> > },  
> > ,Location {  
> > name:   
> > }  
> > type:   
> > }

Then, searches are quite simples to implement.

Make sense ?

HTH  
David 😉

Le 31 août 2011 à 22:18, Anna [anna.grigoryeva@googlemail.com](mailto:anna.grigoryeva@googlemail.com) a écrit :

> Hello,
> 
> I am trying to figure out, if there a way to reduce a number of  
> queries in my search.  
> I use data model like the following:
> 
> Person {  
> id: ,  
> name:   
> }
> 
> Location {  
> id: ,  
> name:   
> }
> 
> Relation {  
> personId: ,  
> locationId: ,  
> type:   
> }
> 
> Persons and Locations are tied with Relations as many-to-many.  
> Relations can be of different types (e.g. "home", "work").  
> The documents are indexed into the same index with different types:  
> Person, Location, Relation.
> 
> My search should find all Persons which are related to a Location with  
> Location.name = "someLocation",  
> and Relation.type = "someType".
> 
> I can do that by running a number of queries sequentially:
> 
> 1. find Location.id by Location.name (termFilter, filteredQuery);
> 2. find all matching Relation.personId by Relation.type and  
> Relation.locationId (andFilter, filteredQuery);
> 3. find all matching Persons by Person.id (termFilter, filteredQuery).
> 
> Is it possible to simplify this search somehow, is there a way to do  
> it in one query?
> 
> Thanks,  
> Anna

---

<div class="post-metadata">

**Author:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [August 31, 2011, 9:25pm UTC](https://discuss.elastic.co/t/simplifying-search/5290/3 "2011-08-31T21:25:35Z")

</div>

This seems like you are trying to use the wrong tool for the job. I assume  
this data is originating in a database, why not just use the database to get  
the information you want? You are not doing anything that requires a search  
engine.

If you still want to try and do this with ES, I would use a different  
indexing strategy... maybe index people with their relationships to  
locations as a nested object:

person: {  
name,  
relation: [  
{locationName, type},  
{locationName, type},  
{locationName, type}  
]  
}

Once you index docs like this you can use nested queries to find all people  
with a certain location and type. Good luck.

Thanks,  
Matt Weber

On Wed, Aug 31, 2011 at 1:18 PM, Anna [anna.grigoryeva@googlemail.com](mailto:anna.grigoryeva@googlemail.com)wrote:

> Hello,
> 
> I am trying to figure out, if there a way to reduce a number of  
> queries in my search.  
> I use data model like the following:
> 
> Person {  
> id: ,  
> name:   
> }
> 
> Location {  
> id: ,  
> name:   
> }
> 
> Relation {  
> personId: ,  
> locationId: ,  
> type:   
> }
> 
> Persons and Locations are tied with Relations as many-to-many.  
> Relations can be of different types (e.g. "home", "work").  
> The documents are indexed into the same index with different types:  
> Person, Location, Relation.
> 
> My search should find all Persons which are related to a Location with  
> Location.name = "someLocation",  
> and Relation.type = "someType".
> 
> I can do that by running a number of queries sequentially:
> 
> 1. find Location.id by Location.name (termFilter, filteredQuery);
> 2. find all matching Relation.personId by Relation.type and  
> Relation.locationId (andFilter, filteredQuery);
> 3. find all matching Persons by Person.id (termFilter, filteredQuery).
> 
> Is it possible to simplify this search somehow, is there a way to do  
> it in one query?
> 
> Thanks,  
> Anna

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [August 31, 2011, 9:26pm UTC](https://discuss.elastic.co/t/simplifying-search/5290/4 "2011-08-31T21:26:02Z")

</div>

what do you want to achieve? every search index is built with the  
domain in mind and so it is very specific. It is often not as generic  
as a relational DB, so you probably have to do some de-normalization  
(e.g. put all the persons into a location or put the location name and  
the person name into the relation and have only relations in the  
index ...)

On 31 Aug., 22:18, Anna [anna.grigory...@googlemail.com](mailto:anna.grigory...@googlemail.com) wrote:

> Hello,
> 
> I am trying to figure out, if there a way to reduce a number of  
> queries in my search.  
> I use data model like the following:
> 
> Person {  
> id: ,  
> name:
> 
> }
> 
> Location {  
> id: ,  
> name:
> 
> }
> 
> Relation {  
> personId: ,  
> locationId: ,  
> type:
> 
> }
> 
> Persons and Locations are tied with Relations as many-to-many.  
> Relations can be of different types (e.g. "home", "work").  
> The documents are indexed into the same index with different types:  
> Person, Location, Relation.
> 
> My search should find all Persons which are related to a Location with  
> Location.name = "someLocation",  
> and Relation.type = "someType".
> 
> I can do that by running a number of queries sequentially:
> 
> 1. find Location.id by Location.name (termFilter, filteredQuery);
> 2. find all matching Relation.personId by Relation.type and  
> Relation.locationId (andFilter, filteredQuery);
> 3. find all matching Persons by Person.id (termFilter, filteredQuery).
> 
> Is it possible to simplify this search somehow, is there a way to do  
> it in one query?
> 
> Thanks,  
> Anna

---

<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, 3:55am UTC](https://discuss.elastic.co/t/simplifying-search/5290/5 "2017-07-06T03:55:24Z")

</div>


