Developer forum

Forum » Development » Extending the index

Extending the index

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I would like to create a custom "facet group" in my index where values are placed in specific groups. I have different use cases, but let's assume price as an example. My products can have a variety of prices and I would like to add them to groups like "from 0 to 100", "from 101 to 200" and so on. Here's what I was thinking:

1. Get the lowest price in the system
2. Get the highest price in the system
3. Determine the various groups based on the highest and lowest price and do some rounding so each group has round numbers as its boundaries
4. Loop over the product data, find the price and create a new facet (I think?) for the price group.

Does this make sense? What would be my extensibility hook that makes this the simplest to implement? I looked at IIndexSchemaExtender and ProductIndexSchemaExtender but they provide fields, not data. I also looked at IIndexBuilder and ProductIndexBuilder. Is that my entry point? When ProductIndexBuilder is the answer, how do I add just my field without indexing the entire product from scratch?

I looked at the manual for indexing and it has a few dead links that could be helpful:

"If you do not want a field provided by an extender, you can exclude it. Read more here
Note: You can create your own schema extenders. Read more about the SchemaExtender API here."

However, the two occurrences of "here" go nowhere. Anyone know what they should be pointing to?

Thanks,

Imar


Replies

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Imar

The price ranges are already possible, see this link: http://doc.dynamicweb-cms.com/Default.aspx?ID=685

You can extend the Indexbuild, i.e. ProductIndexBuilder by creating a IndexBuilderExtender.

using Dynamicweb.Indexing;
using System;

namespace Dynamicweb.Examples.CSharp
{
    class IndexBuilderExtenderExample : Indexing.IndexBuilderExtenderBase<Dynamicweb.Ecommerce.Indexing.ProductIndexBuilder>
    {
        public override void ExtendDocument(IndexDocument document)
        {
            document.Add("CustomPriceField", "0-100");
        }
    }
}

Be aware that this is executed in the context of a thread that does not have access to system.web. That means part of the API will be an issue because it relies on that.

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Nicolai,

Thanks for the code and warning, That looks promising.

How do I get the product (or other indexed) data? Using document["SomeKey"] (and then call base.ExtendDocument(...) first?)

Imar

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Imar

Yes, the document contains all the fields that the IndexBuilder has added to the document - in this case, since we are using the ProductIndexBuilder, product data. In your index setup you can see which fields the document could contain (there are no key if there is no value), see dump #1.

Here is an example of how to take data from the document:

using Dynamicweb.Indexing;
using System;

namespace Dynamicweb.Examples.CSharp
{
    class IndexBuilderExtenderExample : Indexing.IndexBuilderExtenderBase<Dynamicweb.Ecommerce.Indexing.ProductIndexBuilder>
    {
        public override void ExtendDocument(IndexDocument document)
        {
            if (document.ContainsKey("ID"))
            {
                string productid = (string)document["ID"];
            }
            
            document.Add("CustomField", DateTime.Now);
        }
    }
}

On the document you have all the product data - you do not have a product object, which is also an issue because of the lack of http context. And it could be a performance problem.

BR Nicolai

Capture.PNG
Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks, that's all very useful. I'll give it a try.

Imar

 

You must be logged in to post in the forum