Reputation: 380
I have a lucene query that makes fulltext search on indexed fields. I want to add date range to this query.
I found that question and used the answer there: How to search between dates (Hibernate Search)?
But when I want to get data between dates it returns nothing.
I am using MSSQL db and type of date field is datetime.
But it is annotated as @Temporal(TemporalType.TIMESTAMP)
in entity class.
Here is my entity class:
...
@Entity
@Indexed
@FullTextFilterDef(name = "tarihAraligi", impl = Satislar.class)
@Table(name = "satislar")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Satislar.findAll", query = "SELECT s FROM Satislar s"),
@NamedQuery(name = "Satislar.findById", query = "SELECT s FROM Satislar s WHERE s.id = :id"),
@NamedQuery(name = "Satislar.findByAdet", query = "SELECT s FROM Satislar s WHERE s.adet = :adet"),
@NamedQuery(name = "Satislar.findByTarih", query = "SELECT s FROM Satislar s WHERE s.tarih = :tarih"),
@NamedQuery(name = "Satislar.findByUrunadi", query = "SELECT s FROM Satislar s WHERE s.urunadi = :urunadi"),
@NamedQuery(name = "Satislar.findByMusteriadi", query = "SELECT s FROM Satislar s WHERE s.musteriadi = :musteriadi"),
@NamedQuery(name = "Satislar.findByUrunkategori", query = "SELECT s FROM Satislar s WHERE s.urunkategori = :urunkategori"),
@NamedQuery(name = "Satislar.findByUrunfiyat", query = "SELECT s FROM Satislar s WHERE s.urunfiyat = :urunfiyat"),
@NamedQuery(name = "Satislar.findByUrunalis", query = "SELECT s FROM Satislar s WHERE s.urunalis = :urunalis")})
public class Satislar implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@DocumentId
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "adet")
private int adet;
@Basic(optional = false)
@Column(name = "tarih")
@Temporal(TemporalType.TIMESTAMP)
private Date tarih;
@Basic(optional = false)
@Column(name = "urunadi")
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String urunadi;
@Basic(optional = false)
@Column(name = "musteriadi")
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String musteriadi;
@Basic(optional = false)
@Column(name = "urunkategori")
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
private String urunkategori;
@Basic(optional = false)
@Column(name = "urunfiyat")
private int urunfiyat;
@Basic(optional = false)
@Column(name = "urunalis")
private int urunalis;
...
And this is where I make fulltext search:
fullTextSession.beginTransaction();
QueryBuilder b = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(Satislar.class).get();
Query luceneQuery
= b.keyword()
.wildcard()
.onFields(fields)
.matching(kelime + "*")
.createQuery();
Query datequery = b
.range()
.onField( "tarih" ).ignoreFieldBridge()
.from( DateTools.dateToString( new Date(2015, 12, 18 , 17, 40, 40), DateTools.Resolution.MILLISECOND ) )
.to( DateTools.dateToString( new Date(2015, 12, 26 , 17, 40, 40), DateTools.Resolution.MILLISECOND ) ).excludeLimit()
.createQuery();
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(datequery);
List<Satislar> kayitliSatislar = fullTextQuery.list();
dtmSonuc.setRowCount(0);
for (int i = 0; i < kayitliSatislar.size(); i++) {
Satislar satis = kayitliSatislar.get(i);
dtmSonuc.addRow(new String[]{satis.getMusteriadi(), satis.getUrunkategori(), satis.getUrunadi(),
Integer.toString(satis.getAdet()), Integer.toString(satis.getUrunfiyat()), satis.getTarih().toString()});
}
There are 2 different queries. One makes fulltext wildcard search on specified fields and it works. And the other is supposed to make range search but it does not work
I have 3 questions:
1-I am using MSSQL db and type of date field is datetime.
But it is annotated as @Temporal(TemporalType.TIMESTAMP)
in entity class. Is that a problem?
2-Why range search is not working?
3-Can I combine fulltext search with range search?
Upvotes: 0
Views: 2125
Reputation: 1164
Try to add this annotation to your Date fields :
@Basic(optional = false)
@Column(name = "tarih")
@Temporal(TemporalType.TIMESTAMP)
@DateBridge(resolution = Resolution.DAY)
private Date tarih;
If it is still not working, try to use Lucene native queries like this :
TermRangeQuery luceneRangeDateQuery = new TermRangeQuery(fieldName, DateTools.dateToString(from, DateTools.Resolution.DAY),
DateTools.dateToString(to, DateTools.Resolution.DAY), true, true);
This works always. (You can change your DateTools.Resolution.DAY to HOUR, SECOND, YEAR ...)
Upvotes: 1