Developer forum

Forum » Development » Change analyzer in Lucene-index

Change analyzer in Lucene-index

Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Hi,

I have a product index, where I need to use different analyzers on the different fields - the StandardAnalyzer on fields with number/decimals and the KeywordAnalyzer on text-sentences.

The fields are created dynamically from an external PIM-system, by creating FieldDefinitions in a custom SchemaExtender using the api.

The FieldDefinitionBase has a property called AnalyzerTypeName (https://doc.dynamicweb.com/api/html/47aa76e3-5059-f5f3-6237-b4f50c6cdc6d.htm), but I doesn't seem to be working for setting the desired analyzer - if possible at all?

--
Best regards
Kurt Moskjaer Andersen


Replies

 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Anyone with the same problem/challenge?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Kurt,

When you need a different analyzer for a specific field, you should use custom field types (https://doc.dynamicweb.com/documentation-9/repositories/indexing/custom-fields#sideNavTitle1-3), and then update the field definition to use the new field type. There are some limitations about which analyzers you can use, but the list contains all the ones that are available out of the box. You can create your own analyzers if you need custom logic. Let me know if you need more info about that.

Hope this helps.

- Jeppe

 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Hi Jeppe,

Thank you very much for your answer.

So if I have different field types today, like System.String and System.String[], I should create a custom field type for each, select the appropriate analyzer and use this custom field type in the field definitions?

I suppose I can set the field type on the field definition through the api, by setting the typename to the custom field type, when creating the field definition through the api?

/Kurt

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Kurt,

Yes, that's exactly right. Once you have the custom field type, you simply use it as if it were a regular field type.

However, rereading your initial post, I now see you want to do this at runtime through a SchemaExtender. You were on the right track with the AnalyzerTypeName on the field. For some reason, the API checks whether there are any custom field types before allowing custom analyzers to be used. Adding a custom field type and setting the AnalyzerTypeName on the fields should solve the issue for you. You don't need to create a relevant custom field type, or even use it. It just needs to be present.

For completeness sake, I'll leave the process to update field(s) of an existing index here anyway, even though it's not needed in your case:

  1. Get the index through the IIndexService implementation
  2. Get the relevant field(s) from index.Schema.FieldsFromIndexDefinition
  3. Get the custom field type(s) from index.Schema.FieldTypes
  4. Update the field(s) by calling field.SetFieldType(fieldType)
  5. Save the index using indexService.SaveIndex(...)

- Jeppe

Votes for this answer: 2

 

You must be logged in to post in the forum