Hibernate Search 6 : Cannot use 'predicate:simple-query-string' on field

I tried to convert below HS 5 code into HS 6 but its throwing exception while searching

created_date defined and isEligible boolean field in entity class like

  @ApiModelProperty(hidden = true)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    @Column(name = "created_date", nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    @GenericField
    private Date createdDate;

```@Column(name = "is_eligible", nullable = false)
    @NotNull
    @ApiModelProperty(required = true)
    @GenericField
    @JsonAlias("isEligible")
    private boolean isEligible;

search ="";
excludedFields = [createdDate, isEligible]

HS 5 code

 query = ArrayUtils.isEmpty(excludedFields) ? qb.all().createQuery()
                    : qb.all().except(qb.simpleQueryString().onFields(excludedFields[0], excludedFields) .matching(search).createQuery()).createQuery();

Converted into HS 6

searchPredicate = ArrayUtils.isEmpty(excludedFields) ? pf.matchAll().toPredicate()
                    : pf.matchAll().except(pf.simpleQueryString().fields(excludedFields)
                    .matching(search).toPredicate()).toPredicate();

Exception -

Caused by: org.hibernate.search.util.common.SearchException: HSEARCH400614: Cannot use 'predicate:simple-query-string' on field 'createdDate'. Make sure the field is marked as searchable/sortable/projectable/aggregable (whichever is relevant). If it already is, then 'predicate:simple-query-string' is not available for fields of this type.
Context: field 'createdDate'
        at org.hibernate.search.backend.elasticsearch.document.model.impl.ElasticsearchIndexSchemaValueFieldNode.queryElement(ElasticsearchIndexSchemaValueFieldNode.java:70)
        at org.hibernate.search.backend.elasticsearch.search.predicate.impl.ElasticsearchSimpleQueryStringPredicate$Builder.field(ElasticsearchSimpleQueryStringPredicate.java:182)
        at org.hibernate.search.engine.search.predicate.dsl.impl.SimpleQueryStringPredicateFieldMoreStepImpl$CommonState.field(SimpleQueryStringPredicateFieldMoreStepImpl.java:74)
        at org.hibernate.search.engine.search.predicate.dsl.impl.SimpleQueryStringPredicateFieldMoreStepImpl.<init>(SimpleQueryStringPredicateFieldMoreStepImpl.java:38)
        at org.hibernate.search.engine.search.predicate.dsl.impl.SimpleQueryStringPredicateFieldStepImpl.fields(SimpleQueryStringPredicateFieldStepImpl.java:27)

As the error message is telling you, the simpleQueryString predicate is not available on fields of this type (Date). You can only use simpleQueryString on fields of type string.

The difference with Hibernate Search 5 is that Date field used to be indexed as strings, while they are now indexed as numbers in Hibernate Search 6.

EDIT: Actually I made a few mistakes, see next post.

If you want to use this predicate on this field, you need to change the type of the field, and that will probably require a custom value bridge

Actually, that’s wrong:

  1. The change of encoding I mentioned is wrong: dates are indexed as numbers in the Lucene backend, and already were in Hibernate Search 5. But you’re using the Elasticsearch backend, so this change of format is not relevant to you anyway; dates are indexed as date in the Elasticsearch backend, in Hibernate Search 5 and 6 both.
  2. Taking into account the encoding used for dates in Hibernate Search 5, I don’t think simpleQueryString worked on date fields in Hibernate Search 5. Most likely, Hibernate Search 5 simply ignored these fields, and consequently simpleQueryString didn’t work for these fields.

Anyway, my advice still holds:

But alternatively, you can just remove this field from this predicate, since it was most likely ignored in Hibernate Search 5 anyway.

1 Like

I just removed createdDate field then tried, occurred same exception for other field which is of type boolean.

Well, yes. It’s not a string. simpleQueryString is for strings.

Okay, you mean I need to use custom value bridge for both the types of fields? is there any other way in order work this same as Hibernate Search 5 ?

You should have a look at what simpleQueryString does because it makes no sense to call it on boolean or dates. It’s used to implement full text search in strings.

So take a step back and think about what you want to achieve with your search.

1 Like

Implemented custom value bridge for both the types and it is working fine on simpleQueryString

Thanks

Thank you @gsmet for the information