Search code examples
hibernate-search-6

Duplicate index field definition:


How to handle this scenario? Here is the property defined in my entity class

@Column(nullable = false)
@NotNull
@ApiModelProperty(required = true)
@Size(min = 3, max = 255)
@GenericField(sortable = Sortable.YES)
@FullTextField(analyzer = "lowercaseWhitespaceAnalyzer")
private String title;

Exception:

HSEARCH400520: Duplicate index field definition: 'title'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.

Do I need to create two separate fields as shown in the documentation like:

@FullTextField
@KeywordField(name = "title_sort", normalizer = "myNormalizer", sortable = Sortable.YES)
private String title;

If I tried this getting below exception:

Invalid value. Expected 'lowercaseWhitespaceAnalyzer', actual is 'null' field 'context': attribute 'type': failures: - Invalid value. Expected 'text', actual is 'keyword' attribute 'analyzer': failures: - Invalid value. Expected 'lowercaseWhitespaceAnalyzer', actual is 'null' field 'context_sort': failures: - Missing property mapping


Solution

  • Do I need to create two separate fields as shown in the documentation like:

    Yes, indeed, you do.

    If I tried this getting below exception:

    Invalid value. Expected 'lowercaseWhitespaceAnalyzer', actual is 'null' field 'context': attribute 'type': failures: - Invalid value. Expected 'text', actual is 'keyword' attribute 'analyzer': failures: - Invalid value. Expected 'lowercaseWhitespaceAnalyzer', actual is 'null' field 'context_sort': failures: - Missing property mapping

    If you look at the full context of this error (which you didn't include here), you'll notice it says something like "Elasticsearch schema validation failed".

    In short, the schema of the index that already exists on Elasticsearch doesn't match what Hibernate Search needs to implement what you described with annotations.

    You should drop the Elasticsearch schema and re-create it (you'll lose all indexed data and will need to reindex). You can do it manually, or you can let Hibernate Search do it for you; see this section of the documentation.