# Add tag to index for variants

**URL:** https://forum.litium.com/t/add-tag-to-index-for-variants/957
**Category:** Questions
**Tags:** accelerator, search, litium-7
**Created:** [July 1, 2019, 1:03pm UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957 "2019-07-01T13:03:57Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 1, 2019, 1:03pm UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/1 "2019-07-01T13:03:57Z")

</div>

Hi

I added a new string field to the product template with one variant. I want my custom field to be searchable when my product category. Is this possible with adding Tags, and then add this to the search?

How is it done? Should i implement further on the  
`VariantIndexDocumentMerger : IIndexDocumentMerger<Variant>`  
and how?

Appreciate any help on this issue.

Litium version: 7.2

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 1, 2019, 2:04pm UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/2 "2019-07-01T14:04:48Z")

</div>

> I want that field to be searchable when my product category.

What does this mean?

---

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 1, 2019, 2:09pm UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/3 "2019-07-01T14:09:13Z")

</div>

oops sorry. I mean my custom field that i added to the product template for one variant.

MyCustomField → string

> new FieldTemplateFieldGroup  
> {  
> Id = “Product specification”,  
> Collapsed = false,  
> Localizations =  
> {  
> [“sv-SE”] = { Name = “Produktspecifikation” },  
> [“en-US”] = { Name = “Product specification” }  
> },  
> Fields =  
> {  
> “Specification”,  
> “Weight”,  
> **ProductFieldNameConstants.MyCustomField**  
> }  
> }

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 1, 2019, 2:53pm UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/4 "2019-07-01T14:53:25Z")

</div>

You’ve created the field as well? Your code is only for adding a field with that name to the template.

I’m still not sure what you want to do. If you want to search using the new field programmatically, you can add it in a search query with something like this:

```auto
var field = _fieldDefinitionService.Get<ProductArea>(ProductFieldNameConstants.MyCustomField);
var query = new QueryRequest(Solution.Instance.Languages.DefaultLanguageID, ProductCatalogSearchDomains.Products, Solution.Instance.SystemToken);
query.FilterTags.Add(field.GetTagName(), "value")
var result = Solution.Instance.SearchService.Search(query);

```

If you want to use your new field as a filter for a category, you first need to add it under _Settings_ \> _Accelerator_ \> _Product filtering_.  
(If it doesn’t show up, make sure Filter in lists is set to True for the field).

Then go to _Settings_ \> _Websites_ \> _Websites_, edit your website and add the field under **Filters**.

---

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 3, 2019, 8:17am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/5 "2019-07-03T08:17:00Z")

</div>

I will explain the issue, its for a B2B website.

We have products that has a custom field called OrganizationId, which we want to mapped to the Organization’s LegalNumberId.

So if you are an unlogged user you should not see any of the product that has an filled organizationId on it.

If your are a logged user then you should see all products and the ones that match your organizationId. So we want to exclude those product that doesnt match your organizationId.

I found out that VariantIndexDocumentMerger adds all field to the index document for product. In that case we only need to add request.ExcludeTags.Add(new Tag(“document-property-organizationid”, “something here”));" to ProductSearchService.cs. But if there another way or better way?

Right now i am getting all products but then removing the ones the user doesn’t has access to it. Could it be done before the search, maybe in the searchQueryBuilder?

Thanks for your help

---

<div class="post-metadata">

### Author: ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)
#### Post date: [July 3, 2019, 8:40am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/6 "2019-07-03T08:40:03Z")

</div>

You can to do it in SearchQueryBuilder so it checks for it User is logged in or not.

---

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 3, 2019, 9:34am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/7 "2019-07-03T09:34:44Z")

</div>

How can i exclude product that have indexed my custom property.

I want to exclude all product that doesn’t have an organizationId filled.

Tried this but it didnt work.

> if (HttpContext.Current.User?.Identity?.IsAuthenticated != null && HttpContext.Current.User?.Identity?.IsAuthenticated != true)  
> {  
> var tagName = Foundation.Search.Constants.TagNames.GetTagNameForProperty(GcProductFieldNameConstants.OrganizationId);  
> \_request.FilterTags.Add(new Tag(tagName, string.Empty));  
> }

---

<div class="post-metadata">

### Author: ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)
#### Post date: [July 3, 2019, 11:46am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/8 "2019-07-03T11:46:24Z")

</div>

Inject Securitytoken and use its properties.

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 3, 2019, 2:46pm UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/9 "2019-07-03T14:46:07Z")

</div>

Lucene won’t index empty values, so you would have to set a default value on the products or index a default value if the field is empty (by building an `IIndexingProviderPreProcessor`).

Then you can filter on that default value.

In the Accelerator you can find `MostSoldIndexingProviderPreProcessor` as an example of how to modify the search document index.

---

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 5, 2019, 7:53am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/10 "2019-07-05T07:53:38Z")

</div>

Thanks Nils

But now i wonder if i could use Tags as in `_request.FilterTags.Add("organizationId", "123456");` to add a additional values to my search. I know FilterTags excludes values but i am looking for something to add more item to my search by matching the value of the Tag.  
So i get my regular result and those that match the value `"123456"` of the Tag.  
Is it possible?

Thanks again.

---

<div class="post-metadata">

### Author: ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)
#### Post date: [July 5, 2019, 8:01am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/11 "2019-07-05T08:01:15Z")

</div>

If organizationId exists then search will match it against it

---

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 5, 2019, 8:06am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/12 "2019-07-05T08:06:41Z")

</div>

Hi Steve

The OrganizationId exist, but the problem when using FilterTags is that it excludes all other item. Im looking for something to add item to my search by matching to the value of the Tag key, eg “123456”.  
Like ReadTag does but with the option to add a value.

Is there an SearchQuery method/property that could do this?

---

<div class="post-metadata">

### Author: ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)
#### Post date: [July 5, 2019, 8:11am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/13 "2019-07-05T08:11:46Z")

</div>

If you are adding `_request.FilterTags.Add("organizationId", "123456")` to the query, the query will only return the documents that have the tag `organizationId` with value `123456`.

---

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 5, 2019, 8:15am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/14 "2019-07-05T08:15:08Z")

</div>

Hi Patric

Exactly, but is there a way to add additional item to the search by matching the value of the Tags key that is matching with `"123456"`?

---

<div class="post-metadata">

### Author: ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)
#### Post date: [July 5, 2019, 8:17am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/15 "2019-07-05T08:17:07Z")

</div>

If I understand correctly you will do a search for the `organizationId` with the specific values and use the result from that in the query?

In that case, no. It is not possible in one search, you need to do two separate searches.

---

<div class="post-metadata">

### Author: ![Gonzalo](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@Gonzalo](https://forum.litium.com/u/Gonzalo)
#### Post date: [July 5, 2019, 8:18am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/16 "2019-07-05T08:18:32Z")

</div>

Thanks Patric for the quick respond and suggestion!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)
#### Post date: [August 2, 2019, 9:01am UTC](https://forum.litium.com/t/add-tag-to-index-for-variants/957/17 "2019-08-02T09:01:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
