Advertisement
Guest User

Untitled

a guest
Dec 25th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. public class CustomFieldBridge implements FieldBridge {
  2.  
  3. private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
  4. private final static TimeZone GMT = TimeZone.getTimeZone("GMT");
  5.  
  6. @Override
  7. public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
  8. Date date = (Date) value;
  9. Calendar cal = GregorianCalendar.getInstance(GMT);
  10. cal.setTime(date);
  11. int year = cal.get(Calendar.YEAR);
  12. int month = cal.get(Calendar.MONTH) + 1;
  13. int day = cal.get(Calendar.DAY_OF_MONTH);
  14.  
  15. // set year
  16. addStringField(name + ".year", String.valueOf(year), document, luceneOptions);
  17.  
  18. // set month and pad it if needed
  19. addStringField(name + ".month", month < 10 ? "0" : "" + String.valueOf(month), document, luceneOptions);
  20.  
  21. // set day and pad it if needed
  22. addStringField(name + ".day", day < 10 ? "0" : "" + String.valueOf(day), document, luceneOptions);
  23. }
  24.  
  25. private void addStringField(String fieldName, String fieldValue, Document document, LuceneOptions luceneOptions) {
  26. Field field = new Field(fieldName, fieldValue, luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector());
  27. field.setBoost(luceneOptions.getBoost());
  28. try {
  29. field.setTokenStream(new StandardAnalyzer().tokenStream(fieldName, new StringReader(fieldValue)));
  30. } catch (IOException ex) {
  31. LOGGER.error(null, ex);
  32. }
  33. System.out.println("Field : " + field);
  34. document.add(field);
  35. }
  36. }
  37.  
  38. @Basic
  39. @Column(name = "time")
  40. @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  41. @Audited
  42. @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
  43. @FieldBridge(impl = CustomFieldBridge.class)
  44. private Date time;
  45.  
  46. SearchFactory searchFactory = fullTextSession.getSearchFactory();
  47. QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity(Meeting.class).get();
  48. org.apache.lucene.search.Query dateQuery = queryBuilder.keyword()
  49. .onField("time")
  50. .matching("2014").createQuery();
  51. org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(dateQuery).setProjection(ProjectionConstants.THIS, ProjectionConstants.SCORE);
  52. List result = fullTextQuery.list();
  53.  
  54. .onField("time.year")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement