Yes, both neighborhood and street are indexed entities.
some parts of neighborhood entity:
Code:
@DocumentId
@IndexedEmbedded
@FieldBridge(impl = NeighborhoodIdBridge.class)
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "countryIso2", column = @Column(name = "country_iso2", nullable = false, length = 2)),
@AttributeOverride(name = "subdivId", column = @Column(name = "subdiv_id", nullable = false, length = 4)),
@AttributeOverride(name = "cityId", column = @Column(name = "city_id", nullable = false, length = 6)),
@AttributeOverride(name = "neighId", column = @Column(name = "neigh_id", nullable = false, length = 6)) })
public TNeighborhoodId getId() {
return this.id;
}
@IndexedEmbedded
@OneToMany(fetch = FetchType.LAZY, mappedBy = "TNeighborhood")
public Set<TStreet> getTStreets() {
return this.TStreets;
}
street entity:
Code:
@ContainedIn
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "country_iso2", referencedColumnName = "country_iso2", nullable = false),
@JoinColumn(name = "subdiv_id", referencedColumnName = "subdiv_id", nullable = false),
@JoinColumn(name = "city_id", referencedColumnName = "city_id", nullable = false),
@JoinColumn(name = "neigh_id", referencedColumnName = "neigh_id", nullable = false) })
public TNeighborhood getTNeighborhood() {
return this.TNeighborhood;
}
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
@Column(name = "street_name", nullable = false, length = 200)
public String getStreetName() {
return this.streetName;
}
// must be indexed for sorting
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
@Column(name = "street_numestab", nullable = false)
public short getStreetNumestab() {
return this.streetNumestab;
}
numestab is updated which seems to cause the problem.
Search code:
Code:
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
.getFullTextEntityManager(this.entityManager);
QueryContextBuilder qcb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder();
QueryBuilder qb = qcb.forEntity(TNeighborhood.class).get();
BooleanJunction<?> bj = qb.bool().must(
qb.keyword().onField("id.countryIso2").matching(countryIso2)
.createQuery());
bj = bj.must(qb.keyword().onField("id.subdivId").matching(subdivId)
.createQuery());
if (CodeUtils.notEmptyOrHyphen(cityId)) {
bj = bj.must(qb.keyword().onField("id.cityId").matching(cityId)
.createQuery());
}
else {
bj = bj.must(qb.keyword().onField("TCity.cityNameSearchNoAna")
.matching(CodeUtils.removeDiacritics(cityName).toLowerCase())
.createQuery());
}
if (CodeUtils.notEmptyOrHyphen(neighborhoodId)) {
bj = bj.must(qb.keyword().onField("id.neighId")
.matching(neighborhoodId).createQuery());
}
else {
bj = bj.must(qb.keyword().onField("neighNameSearchNoAna")
.matching(CodeUtils.removeDiacritics(neighName).toLowerCase())
.createQuery());
}
if (prefix != null) {
bj = bj.must(qb.keyword().wildcard().onField("TStreets.streetName")
.matching(CodeUtils.removeDiacritics(prefix).toLowerCase() + "*")
.createQuery());
}
FullTextQuery query = fullTextEntityManager.createFullTextQuery(
bj.createQuery(), TNeighborhood.class);
query.setProjection(ProjectionConstants.DOCUMENT);
SortField sortField;
if (order == Order.numEstabs) {
sortField = new SortField("TStreets.streetNumestab", SortField.INT,
true);
}
else {
sortField = new SortField("TStreets.streetName", SortField.STRING,
false);
}
query.setSort(new Sort(sortField));
if (numMaxRows != null) {
query.setMaxResults(numMaxRows);
}
query.limitExecutionTimeTo(2, TimeUnit.SECONDS);
List<StreetBean> ret = new ArrayList<StreetBean>();
for (Object row : query.getResultList()) {
Document doc = (Document) ((Object[]) row)[0];
String streetId = doc.get("TStreets.streetId");
if (streetId != null) {
ret.add(new StreetBean(
Integer.parseInt(streetId),
countryIso2,
subdivId,
cityId,
neighborhoodId,
doc.get("TStreets.streetName"),
Integer.parseInt(doc.get("TStreets.streetNumestab")),
Integer.parseInt(doc.get("TStreets.TStreetType"))));
}
}
Result list "ret" contains the same street several times, although the street index (and the db table of course) contain it only once.
It seems that after merge of a street entity, a new document is created in the neighborhood index. All these different neighborhood documents contain several streets of the neighborhood.
Thanks!