# Call for ideas

**URL:** <https://discuss.elastic.co/t/call-for-ideas/7825>\
**Category:** Elasticsearch\
**Created:** [May 23, 2012, 4:44pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825 "2012-05-23T16:44:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Vaucher](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@Philippe\_Vaucher](https://discuss.elastic.co/u/Philippe_Vaucher)\
**Post date:** [May 23, 2012, 4:44pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825/1 "2012-05-23T16:44:02Z")

</div>

Hello,

I'm currently using elasticsearch for an intranet which main goal is to  
manage users and invoices.  
I created an index for the users with their invoices as an array of objects  
containing the invoices informations.

I'm able to search for users just fine, but I wonder how I am supposed to  
search in the invoices... for example, if I want to search for all invoices  
with values between 0 and 500 and which user name is "frank" (lucene  
search):

"name:frank AND invoices.value:[0 TO 500]"

This "works" fine in the sense that it returns all users named frank that  
have at least one invoice with value between 0 and 500, but in what is  
returned it also return the other invoices that are over 500 for those  
users.

Is there a way to make it filter the list of invoices so only the ones  
matching are returned?  
Or maybe I should create two indexes, one for the users and one for the  
invoices? But doens't that duplicate a lot of data?  
Or maybe there is a way to represent/query relational data in elasticsearch  
by indexing it differently?

Thanks,  
Philippe

---

<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:** [May 23, 2012, 4:58pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825/2 "2012-05-23T16:58:22Z")

</div>

Yes. Duplicate data.  
If you are searching invoice, you should index invoices (as a type, not as an index)  
In your invoice, put all what you know about the user and query on it.

My 2 cents.

David 😉  
Twitter : @dadoonet / @elasticsearchfr

Le 23 mai 2012 à 18:44, Philippe Vaucher [philippe.vaucher@gmail.com](mailto:philippe.vaucher@gmail.com) a écrit :

> Hello,
> 
> I'm currently using elasticsearch for an intranet which main goal is to manage users and invoices.  
> I created an index for the users with their invoices as an array of objects containing the invoices informations.
> 
> I'm able to search for users just fine, but I wonder how I am supposed to search in the invoices... for example, if I want to search for all invoices with values between 0 and 500 and which user name is "frank" (lucene search):
> 
> "name:frank AND invoices.value:[0 TO 500]"
> 
> This "works" fine in the sense that it returns all users named frank that have at least one invoice with value between 0 and 500, but in what is returned it also return the other invoices that are over 500 for those users.
> 
> Is there a way to make it filter the list of invoices so only the ones matching are returned?  
> Or maybe I should create two indexes, one for the users and one for the invoices? But doens't that duplicate a lot of data?  
> Or maybe there is a way to represent/query relational data in elasticsearch by indexing it differently?
> 
> Thanks,  
> Philippe

---

<div class="post-metadata">

**Author:** ![Philippe\_Vaucher](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@Philippe\_Vaucher](https://discuss.elastic.co/u/Philippe_Vaucher)\
**Post date:** [May 24, 2012, 1:31pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825/3 "2012-05-24T13:31:54Z")

</div>

> Yes. Duplicate data.  
> If you are searching invoice, you should index invoices (as a type, not as  
> an index)  
> In your invoice, put all what you know about the user and query on it.

Well sometimes I'll be searching Users, sometimes I'll be searching  
Invoices. I think creating two indexes makes more sense, especially if I  
want to list each invoices properties.

Anyway, so be it! Thank you for confirming that elasticsearch relations are  
one-way only and that its model is really a tree, and thus you need to  
create multiple indexes.

Philippe

---

<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:** [May 24, 2012, 2:07pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825/4 "2012-05-24T14:07:40Z")

</div>

Hi Philippe,

Just a few things I would like to add.

I did not say that you can not create relations. You can do it with  
parent/child and nested concepts but I really prefer to avoid that  
complexity if it's only to save some disk space...

I understood that you want also to manage your users.

So you can have an index named : myindex with two types : invoice and user  
You could also have one index invoice with a type invoice and one index  
user with a type user (this is what you are talking about I think).

I prefer the first option as I will have to manage my index settings only  
once, but it really depends on your use case. If you want to set a replica  
factor different for each type, you will have to separate them in many  
indices.

Cheers  
David.

Le 24 mai 2012 à 15:31, Philippe Vaucher [philippe.vaucher@gmail.com](mailto:philippe.vaucher@gmail.com) a  
écrit :

> > If you are searching invoice, you should index invoices (as a type, not as  
> > an index)  
> > In your invoice, put all what you know about the user and query on it.
> 
> Well sometimes I'll be searching Users, sometimes I'll be searching  
> Invoices. I think creating two indexes makes more sense, especially if I  
> want to list each invoices properties.
> 
> Anyway, so be it! Thank you for confirming that elasticsearch relations are  
> one-way only and that its model is really a tree, and thus you need to  
> create multiple indexes.
> 
> Philippe

--  
David Pilato  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet

---

<div class="post-metadata">

**Author:** ![jschelle\_2](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jschelle\_2](https://discuss.elastic.co/u/jschelle_2)\
**Post date:** [May 24, 2012, 2:10pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825/5 "2012-05-24T14:10:36Z")

</div>

Your could also use a nested query if your invoices are objects inside users

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

You could also have user be a type and invoice be a type and simply have  
the invoice type contain a field like userId. This way only the userId is  
duplicated and not all the data.

On Thursday, May 24, 2012 9:31:54 AM UTC-4, Philippe Vaucher wrote:

> Yes. Duplicate data.
> 
> > If you are searching invoice, you should index invoices (as a type, not  
> > as an index)  
> > In your invoice, put all what you know about the user and query on it.
> 
> Well sometimes I'll be searching Users, sometimes I'll be searching  
> Invoices. I think creating two indexes makes more sense, especially if I  
> want to list each invoices properties.
> 
> Anyway, so be it! Thank you for confirming that elasticsearch relations  
> are one-way only and that its model is really a tree, and thus you need to  
> create multiple indexes.
> 
> Philippe

---

<div class="post-metadata">

**Author:** ![Philippe\_Vaucher](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@Philippe\_Vaucher](https://discuss.elastic.co/u/Philippe_Vaucher)\
**Post date:** [May 24, 2012, 3:26pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825/6 "2012-05-24T15:26:53Z")

</div>

> I did not say that you can not create relations. You can do it with  
> parent/child and nested concepts but I really prefer to avoid that  
> complexity if it's only to save some disk space...

I see, so I have two solutions:

1. Create a user type which has all the invoices as properties, and  
create an invoice type that have its user information as properties. This  
duplicates data but then things are simple, if I want user I search in the  
users and if I want invoices I search in the invoices.
2. Create one user type that has all the invoices as _nested_  
properties, then use a special search to tell elasticsearch to search and  
return me invoices instead of users. This doesn't duplicate data but it's  
more complicated to set up.

> So you can have an index named : myindex with two types : invoice and user
> 
> You could also have one index invoice with a type invoice and one index  
> user with a type user (this is what you are talking about I think).

Yes I realised that I confused index and type as in my setup they're named  
the same 🙂

Thanks!  
Philippe

>

---

<div class="post-metadata">

**Author:** ![Philippe\_Vaucher](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@Philippe\_Vaucher](https://discuss.elastic.co/u/Philippe_Vaucher)\
**Post date:** [May 24, 2012, 3:29pm UTC](https://discuss.elastic.co/t/call-for-ideas/7825/7 "2012-05-24T15:29:23Z")

</div>

> Your could also use a nested query if your invoices are objects inside  
> users  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html)

Thanks for this interesting alternative.

You could also have user be a type and invoice be a type and simply have

> the invoice type contain a field like userId. This way only the userId is  
> duplicated and not all the data.

Hum, good idea, tho it requires a bit more work than simply duplicating the  
data because I'd have to load the results from the database instead of just  
using the elasticsearch results.

Philippe

---

<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:27am UTC](https://discuss.elastic.co/t/call-for-ideas/7825/8 "2017-07-06T03:27:15Z")

</div>


