| I try to do an SQL IN like query with the Hibernate Search API and experiencing an different behaviour on @Id an @Field attributes of an entity class. The used entities are:
@Entity
@Indexed(index = "hibernate/book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private String title;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private String subtitle;
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
@DateBridge(resolution = Resolution.DAY)
private Date publicationDate;
@IndexedEmbedded
@ManyToMany
private Set<Author> authors = new HashSet<>();
}
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Field
private String name;
}
The code for searching the titles of a book looks like:
List<String> titles = new ArrayList<>();
titles.add("Test1");
titles.add("Test2");
titles.add("Test2");
Query titleQuery = builder.keyword().withConstantScore().onField("title").matching(titles.stream().collect(Collectors.joining(" OR "))).createQuery();
logger.info("Title Query: {}", titleQuery);
FullTextQuery ftq = fullTextSession.createFullTextQuery(builder.bool().must(titleQuery).createQuery());
The log output for the titleQuery is:
ConstantScore(QueryWrapperFilter(title:test1 title:test2))
and the books with the titles are found. When searching for the book ids it no longer finds books. Using this code
List<String> ids = new ArrayList<>();
ids.add("1");
ids.add("2");
ids.add("3");
Query idQuery = builder.keyword().withConstantScore().onField("id").matching(ids.stream().collect(Collectors.joining(" OR "))).createQuery();
logger.info("Id Query: {}", idQuery);
FullTextQuery ftq = fullTextSession.createFullTextQuery(idQuery);
the id query is logged as:
ConstantScore(QueryWrapperFilter(id:1 OR 2 OR 3))
I would have expected the id query to look like
ConstantScore(QueryWrapperFilter(id:1 id:2 id:3))
I tested this on every given Hibernate Search version and the behaviour is the same. |