org.apache.lucene.document.DateTools Java Examples

The following examples show how to use org.apache.lucene.document.DateTools. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FilePositionDoc.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Document Document(File f)
     throws java.io.FileNotFoundException {
  Document doc = new Document();
  doc.add(new StoredField("path", f.getPath()));
  doc.add(new StoredField("modified",
                    DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE)));
  
  //create new FieldType to store term positions (TextField is not sufficiently configurable)
  FieldType ft = new FieldType();
  ft.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
  ft.setTokenized(true);
  ft.setStoreTermVectors(true);
  ft.setStoreTermVectorPositions(true);
  Field contentsField = new Field("contents", new FileReader(f), ft);

  doc.add(contentsField);
  return doc;
}
 
Example #2
Source File: FilePositionDoc.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Document Document(String inLine, int lineNumber) {
	  	
		Document doc = new Document();
	    doc.add(new StoredField("line_number", ""+lineNumber));
	    doc.add(new StoredField("modified",
	                      DateTools.timeToString(System.currentTimeMillis(), DateTools.Resolution.MINUTE)));
	    
	    //create new FieldType to store term positions (TextField is not sufficiently configurable)
	    FieldType ft = new FieldType();
	    ft.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
	    ft.setTokenized(true);
	    ft.setStoreTermVectors(true);
	    ft.setStoreTermVectorPositions(true);
	    Field contentsField = new Field("contents", inLine, ft);

	    doc.add(contentsField);
	    return doc;
}
 
Example #3
Source File: LuceneMessageSearchIndex.java    From james-project with Apache License 2.0 6 votes vote down vote up
private Query createQuery(String field, DateOperator dop) throws UnsupportedSearchException {
    Date date = dop.getDate();
    DateResolution res = dop.getDateResultion();
    DateTools.Resolution dRes = toResolution(res);
    String value = DateTools.dateToString(date, dRes);
    switch (dop.getType()) {
    case ON:
        return new TermQuery(new Term(field, value));
    case BEFORE: 
        return new TermRangeQuery(field, DateTools.dateToString(MIN_DATE, dRes), value, true, false);
    case AFTER: 
        return new TermRangeQuery(field, value, DateTools.dateToString(MAX_DATE, dRes), false, true);
    default:
        throw new UnsupportedSearchException();
    }
}
 
Example #4
Source File: LuceneMessageSearchIndex.java    From james-project with Apache License 2.0 6 votes vote down vote up
private DateTools.Resolution toResolution(DateResolution res) {
    switch (res) {
    case Year:
        return DateTools.Resolution.YEAR;
    case Month:
        return DateTools.Resolution.MONTH;
    case Day:
        return DateTools.Resolution.DAY;
    case Hour:
        return DateTools.Resolution.HOUR;
    case Minute:
        return DateTools.Resolution.MINUTE;
    case Second:
        return DateTools.Resolution.SECOND;
    default:
        return DateTools.Resolution.MILLISECOND;
    }
}
 
Example #5
Source File: StandardQueryConfigHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public StandardQueryConfigHandler() {
  // Add listener that will build the FieldConfig.
  addFieldConfigListener(new FieldBoostMapFCListener(this));
  addFieldConfigListener(new FieldDateResolutionFCListener(this));
  addFieldConfigListener(new PointsConfigListener(this));
  
  // Default Values
  set(ConfigurationKeys.ALLOW_LEADING_WILDCARD, false); // default in 2.9
  set(ConfigurationKeys.ANALYZER, null); //default value 2.4
  set(ConfigurationKeys.DEFAULT_OPERATOR, Operator.OR);
  set(ConfigurationKeys.PHRASE_SLOP, 0); //default value 2.4
  set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, false); //default value 2.4
  set(ConfigurationKeys.FIELD_BOOST_MAP, new LinkedHashMap<String, Float>());
  set(ConfigurationKeys.FUZZY_CONFIG, new FuzzyConfig());
  set(ConfigurationKeys.LOCALE, Locale.getDefault());
  set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD, MultiTermQuery.CONSTANT_SCORE_REWRITE);
  set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, new HashMap<CharSequence, DateTools.Resolution>());
  
}
 
