cuba-platform / fts

Full-Text Search Addon
https://www.cuba-platform.com/
Apache License 2.0
4 stars 1 forks source link

Entity update and delete removes other entities with the same ID from the fts index #64

Closed gorbunkov closed 5 years ago

gorbunkov commented 5 years ago
  1. Create two entities with String ID, e.g. Firm and City

    @NamePattern("%s|name")
    @Table(name = "SAMPLE_FIRM")
    @Entity(name = "sample_Firm")
    public class Firm extends BaseStringIdEntity implements HasUuid {
    
    @Id
    @Column(name = "CODE", nullable = false, length = 10)
    protected String code;
    
    @Column(name = "NAME")
    protected String name;
    
    @Column(name = "ADDRESS")
    protected String address;
    
    @Column(name = "UUID")
    protected UUID uuid;
    
    //getters and setters
    }
@NamePattern("%s|name")
@Table(name = "SAMPLE_CITY")
@Entity(name = "sample_City")
public class City extends BaseStringIdEntity implements HasUuid {
    private static final long serialVersionUID = -7090507348534615303L;

    @Id
    @Column(name = "CODE", nullable = false, length = 10)
    protected String code;

    @Column(name = "NAME")
    protected String name;

    @Column(name = "UUID")
    protected UUID uuid;

   //getters and setters
}
  1. Generate screens for the entities and add entities to the fts.xml config.
  2. Run the application. Create a city and a firm with the same code, e.g. 001.
  3. Reindex all and process the FTS queue using the FtsManager MBean
  4. Search for the 001 - you'll get two records: one city and one firm.
  5. Update the city - modify its name for example.
  6. Process FTS queue
  7. Execute the full-text search for the 001 ER: two records will be found AR: only one record is found

This happens because of the following code in the LuceneIndexerBean.indexEntity:

if (FtsChangeType.UPDATE.equals(changeType)) {
     writer.updateDocument(new Term(FLD_ID, entityId.toString()), doc);

The code doesn't take into account that there may be entities with the same id but with the different type in the FTS index.

Sample project: fts-update-bug.zip

P.S. The same error happens on entity deletion. If you remove the Firm with a code 001, the city with this code will be removed from the FTS index as well.