# Tag based searching

**URL:** https://discuss.elastic.co/t/tag-based-searching/10200
**Category:** Elasticsearch
**Created:** [December 29, 2012, 6:28am UTC](https://discuss.elastic.co/t/tag-based-searching/10200 "2012-12-29T06:28:41Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [December 29, 2012, 6:28am UTC](https://discuss.elastic.co/t/tag-based-searching/10200/1 "2012-12-29T06:28:41Z")

</div>

I am running a Rails application where I need to implement search  
functionality of users, using elasticsearch. There are many registered  
users on the website. User can submit his/her information like college,  
company, address, emails, etc. Now I need a common search box whereby,  
while entering the data, suggestions are shown based on the text  
written. Also I want to show the no. of users associated with a particular  
suggestion when it is shown (using facets). The suggestions can be anything

- email, college, company, etc. I am using elastictastic (  
[https://github.com/brewster/elastictastic](https://github.com/brewster/elastictastic)) to define mapping of data.

Since I want to use elasticsearch for implementing the above functionality,  
I came up with the following structure:

There will be user document which will contain the user\_id, and other user  
related relevant information. And there will be a tag document which will  
have fields as (tag\_type and tag\_value). tag\_type can be company, college,  
etc. while tag\_value should be the name. The id of the tag document will  
the generated from its tag\_value and tag\_type.

In such a scenario, user can be associated with many tags and also, tags  
can be associated to many users.

I don't know how to define the structure in elasticsearch to implement the  
above. Parent child relationship can't be applied here. I need to know how  
to define such kind of relationships in elasticsearch.

--  
Thanks,  
Aash

--

---

<div class="post-metadata">

### Author: ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)
#### Post date: [December 29, 2012, 10:36am UTC](https://discuss.elastic.co/t/tag-based-searching/10200/2 "2012-12-29T10:36:02Z")

</div>

faceted search is what you are looking for.  
Look into facets in -

> **[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.

On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya [aash.discover@gmail.com](mailto:aash.discover@gmail.com)wrote:

> There will be user document which will contain the user\_id, and other user  
> related relevant information. And there will be a tag document which will  
> have fields as (tag\_type and tag\_value). tag\_type can be company, college,  
> etc. while tag\_value should be the name. The id of the tag document will  
> the generated from its tag\_value and tag\_type.

--

---

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [December 29, 2012, 12:06pm UTC](https://discuss.elastic.co/t/tag-based-searching/10200/3 "2012-12-29T12:06:17Z")

</div>

Can you please give me an example whereby using the above structure, I can  
query elasticsearch and get relevant results? I am sorry for the  
inconvenience, but I am completely new to elasticsearch.  
Thanks a lot.

On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan [vineethmohan@algotree.com](mailto:vineethmohan@algotree.com)wrote:

> faceted search is what you are looking for.  
> Look into facets in -  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> 
> On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya [aash.discover@gmail.com](mailto:aash.discover@gmail.com)wrote:
> 
> > There will be user document which will contain the user\_id, and other  
> > user related relevant information. And there will be a tag document which  
> > will have fields as (tag\_type and tag\_value). tag\_type can be company,  
> > college, etc. while tag\_value should be the name. The id of the tag  
> > document will the generated from its tag\_value and tag\_type.
> 
> --

--  
Thanks,  
Aash

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [December 31, 2012, 1:45pm UTC](https://discuss.elastic.co/t/tag-based-searching/10200/4 "2012-12-31T13:45:58Z")

</div>

Why cannot you simplify things a bit and just add company, college,  
address, email, etc as user fields?

On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:

> Can you please give me an example whereby using the above structure, I can  
> query elasticsearch and get relevant results? I am sorry for the  
> inconvenience, but I am completely new to elasticsearch.  
> Thanks a lot.
> 
> On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan \<[vineet...@algotree.com](mailto:vineet...@algotree.com)\<javascript:\>
> 
> > wrote:
> 
> > faceted search is what you are looking for.  
> > Look into facets in -  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > 
> > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya \<[aash.d...@gmail.com](mailto:aash.d...@gmail.com)\<javascript:\>
> > 
> > > wrote:
> > 
> > > There will be user document which will contain the user\_id, and other  
> > > user related relevant information. And there will be a tag document which  
> > > will have fields as (tag\_type and tag\_value). tag\_type can be company,  
> > > college, etc. while tag\_value should be the name. The id of the tag  
> > > document will the generated from its tag\_value and tag\_type.
> > 
> > --
> 
> --  
> Thanks,  
> Aash

--

---

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [January 1, 2013, 8:41am UTC](https://discuss.elastic.co/t/tag-based-searching/10200/5 "2013-01-01T08:41:26Z")

</div>

I guess I could do that too.  
In the structure that I mentioned, the problem I am facing is that, while  
computing the facets against "tag\_value", the returned values only contain  
the facet count and its corresponding "tag\_value". In the above result, I  
also want the corresponding "tag\_type", the type of the tag(college,  
company, etc.) my result is associated with. How do I do that?

On Mon, Dec 31, 2012 at 7:15 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> Why cannot you simplify things a bit and just add company, college,  
> address, email, etc as user fields?
> 
> On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:
> 
> > Can you please give me an example whereby using the above structure, I  
> > can query elasticsearch and get relevant results? I am sorry for the  
> > inconvenience, but I am completely new to elasticsearch.  
> > Thanks a lot.
> > 
> > On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan [vineet...@algotree.com](mailto:vineet...@algotree.com)wrote:
> > 
> > > faceted search is what you are looking for.  
> > > Look into facets in - [http://www.elasticsearch.org/](http://www.elasticsearch.org/)\*\*  
> > > guide/reference/api/search/\*\*facets/index.html[http://www.elasticsearch.org/guide/reference/api/search/facets/index.html](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > 
> > > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya [aash.d...@gmail.com](mailto:aash.d...@gmail.com)wrote:
> > > 
> > > > There will be user document which will contain the user\_id, and other  
> > > > user related relevant information. And there will be a tag document which  
> > > > will have fields as (tag\_type and tag\_value). tag\_type can be company,  
> > > > college, etc. while tag\_value should be the name. The id of the tag  
> > > > document will the generated from its tag\_value and tag\_type.
> > > 
> > > --
> > 
> > --  
> > Thanks,  
> > Aash
> 
> --

--  
Thanks,  
Aash

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [January 1, 2013, 3:08pm UTC](https://discuss.elastic.co/t/tag-based-searching/10200/6 "2013-01-01T15:08:31Z")

</div>

If you will store values in designated fields, you can simply run facets on  
these fields. So, each field (company, college, ...) will have it's own  
facet.

On Tuesday, January 1, 2013 3:41:26 AM UTC-5, geeky\_sh wrote:

> I guess I could do that too.  
> In the structure that I mentioned, the problem I am facing is that, while  
> computing the facets against "tag\_value", the returned values only contain  
> the facet count and its corresponding "tag\_value". In the above result, I  
> also want the corresponding "tag\_type", the type of the tag(college,  
> company, etc.) my result is associated with. How do I do that?
> 
> On Mon, Dec 31, 2012 at 7:15 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Why cannot you simplify things a bit and just add company, college,  
> > address, email, etc as user fields?
> > 
> > On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:
> > 
> > > Can you please give me an example whereby using the above structure, I  
> > > can query elasticsearch and get relevant results? I am sorry for the  
> > > inconvenience, but I am completely new to elasticsearch.  
> > > Thanks a lot.
> > > 
> > > On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan [vineet...@algotree.com](mailto:vineet...@algotree.com)wrote:
> > > 
> > > > faceted search is what you are looking for.  
> > > > Look into facets in - [http://www.elasticsearch.org/](http://www.elasticsearch.org/)\*\*  
> > > > guide/reference/api/search/\*\*facets/index.html[http://www.elasticsearch.org/guide/reference/api/search/facets/index.html](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > > 
> > > > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya [aash.d...@gmail.com](mailto:aash.d...@gmail.com)wrote:
> > > > 
> > > > > There will be user document which will contain the user\_id, and other  
> > > > > user related relevant information. And there will be a tag document which  
> > > > > will have fields as (tag\_type and tag\_value). tag\_type can be company,  
> > > > > college, etc. while tag\_value should be the name. The id of the tag  
> > > > > document will the generated from its tag\_value and tag\_type.
> > > > 
> > > > --
> > > 
> > > --  
> > > Thanks,  
> > > Aash
> > 
> > --
> 
> --  
> Thanks,  
> Aash

--

---

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [January 1, 2013, 5:38pm UTC](https://discuss.elastic.co/t/tag-based-searching/10200/7 "2013-01-01T17:38:48Z")

</div>

Yeah, that makes sense. I will try it soon and see if it meets my needs.  
Thanks a lot!

On Tue, Jan 1, 2013 at 8:38 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> If you will store values in designated fields, you can simply run facets  
> on these fields. So, each field (company, college, ...) will have it's own  
> facet.
> 
> On Tuesday, January 1, 2013 3:41:26 AM UTC-5, geeky\_sh wrote:
> 
> > I guess I could do that too.  
> > In the structure that I mentioned, the problem I am facing is that, while  
> > computing the facets against "tag\_value", the returned values only contain  
> > the facet count and its corresponding "tag\_value". In the above result, I  
> > also want the corresponding "tag\_type", the type of the tag(college,  
> > company, etc.) my result is associated with. How do I do that?
> > 
> > On Mon, Dec 31, 2012 at 7:15 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > 
> > > Why cannot you simplify things a bit and just add company, college,  
> > > address, email, etc as user fields?
> > > 
> > > On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:
> > > 
> > > > Can you please give me an example whereby using the above structure, I  
> > > > can query elasticsearch and get relevant results? I am sorry for the  
> > > > inconvenience, but I am completely new to elasticsearch.  
> > > > Thanks a lot.
> > > > 
> > > > On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan [vineet...@algotree.com](mailto:vineet...@algotree.com)wrote:
> > > > 
> > > > > faceted search is what you are looking for.  
> > > > > Look into facets in - [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**g)\*\*  
> > > > > uide/reference/api/search/ **face** ts/index.html[http://www.elasticsearch.org/guide/reference/api/search/facets/index.html](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > > > 
> > > > > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya [aash.d...@gmail.com](mailto:aash.d...@gmail.com)wrote:
> > > > > 
> > > > > > There will be user document which will contain the user\_id, and other  
> > > > > > user related relevant information. And there will be a tag document which  
> > > > > > will have fields as (tag\_type and tag\_value). tag\_type can be company,  
> > > > > > college, etc. while tag\_value should be the name. The id of the tag  
> > > > > > document will the generated from its tag\_value and tag\_type.
> > > > > 
> > > > > --
> > > > 
> > > > --  
> > > > Thanks,  
> > > > Aash
> > > 
> > > --
> > 
> > --  
> > Thanks,  
> > Aash
> 
> --

--  
Thanks,  
Aash

--

---

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [January 2, 2013, 7:52am UTC](https://discuss.elastic.co/t/tag-based-searching/10200/8 "2013-01-02T07:52:35Z")

</div>

Hi,  
The solution suggested above works fine. I have only one question. Since, I  
am using global suggestion, I want the results to be sorted out on basis of  
its relevance. As I am using multiple terms in the facets to get the  
results, is there a way through which I can globally sort the results of  
all the facet\_terms according to its relevance and get one global results  
array?

On Tue, Jan 1, 2013 at 11:08 PM, aash dhariya [aash.discover@gmail.com](mailto:aash.discover@gmail.com)wrote:

> Yeah, that makes sense. I will try it soon and see if it meets my needs.  
> Thanks a lot!
> 
> On Tue, Jan 1, 2013 at 8:38 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:
> 
> > If you will store values in designated fields, you can simply run facets  
> > on these fields. So, each field (company, college, ...) will have it's own  
> > facet.
> > 
> > On Tuesday, January 1, 2013 3:41:26 AM UTC-5, geeky\_sh wrote:
> > 
> > > I guess I could do that too.  
> > > In the structure that I mentioned, the problem I am facing is that,  
> > > while computing the facets against "tag\_value", the returned values only  
> > > contain the facet count and its corresponding "tag\_value". In the above  
> > > result, I also want the corresponding "tag\_type", the type of the  
> > > tag(college, company, etc.) my result is associated with. How do I do that?
> > > 
> > > On Mon, Dec 31, 2012 at 7:15 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > 
> > > > Why cannot you simplify things a bit and just add company, college,  
> > > > address, email, etc as user fields?
> > > > 
> > > > On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:
> > > > 
> > > > > Can you please give me an example whereby using the above structure, I  
> > > > > can query elasticsearch and get relevant results? I am sorry for the  
> > > > > inconvenience, but I am completely new to elasticsearch.  
> > > > > Thanks a lot.
> > > > > 
> > > > > On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan \<[vineet...@algotree.com](mailto:vineet...@algotree.com)
> > > > > 
> > > > > > wrote:
> > > > > 
> > > > > > faceted search is what you are looking for.  
> > > > > > Look into facets in - [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**g)\*\*  
> > > > > > uide/reference/api/search/ **face** ts/index.html[http://www.elasticsearch.org/guide/reference/api/search/facets/index.html](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > > > > 
> > > > > > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya [aash.d...@gmail.com](mailto:aash.d...@gmail.com)wrote:
> > > > > > 
> > > > > > > There will be user document which will contain the user\_id, and  
> > > > > > > other user related relevant information. And there will be a tag document  
> > > > > > > which will have fields as (tag\_type and tag\_value). tag\_type can be  
> > > > > > > company, college, etc. while tag\_value should be the name. The id of the  
> > > > > > > tag document will the generated from its tag\_value and tag\_type.
> > > > > > 
> > > > > > --
> > > > > 
> > > > > --  
> > > > > Thanks,  
> > > > > Aash
> > > > 
> > > > --
> > > 
> > > --  
> > > Thanks,  
> > > Aash
> > 
> > --
> 
> --  
> Thanks,  
> Aash

--  
Thanks,  
Aash

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [January 2, 2013, 12:43pm UTC](https://discuss.elastic.co/t/tag-based-searching/10200/9 "2013-01-02T12:43:03Z")

</div>

Facets cannot be sorted by relevance. They can be sorted by the number of  
records they show up in. Assuming that this is what you meant by relevance,  
there are a few ways to create a single combined list of facets. First of  
all, you can combine facets together on the client, just merge all facets  
lists together and sort them by count. You can also run facets against  
multiple fields (See Multi Field section on the Terms Facet[http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)page), but this is, probably, not what you want since you will not know  
which field each value came from. Alternatively, you can go back to your  
original setup and create a facet on a script field that would return  
something like (tag\_type + "/" + tag\_value), but it might be quite slow. A  
faster variation of this solution would be to store (tag\_type + "/" +  
tag\_value) as a special not-analyzed field and run facets on it, but  
because this field can potentially have very large number of values per  
record, this solution can require a lot of memory.

On Wednesday, January 2, 2013 2:52:35 AM UTC-5, geeky\_sh wrote:

> Hi,  
> The solution suggested above works fine. I have only one question. Since,  
> I am using global suggestion, I want the results to be sorted out on basis  
> of its relevance. As I am using multiple terms in the facets to get the  
> results, is there a way through which I can globally sort the results of  
> all the facet\_terms according to its relevance and get one global results  
> array?
> 
> On Tue, Jan 1, 2013 at 11:08 PM, aash dhariya \<[aash.d...@gmail.com](mailto:aash.d...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Yeah, that makes sense. I will try it soon and see if it meets my needs.  
> > Thanks a lot!
> > 
> > On Tue, Jan 1, 2013 at 8:38 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com)\<javascript:\>
> > 
> > > wrote:
> > 
> > > If you will store values in designated fields, you can simply run facets  
> > > on these fields. So, each field (company, college, ...) will have it's own  
> > > facet.
> > > 
> > > On Tuesday, January 1, 2013 3:41:26 AM UTC-5, geeky\_sh wrote:
> > > 
> > > > I guess I could do that too.  
> > > > In the structure that I mentioned, the problem I am facing is that,  
> > > > while computing the facets against "tag\_value", the returned values only  
> > > > contain the facet count and its corresponding "tag\_value". In the above  
> > > > result, I also want the corresponding "tag\_type", the type of the  
> > > > tag(college, company, etc.) my result is associated with. How do I do that?
> > > > 
> > > > On Mon, Dec 31, 2012 at 7:15 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > > 
> > > > > Why cannot you simplify things a bit and just add company, college,  
> > > > > address, email, etc as user fields?
> > > > > 
> > > > > On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:
> > > > > 
> > > > > > Can you please give me an example whereby using the above structure,  
> > > > > > I can query elasticsearch and get relevant results? I am sorry for the  
> > > > > > inconvenience, but I am completely new to elasticsearch.  
> > > > > > Thanks a lot.
> > > > > > 
> > > > > > On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan \<  
> > > > > > [vineet...@algotree.com](mailto:vineet...@algotree.com)\> wrote:
> > > > > > 
> > > > > > > faceted search is what you are looking for.  
> > > > > > > Look into facets in - [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**g)\*\*  
> > > > > > > uide/reference/api/search/ **face** ts/index.html[http://www.elasticsearch.org/guide/reference/api/search/facets/index.html](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > > > > > 
> > > > > > > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya [aash.d...@gmail.com](mailto:aash.d...@gmail.com)wrote:
> > > > > > > 
> > > > > > > > There will be user document which will contain the user\_id, and  
> > > > > > > > other user related relevant information. And there will be a tag document  
> > > > > > > > which will have fields as (tag\_type and tag\_value). tag\_type can be  
> > > > > > > > company, college, etc. while tag\_value should be the name. The id of the  
> > > > > > > > tag document will the generated from its tag\_value and tag\_type.
> > > > > > > 
> > > > > > > --
> > > > > > 
> > > > > > --  
> > > > > > Thanks,  
> > > > > > Aash
> > > > > 
> > > > > --
> > > > 
> > > > --  
> > > > Thanks,  
> > > > Aash
> > > 
> > > --
> > 
> > --  
> > Thanks,  
> > Aash
> 
> --  
> Thanks,  
> Aash

--

---

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [January 3, 2013, 12:17pm UTC](https://discuss.elastic.co/t/tag-based-searching/10200/10 "2013-01-03T12:17:34Z")

</div>

Thanks a lot for your reply. I will try it out.

On Wed, Jan 2, 2013 at 6:13 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> Facets cannot be sorted by relevance. They can be sorted by the number of  
> records they show up in. Assuming that this is what you meant by relevance,  
> there are a few ways to create a single combined list of facets. First of  
> all, you can combine facets together on the client, just merge all facets  
> lists together and sort them by count. You can also run facets against  
> multiple fields (See Multi Field section on the Terms Facet[http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)page), but this is, probably, not what you want since you will not know  
> which field each value came from. Alternatively, you can go back to your  
> original setup and create a facet on a script field that would return  
> something like (tag\_type + "/" + tag\_value), but it might be quite slow. A  
> faster variation of this solution would be to store (tag\_type + "/" +  
> tag\_value) as a special not-analyzed field and run facets on it, but  
> because this field can potentially have very large number of values per  
> record, this solution can require a lot of memory.
> 
> On Wednesday, January 2, 2013 2:52:35 AM UTC-5, geeky\_sh wrote:
> 
> > Hi,  
> > The solution suggested above works fine. I have only one question. Since,  
> > I am using global suggestion, I want the results to be sorted out on basis  
> > of its relevance. As I am using multiple terms in the facets to get the  
> > results, is there a way through which I can globally sort the results of  
> > all the facet\_terms according to its relevance and get one global results  
> > array?
> > 
> > On Tue, Jan 1, 2013 at 11:08 PM, aash dhariya [aash.d...@gmail.com](mailto:aash.d...@gmail.com)wrote:
> > 
> > > Yeah, that makes sense. I will try it soon and see if it meets my needs.  
> > > Thanks a lot!
> > > 
> > > On Tue, Jan 1, 2013 at 8:38 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > 
> > > > If you will store values in designated fields, you can simply run  
> > > > facets on these fields. So, each field (company, college, ...) will have  
> > > > it's own facet.
> > > > 
> > > > On Tuesday, January 1, 2013 3:41:26 AM UTC-5, geeky\_sh wrote:
> > > > 
> > > > > I guess I could do that too.  
> > > > > In the structure that I mentioned, the problem I am facing is that,  
> > > > > while computing the facets against "tag\_value", the returned values only  
> > > > > contain the facet count and its corresponding "tag\_value". In the above  
> > > > > result, I also want the corresponding "tag\_type", the type of the  
> > > > > tag(college, company, etc.) my result is associated with. How do I do that?
> > > > > 
> > > > > On Mon, Dec 31, 2012 at 7:15 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > > > 
> > > > > > Why cannot you simplify things a bit and just add company, college,  
> > > > > > address, email, etc as user fields?
> > > > > > 
> > > > > > On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:
> > > > > > 
> > > > > > > Can you please give me an example whereby using the above structure,  
> > > > > > > I can query elasticsearch and get relevant results? I am sorry for the  
> > > > > > > inconvenience, but I am completely new to elasticsearch.  
> > > > > > > Thanks a lot.
> > > > > > > 
> > > > > > > On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan \<  
> > > > > > > [vineet...@algotree.com](mailto:vineet...@algotree.com)\> wrote:
> > > > > > > 
> > > > > > > > faceted search is what you are looking for.  
> > > > > > > > Look into facets in - [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**g)\*\*\*\*  
> > > > > > > > uide/reference/api/search/\*\*face\*\*\*\*ts/index.html[http://www.elasticsearch.org/guide/reference/api/search/facets/index.html](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > > > > > > 
> > > > > > > > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya \<[aash.d...@gmail.com](mailto:aash.d...@gmail.com)
> > > > > > > > 
> > > > > > > > > wrote:
> > > > > > > > 
> > > > > > > > > There will be user document which will contain the user\_id, and  
> > > > > > > > > other user related relevant information. And there will be a tag document  
> > > > > > > > > which will have fields as (tag\_type and tag\_value). tag\_type can be  
> > > > > > > > > company, college, etc. while tag\_value should be the name. The id of the  
> > > > > > > > > tag document will the generated from its tag\_value and tag\_type.
> > > > > > > > 
> > > > > > > > --
> > > > > > > 
> > > > > > > --  
> > > > > > > Thanks,  
> > > > > > > Aash
> > > > > > 
> > > > > > --
> > > > > 
> > > > > --  
> > > > > Thanks,  
> > > > > Aash
> > > > 
> > > > --
> > > 
> > > --  
> > > Thanks,  
> > > Aash
> > 
> > --  
> > Thanks,  
> > Aash
> 
> --

--  
Thanks,  
Aash

--

---

<div class="post-metadata">

### Author: ![aash\_dhariya](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aash\_dhariya](https://discuss.elastic.co/u/aash_dhariya)
#### Post date: [January 4, 2013, 5:53am UTC](https://discuss.elastic.co/t/tag-based-searching/10200/11 "2013-01-04T05:53:09Z")

</div>

Well, relevance based search is important in my problem. Therefore, I have  
decided to move forward with the following structure:

There will be a user document which will contain tags as nested documents.  
Next, I will have the same tag documents indexed separately.

To trigger search when the user types the input, I will first search in the  
user documents based on the tag\_value(using facets). In return I will get  
the matching tag\_ids. Next, I will search the tag documents (which are  
separately indexed) and get the corresponding tag\_type and tag\_value. In  
the latter search, I will search against the user-typed input again, so  
that I will get the output sorted by relevance.

In total, to get the final results, I need to make two searches in the ES  
documents

On Thu, Jan 3, 2013 at 5:47 PM, aash dhariya [aash.discover@gmail.com](mailto:aash.discover@gmail.com)wrote:

> Thanks a lot for your reply. I will try it out.
> 
> On Wed, Jan 2, 2013 at 6:13 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:
> 
> > Facets cannot be sorted by relevance. They can be sorted by the number of  
> > records they show up in. Assuming that this is what you meant by relevance,  
> > there are a few ways to create a single combined list of facets. First of  
> > all, you can combine facets together on the client, just merge all facets  
> > lists together and sort them by count. You can also run facets against  
> > multiple fields (See Multi Field section on the Terms Facet[http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)page), but this is, probably, not what you want since you will not know  
> > which field each value came from. Alternatively, you can go back to your  
> > original setup and create a facet on a script field that would return  
> > something like (tag\_type + "/" + tag\_value), but it might be quite slow. A  
> > faster variation of this solution would be to store (tag\_type + "/" +  
> > tag\_value) as a special not-analyzed field and run facets on it, but  
> > because this field can potentially have very large number of values per  
> > record, this solution can require a lot of memory.
> > 
> > On Wednesday, January 2, 2013 2:52:35 AM UTC-5, geeky\_sh wrote:
> > 
> > > Hi,  
> > > The solution suggested above works fine. I have only one question.  
> > > Since, I am using global suggestion, I want the results to be sorted out on  
> > > basis of its relevance. As I am using multiple terms in the facets to get  
> > > the results, is there a way through which I can globally sort the results  
> > > of all the facet\_terms according to its relevance and get one global  
> > > results array?
> > > 
> > > On Tue, Jan 1, 2013 at 11:08 PM, aash dhariya [aash.d...@gmail.com](mailto:aash.d...@gmail.com)wrote:
> > > 
> > > > Yeah, that makes sense. I will try it soon and see if it meets my  
> > > > needs. Thanks a lot!
> > > > 
> > > > On Tue, Jan 1, 2013 at 8:38 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > > 
> > > > > If you will store values in designated fields, you can simply run  
> > > > > facets on these fields. So, each field (company, college, ...) will have  
> > > > > it's own facet.
> > > > > 
> > > > > On Tuesday, January 1, 2013 3:41:26 AM UTC-5, geeky\_sh wrote:
> > > > > 
> > > > > > I guess I could do that too.  
> > > > > > In the structure that I mentioned, the problem I am facing is that,  
> > > > > > while computing the facets against "tag\_value", the returned values only  
> > > > > > contain the facet count and its corresponding "tag\_value". In the above  
> > > > > > result, I also want the corresponding "tag\_type", the type of the  
> > > > > > tag(college, company, etc.) my result is associated with. How do I do that?
> > > > > > 
> > > > > > On Mon, Dec 31, 2012 at 7:15 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > > > > 
> > > > > > > Why cannot you simplify things a bit and just add company, college,  
> > > > > > > address, email, etc as user fields?
> > > > > > > 
> > > > > > > On Saturday, December 29, 2012 7:06:17 AM UTC-5, geeky\_sh wrote:
> > > > > > > 
> > > > > > > > Can you please give me an example whereby using the above  
> > > > > > > > structure, I can query elasticsearch and get relevant results? I am sorry  
> > > > > > > > for the inconvenience, but I am completely new to elasticsearch.  
> > > > > > > > Thanks a lot.
> > > > > > > > 
> > > > > > > > On Sat, Dec 29, 2012 at 4:06 PM, Vineeth Mohan \<  
> > > > > > > > [vineet...@algotree.com](mailto:vineet...@algotree.com)\> wrote:
> > > > > > > > 
> > > > > > > > > faceted search is what you are looking for.  
> > > > > > > > > Look into facets in - [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**g)\*\*\*\*  
> > > > > > > > > uide/reference/api/search/\*\*face\*\*\*\*ts/index.html[http://www.elasticsearch.org/guide/reference/api/search/facets/index.html](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > > > > > > > 
> > > > > > > > > On Sat, Dec 29, 2012 at 11:58 AM, aash dhariya \<  
> > > > > > > > > [aash.d...@gmail.com](mailto:aash.d...@gmail.com)\> wrote:
> > > > > > > > > 
> > > > > > > > > > There will be user document which will contain the user\_id, and  
> > > > > > > > > > other user related relevant information. And there will be a tag document  
> > > > > > > > > > which will have fields as (tag\_type and tag\_value). tag\_type can be  
> > > > > > > > > > company, college, etc. while tag\_value should be the name. The id of the  
> > > > > > > > > > tag document will the generated from its tag\_value and tag\_type.
> > > > > > > > > 
> > > > > > > > > --
> > > > > > > > 
> > > > > > > > --  
> > > > > > > > Thanks,  
> > > > > > > > Aash
> > > > > > > 
> > > > > > > --
> > > > > > 
> > > > > > --  
> > > > > > Thanks,  
> > > > > > Aash
> > > > > 
> > > > > --
> > > > 
> > > > --  
> > > > Thanks,  
> > > > Aash
> > > 
> > > --  
> > > Thanks,  
> > > Aash
> > 
> > --
> 
> --  
> Thanks,  
> Aash

--  
Thanks,  
Aash

--

---

<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, 2:57am UTC](https://discuss.elastic.co/t/tag-based-searching/10200/12 "2017-07-06T02:57:52Z")

</div>