Example #6
Source File: FieldDateResolutionFCListener.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void buildFieldConfig(FieldConfig fieldConfig) {
  DateTools.Resolution dateRes = null;
  Map<CharSequence, DateTools.Resolution> dateResMap = this.config.get(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP);

  if (dateResMap != null) {
    dateRes = dateResMap.get(
        fieldConfig.getField());
  }

  if (dateRes == null) {
    dateRes = this.config.get(ConfigurationKeys.DATE_RESOLUTION);
  }

  if (dateRes != null) {
    fieldConfig.set(ConfigurationKeys.DATE_RESOLUTION, dateRes);
  }

}
 
Example #7
Source File: QueryParserBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the date resolution that is used by RangeQueries for the given field.
 * Returns null, if no default or field specific date resolution has been set
 * for the given field.
 *
 */
public DateTools.Resolution getDateResolution(String fieldName) {
  if (fieldName == null) {
    throw new IllegalArgumentException("Field must not be null.");
  }

  if (fieldToDateResolution == null) {
    // no field specific date resolutions set; return default date resolution instead
    return this.dateResolution;
  }

  DateTools.Resolution resolution = fieldToDateResolution.get(fieldName);
  if (resolution == null) {
    // no date resolutions set for the given field; return default date resolution instead
    resolution = this.dateResolution;
  }

  return resolution;
}
 
Example #8
Source File: TestDateSort.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Document createDocument(String text, long time) {
  Document document = new Document();

  // Add the text field.
  Field textField = newTextField(TEXT_FIELD, text, Field.Store.YES);
  document.add(textField);

  // Add the date/time field.
  String dateTimeString = DateTools.timeToString(time, DateTools.Resolution.SECOND);
  Field dateTimeField = newStringField(DATE_TIME_FIELD, dateTimeString, Field.Store.YES);
  document.add(dateTimeField);
  document.add(new SortedDocValuesField(DATE_TIME_FIELD, new BytesRef(dateTimeString)));

  return document;
}
 
Example #9
Source File: QueryParserBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the date resolution used by RangeQueries for a specific field.
 *
 * @param fieldName field for which the date resolution is to be set
 * @param dateResolution date resolution to set
 */
public void setDateResolution(String fieldName, DateTools.Resolution dateResolution) {
  if (fieldName == null) {
    throw new IllegalArgumentException("Field must not be null.");
  }

  if (fieldToDateResolution == null) {
    // lazily initialize HashMap
    fieldToDateResolution = new HashMap<>();
  }

  fieldToDateResolution.put(fieldName, dateResolution);
}
 
Example #10
Source File: LuceneContent.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获得Lucene格式的Document
 * 
 * @param c
 *            文章对象
 * @return
 */
