1

How to index list of strings in Hibernate search?

i tried like this

@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES, analyzer = @Analyzer(definition = "customanalyzer_query"))
@ElementCollection(fetch = FetchType.EAGER)
private Set<String> hashedTagList;

I'm getting error while committing new object.

I'm using Hibernate ogm with mongodb

1
  • did you try "Set<String>" ? It can't index elements of type Object. Commented Mar 8, 2018 at 15:31

1 Answer 1

4

You can index element collections by using @IndexedEmbedded. This would be the easiest way to do what you want:

@Field(analyze = Analyze.NO, store = Store.YES)
@IndexedEmbedded
private Set<String> keywords = new HashSet<String>();

Note that you have to use Set so that the type of the contained element is clearly defined.

It's a workaround, we plan to fix the issue in a cleaner way in the upcoming Search 6.

You can also use a @FieldBridge to denormalize the data. The remark about using Set stays valid.

You can find the @FieldBridge we used at my previous job exactly for this purpose here: https://github.com/openwide-java/owsi-core-parent/blob/master/owsi-core/owsi-core-components/owsi-core-component-jpa/src/main/java/fr/openwide/core/jpa/search/bridge/StringCollectionFieldBridge.java .

By the way, you define an analyzer but you set analyze to Analyze.NO so your analyzer won't be used.

Sign up to request clarification or add additional context in comments.

9 Comments

getting this error now : org.hibernate.search.bridge.BridgeException: Exception while calling bridge#objectToString entity class: com.healthelife.notes.entity.Notes entity property path: hashedTagList field bridge: String2FieldBridgeAdaptor [stringBridge=com.healthelife.notes.helper.CollectionToCSVBridge@56223ef0]
Well I can't really help you if you don't use what I recommended and you don't post the full exception. Updated my answer with a working setup.
sorry for not providing full exception, i followed whatever you suggested only - StringCollectionFieldBridge
here is my entity class ---- @FieldBridge(impl=StringCollectionFieldBridge.class) @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES, analyzer = @Analyzer(definition = "customanalyzer_query")) @ElementCollection(fetch = FetchType.EAGER) private Set<String> hashedTagList; public Set<String> getHashedTagList() { return hashedTagList; } public void setHashedTagList(Set<String> hashedTagList) { this.hashedTagList = hashedTagList; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.