# Is there a way to give a unique ID to a field?

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268>\
**Category:** Elasticsearch\
**Created:** [August 28, 2011, 3:16pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268 "2011-08-28T15:16:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogregras](https://avatars.discourse-cdn.com/v4/letter/o/cdc98d/32.png) [@ogregras](https://discuss.elastic.co/u/ogregras)\
**Post date:** [August 28, 2011, 3:16pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268/1 "2011-08-28T15:16:10Z")

</div>

I'm currently working with facets and I wish my application URL to keep  
information about the facets that have been selected by the user so far.

For example:

[http://www.example.com/widgets/](http://www.example.com/widgets/)_manufacturer-bang\_olufse_/_color-blue_

Each selected facet is added to the URL and a resulting search, for all  
facets in the URL, is done.

But let's say the manufacturer in this example is indexed as "Bang&Olufse",  
I want to to be able to find it using the friendly token saved in the URL.  
If I search using my current friendly token,"bang\_olufse", I won't find  
anything.

I'd like a generic way to target a specific field using a unique id/token. I  
don't simply want the search to be case insensitive, I'd like a way to  
create a real custom friendly URL token ("bang\_olufse" for example, or even  
"b\_o") and have a way to find the right field using it. Is there a way to do  
that?

I though about generating a unique string like "\_\_faceturl-bang\_olufse" and  
append it _to the content of the field itself_, so a following search  
for "\_\_faceturl-[FRIENDLY\_URL\_TOKEN]" would find it. But this is really ugly  
and force me to deal with the fact that the content contains this id that I  
don't want to display.

Any suggestions on how to map a friendly URL token to a specific field?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ogregras](https://avatars.discourse-cdn.com/v4/letter/o/cdc98d/32.png) [@ogregras](https://discuss.elastic.co/u/ogregras)\
**Post date:** [August 28, 2011, 4:06pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268/2 "2011-08-28T16:06:30Z")

</div>

I guess my question was stupid: I simply have to add a  
"manufacturer\_friendly" field when indexing the record, and search on this  
field using the friendly tokens from the URL!

---

<div class="post-metadata">

**Author:** ![ogregras](https://avatars.discourse-cdn.com/v4/letter/o/cdc98d/32.png) [@ogregras](https://discuss.elastic.co/u/ogregras)\
**Post date:** [August 28, 2011, 9:12pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268/3 "2011-08-28T21:12:20Z")

</div>

I'm not too sure about my "solution" after all...

If I create a field dedicated to the "friendly" value, I have to get both  
this field and the field containing the original value from a facet search :  
the friendly value to create the URL token and the original value to display  
the facet value to the user. The problem is, by setting both of those fields  
to my query's TermsFacetBuilder, the result is a _mix_ of both fields.. I  
have no way to associate the friendly value and the original value of the  
same term!

The workaround I'm thinking about is the following..

- 3 fields: "full", "friendly" and "fullAndFriendly"

- When I index a document, I add values in these three fields:

- When I search, I ask ES to use the "fullAndFriendly" field for the facet  
to return. I then parse the returned value to get both full values and  
friendly values for the facet's terms (yark!).

- I can now display the facet and its terms using the full values and build  
the URLs corresponding to this facet using the friendly values.

- When there is a friendly facet token in the URL, I use it in my search and  
tell ES to use the "friendly" field to search this value.

There must be an easiest way to do this! 🙂

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 29, 2011, 2:32pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268/4 "2011-08-29T14:32:43Z")

</div>

Another possible way to reduce the number of steps (by 1) is to use a  
term script where the value is similar to your fullAndFriendly field.

```
"facets" : {
    "manufacturers" : {
        "terms" : {
            "field" : "full",
            "size" : 10,
            "script" : "term + '[uniqueSeparatorString]' + _source.friendly"
        }
    }
}

```

That said, I am unable to set the uniqueSeparatorString to be anything  
besides a number without getting "Error: unresolvable property or  
identifier ..."

It would be great to have a value\_script for term facets, but I am not  
sure if that makes sense.

--  
Ivan

On Sun, Aug 28, 2011 at 5:12 PM, ogregras [ogregras@gmail.com](mailto:ogregras@gmail.com) wrote:

> I'm not too sure about my "solution" after all...
> 
> If I create a field dedicated to the "friendly" value, I have to get both  
> this field and the field containing the original value from a facet search :  
> the friendly value to create the URL token and the original value to display  
> the facet value to the user. The problem is, by setting both of those fields  
> to my query's TermsFacetBuilder, the result is a mix of both fields.. I have  
> no way to associate the friendly value and the original value of the same  
> term!  
> The workaround I'm thinking about is the following..
> 
> - 3 fields: "full", "friendly" and "fullAndFriendly"
> - When I index a document, I add values in these three fields:
> - Its full value in the "full" field
> - Its generated friendly value in the "friendly" field
> - And finally a concatenated value in the "fullAndFriendly"  
> field consisting in : fullValue + [uniqueSeparatorString] + friendlyValue
> 
> - When I search, I ask ES to use the "fullAndFriendly" field for the facet  
> to return. I then parse the returned value to get both full values and  
> friendly values for the facet's terms (yark!).
> - I can now display the facet and its terms using the full values and build  
> the URLs corresponding to this facet using the friendly values.
> - When there is a friendly facet token in the URL, I use it in my search and  
> tell ES to use the "friendly" field to search this value.
> 
> There must be an easiest way to do this! 🙂

---

<div class="post-metadata">

**Author:** ![ogregras](https://avatars.discourse-cdn.com/v4/letter/o/cdc98d/32.png) [@ogregras](https://discuss.elastic.co/u/ogregras)\
**Post date:** [August 29, 2011, 3:24pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268/5 "2011-08-29T15:24:24Z")

</div>

First, thanks for the help Ivan! I was pretty sure I would have no help with  
such a specific question!

I may investigate the term scripts, I wasn't aware of those!

But since adding a custom metadata to a field doesn't seem to be currently  
possible, I may also stick with my workaround. It requires 3 fields, which  
is not the ideal, but it works great. I just need to make sure the friendly  
tokens I generate CAN'T contain one of more of the characters that are used  
in the "uniqueSeparatorString". Then by spliting the fullAndFriendly field  
using lastIndexOf([uniqueSeparatorString]) , I'm 100% sure the left part is  
the full value and the right part is the friendly value. But it's still  
ugly...

Thanks again.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 29, 2011, 9:02pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-give-a-unique-id-to-a-field/5268/6 "2011-08-29T21:02:47Z")

</div>

On Mon, Aug 29, 2011 at 11:24 AM, ogregras [ogregras@gmail.com](mailto:ogregras@gmail.com) wrote:

> First, thanks for the help Ivan! I was pretty sure I would have no help with  
> such a specific question!

I don't think your question is very specific. Your use case for  
facets is typical and the combination of requiring not-analyzed fields  
for the facet and user-friendly description is annoying. Luckily for  
many of my use cases, the facets are known in advance, so I can  
provide an external mapping between facet and friendly value.

--  
Ivan

---

<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/is-there-a-way-to-give-a-unique-id-to-a-field/5268/7 "2017-07-06T03:55:39Z")

</div>