public static Document createDocument(Content c) {
	Document doc = new Document();
	doc.add(new Field(ID, c.getId().toString(), Field.Store.YES,
			Field.Index.NOT_ANALYZED));
	doc.add(new Field(SITE_ID, c.getSite().getId().toString(),
			Field.Store.NO, Field.Index.NOT_ANALYZED));
	doc.add(new Field(RELEASE_DATE, DateTools.dateToString(c
			.getReleaseDate(), Resolution.DAY), Field.Store.NO,
			Field.Index.NOT_ANALYZED));
	Channel channel = c.getChannel();
	while (channel != null) {
		doc.add(new Field(CHANNEL_ID_ARRAY, channel.getId().toString(),
				Field.Store.NO, Field.Index.NOT_ANALYZED));
		channel = channel.getParent();
	}
	doc.add(new Field(TITLE, c.getTitle(), Field.Store.NO,
			Field.Index.ANALYZED));
	if (!StringUtils.isBlank(c.getTxt())) {
		doc.add(new Field(CONTENT, c.getTxt(), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	if(c.getAttr()!=null&&StringUtils.isNotBlank(c.getAttr().get("workplace"))){
		doc.add(new Field(WORKPLACE, c.getAttr().get("workplace"), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	if(c.getAttr()!=null&&StringUtils.isNotBlank(c.getAttr().get("category"))){
		doc.add(new Field(CATEGORY, c.getAttr().get("category"), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	return doc;
}
 
Example #11
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1, false);
  String endDate = getLocalizedDate(2002, 1, 4, false);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  PrecedenceQueryParser qp = new PrecedenceQueryParser(new MockAnalyzer(random()));

  Map<CharSequence, DateTools.Resolution> fieldMap = new HashMap<>();
  // set a field specific date resolution
  fieldMap.put(monthField, DateTools.Resolution.MONTH);
  qp.setDateResolutionMap(fieldMap);

  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);

  // set second field specific date resolution
  fieldMap.put(hourField, DateTools.Resolution.HOUR);
  qp.setDateResolutionMap(fieldMap);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.HOUR);
}
 
Example #12
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertDateRangeQueryEquals(CommonQueryParserConfiguration cqpC, String field, String startDate, String endDate, 
                                       Date endDateInclusive, DateTools.Resolution resolution) throws Exception {
  assertQueryEquals(cqpC, field, field + ":[" + escapeDateString(startDate) + " TO " + escapeDateString(endDate) + "]",
             "[" + getDate(startDate, resolution) + " TO " + getDate(endDateInclusive, resolution) + "]");
  assertQueryEquals(cqpC, field, field + ":{" + escapeDateString(startDate) + " TO " + escapeDateString(endDate) + "}",
             "{" + getDate(startDate, resolution) + " TO " + getDate(endDate, resolution) + "}");
}
 
Example #13
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1);
  String endDate = getLocalizedDate(2002, 1, 4);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.clear();
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  Analyzer a = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
  CommonQueryParserConfiguration qp = getParserConfig(a);
  
  // set a field specific date resolution
  setDateResolution(qp, monthField, DateTools.Resolution.MONTH);
  
  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);
  
  // set second field specific date resolution    
  setDateResolution(qp, hourField, DateTools.Resolution.HOUR);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate, 
          endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate, 
          endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate, 
          endDateExpected.getTime(), DateTools.Resolution.HOUR);  
}
 
Example #14
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertDateRangeQueryEquals(PrecedenceQueryParser qp, String field,
    String startDate, String endDate, Date endDateInclusive,
    DateTools.Resolution resolution) throws Exception {
  assertQueryEquals(qp, field, field + ":[" + escapeDateString(startDate)
      + " TO " + escapeDateString(endDate) + "]", "["
      + getDate(startDate, resolution) + " TO "
      + getDate(endDateInclusive, resolution) + "]");
  assertQueryEquals(qp, field, field + ":{" + escapeDateString(startDate)
      + " TO " + escapeDateString(endDate) + "}", "{"
      + getDate(startDate, resolution) + " TO "
      + getDate(endDate, resolution) + "}");
}
 
Example #15
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** for testing DateTools support */
private String getDate(String s, DateTools.Resolution resolution)
    throws Exception {
  // we use the default Locale since LuceneTestCase randomizes it
  DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  return getDate(df.parse(s), resolution);
}
 
Example #16
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1);
  String endDate = getLocalizedDate(2002, 1, 4);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.clear();
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  StandardQueryParser qp = new StandardQueryParser();

  Map<CharSequence, DateTools.Resolution> dateRes =  new HashMap<>();
  
  // set a field specific date resolution    
  dateRes.put(monthField, DateTools.Resolution.MONTH);
  qp.setDateResolutionMap(dateRes);

  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);

  // set second field specific date resolution
  dateRes.put(hourField, DateTools.Resolution.HOUR);
  qp.setDateResolutionMap(dateRes);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two
  // fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.HOUR);
}
 
Example #17
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertDateRangeQueryEquals(StandardQueryParser qp,
    String field, String startDate, String endDate, Date endDateInclusive,
    DateTools.Resolution resolution) throws Exception {
  assertQueryEquals(qp, field, field + ":[" + escapeDateString(startDate) + " TO " + escapeDateString(endDate)
      + "]", "[" + getDate(startDate, resolution) + " TO "
      + getDate(endDateInclusive, resolution) + "]");
  assertQueryEquals(qp, field, field + ":{" + escapeDateString(startDate) + " TO " + escapeDateString(endDate)
      + "}", "{" + getDate(startDate, resolution) + " TO "
      + getDate(endDate, resolution) + "}");
}
 
Example #18
Source File: WebDSLDateBridge.java    From webdsl with Apache License 2.0 5 votes vote down vote up
public Object stringToObject(String stringValue) {
    if ( StringHelper.isEmpty( stringValue ) ) return null;
    try {
        return DateTools.stringToDate( stringValue );
    }
    catch (ParseException e) {
        throw new SearchException( "Unable to parse into date: " + stringValue, e );
    }
}
 
Example #19
Source File: LuceneSearchEngine.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert a list of Lucene items into a list of generic search items
 * 
 * @param listSource The list of Lucene items
 * @return A list of generic search items
 */
private List<SearchResult> convertList( List<SearchItem> listSource )
{
    List<SearchResult> listDest = new ArrayList<>( );

    for ( SearchItem item : listSource )
    {
        SearchResult result = new SearchResult( );
        result.setId( item.getId( ) );

        try
        {
            result.setDate( DateTools.stringToDate( item.getDate( ) ) );
        }
        catch ( ParseException e )
        {
            AppLogService
                    .error( "Bad Date Format for indexed item \"" + item.getTitle( ) + "\" : " + e.getMessage( ) );
        }

        result.setUrl( item.getUrl( ) );
        result.setTitle( item.getTitle( ) );
        result.setSummary( item.getSummary( ) );
        result.setType( item.getType( ) );
        listDest.add( result );
    }

    return listDest;
}
 
Example #20
Source File: Server.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param checkinIn the checkin to set
 */
public void setCheckin(Date checkinIn) {
    if (checkinIn != null) {
        this.checkin = DateTools.dateToString(checkinIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.checkin = null;
    }
}
 
Example #21
Source File: Server.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param registeredIn the registered to set
 */
public void setRegistered(Date registeredIn) {
    if (registeredIn != null) {
        this.registered = DateTools.dateToString(registeredIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.registered = null;
    }
}
 
Example #22
Source File: QueryParserPaneProvider.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private JPanel initDateRangeQuerySettingsPanel() {
  JPanel panel = new JPanel();
  panel.setOpaque(false);
  panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

  JPanel header = new JPanel(new FlowLayout(FlowLayout.LEADING));
  header.setOpaque(false);
  header.add(new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.daterange_query")));
  panel.add(header);

  JPanel resolution = new JPanel(new FlowLayout(FlowLayout.LEADING));
  resolution.setOpaque(false);
  resolution.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
  JLabel resLabel = new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.date_res"));
  resolution.add(resLabel);
  Arrays.stream(DateTools.Resolution.values()).map(DateTools.Resolution::name).forEach(dateResCB::addItem);
  dateResCB.setSelectedItem(config.getDateResolution().name());
  dateResCB.setOpaque(false);
  resolution.add(dateResCB);
  panel.add(resolution);

  JPanel locale = new JPanel(new FlowLayout(FlowLayout.LEADING));
  locale.setOpaque(false);
  locale.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
  JLabel locLabel = new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.locale"));
  locale.add(locLabel);
  locationTF.setColumns(10);
  locationTF.setText(config.getLocale().toLanguageTag());
  locale.add(locationTF);
  JLabel tzLabel = new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.timezone"));
  locale.add(tzLabel);
  timezoneTF.setColumns(10);
  timezoneTF.setText(config.getTimeZone().getID());
  locale.add(timezoneTF);
  panel.add(locale);

  return panel;
}
 
Example #23
Source File: TrecContentSourceTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void assertDocData(DocData dd, String expName, String expTitle,
                           String expBody, Date expDate)
    throws ParseException {
  assertNotNull(dd);
  assertEquals(expName, dd.getName());
  assertEquals(expTitle, dd.getTitle());
  assertTrue(dd.getBody().indexOf(expBody) != -1);
  Date date = dd.getDate() != null ? DateTools.stringToDate(dd.getDate()) : null;
  assertEquals(expDate, date);
}
 
Example #24
Source File: DocData.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void setDate(Date date) {
  if (date != null) {
    setDate(DateTools.dateToString(date, DateTools.Resolution.SECOND));
  } else {
    this.date = null;
  }
}
 
Example #25
Source File: LuceneQueryVisitor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Query createRangeQuery(Class<?> cls, String name, Object value, ConditionType type) {

        boolean minInclusive = type == ConditionType.GREATER_OR_EQUALS || type == ConditionType.EQUALS;
        boolean maxInclusive = type == ConditionType.LESS_OR_EQUALS || type == ConditionType.EQUALS;

        if (String.class.isAssignableFrom(cls) || Number.class.isAssignableFrom(cls)) {
            Query query = null;

            if (Double.class.isAssignableFrom(cls)) {
                query = createDoubleRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Float.class.isAssignableFrom(cls)) {
                query = createFloatRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Long.class.isAssignableFrom(cls)) {
                query = createLongRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else {
                query = createIntRangeQuery(name, value, type, minInclusive, maxInclusive);
            }

            return query;
        } else if (Date.class.isAssignableFrom(cls)) {
            final Date date = getValue(Date.class, getFieldTypeConverter(), value.toString());
            final String luceneDateValue = getString(Date.class, getFieldTypeConverter(), date);

            if (type == ConditionType.LESS_THAN || type == ConditionType.LESS_OR_EQUALS) {
                return TermRangeQuery.newStringRange(name, null, luceneDateValue, minInclusive, maxInclusive);
            }
            return TermRangeQuery.newStringRange(name, luceneDateValue,
                DateTools.dateToString(new Date(), Resolution.MILLISECOND), minInclusive, maxInclusive);
        } else {
            return null;
        }
    }
 
Example #26
Source File: MtasDocumentIndex.java    From inception with Apache License 2.0 5 votes vote down vote up
private void indexDocument(String aDocumentTitle, long aSourceDocumentId,
        long aAnnotationDocumentId, String aUser, byte[] aBinaryCas)
    throws IOException
{
    // Calculate timestamp that will be indexed
    String timestamp = DateTools.dateToString(new Date(), DateTools.Resolution.MILLISECOND);
    
    log.trace(
            "Indexing document in project [{}]({}). sourceId: {}, annotationId: {}, "
            + "user: {} timestamp: {}",
            project.getName(), project.getId(), aSourceDocumentId, aAnnotationDocumentId,
            aUser, timestamp);
    
    IndexWriter indexWriter = getIndexWriter();
    
    // Prepare bytearray with document content to be indexed
    String encodedCAS = new String(MtasUtils.bytesToChars(aBinaryCas));

    // Create new Lucene document
    Document doc = new Document();
    
    // Add indexed fields
    doc.add(new StringField(FIELD_ID, String.valueOf(aSourceDocumentId) + "/"
            + String.valueOf(aAnnotationDocumentId), Field.Store.YES));
    doc.add(new StringField(FIELD_SOURCE_DOCUMENT_ID, String.valueOf(aSourceDocumentId),
            Field.Store.YES));
    doc.add(new StringField(FIELD_ANNOTATION_DOCUMENT_ID,
            String.valueOf(aAnnotationDocumentId), Field.Store.YES));
    doc.add(new StringField(FIELD_TITLE, aDocumentTitle, Field.Store.YES));
    doc.add(new StringField(FIELD_USER, aUser, Field.Store.YES));
    doc.add(new StringField(FIELD_TIMESTAMP, timestamp, Field.Store.YES));
    doc.add(new TextField(FIELD_CONTENT, encodedCAS, Field.Store.NO));

    // Add document to the Lucene index
    indexWriter.addDocument(doc);

    scheduleCommit();
}
 
Example #27
Source File: Server.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param registeredIn the registered to set
 */
public void setRegistered(Date registeredIn) {
    if (registeredIn != null) {
        this.registered = DateTools.dateToString(registeredIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.registered = null;
    }
}
 
Example #28
Source File: Server.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param checkinIn the checkin to set
 */
public void setCheckin(Date checkinIn) {
    if (checkinIn != null) {
        this.checkin = DateTools.dateToString(checkinIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.checkin = null;
    }
}
 
Example #29
Source File: TestCustomSearcherSort.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private String getLuceneDate() {
  return DateTools.timeToString(base.getTimeInMillis() + random.nextInt()
      - Integer.MIN_VALUE, DateTools.Resolution.DAY);
}
 
Example #30
Source File: DefaultParamConverterProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public String toString(final Date value) {
    return value != null ? DateTools.dateToString(value, Resolution.MILLISECOND) : null;
}