Add tag to index for variants

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

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

What does this mean?

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
}
}

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:

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.

1 Like

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

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

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));
}

Inject Securitytoken and use its properties.

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.

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.

If organizationId exists then search will match it against it

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?

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.

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"?

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.

Thanks Patric for the quick respond and suggestion!

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