Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/LuceneIndex.java @ 621:fd15da41afca
allow list of values to be stored in lucene
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 06 Jan 2016 21:41:33 -0700 |
parents | 89eb02f9827f |
children | 1a53333eb4d5 |
comparison
equal
deleted
inserted
replaced
620:89eb02f9827f | 621:fd15da41afca |
---|---|
442 | 442 |
443 }; | 443 }; |
444 | 444 |
445 | 445 |
446 | 446 |
447 private IndexableField newField(LuanState luan,String name,Object value,Field.Store store,Set<String> indexed) | |
448 throws LuanException | |
449 { | |
450 if( value instanceof String ) { | |
451 String s = (String)value; | |
452 FieldParser fp = mfp.fields.get(name); | |
453 if( fp != null ) { | |
454 if( fp instanceof StringFieldParser && fp != STRING_FIELD_PARSER ) { | |
455 return new TextField(name, s, store); | |
456 } else { | |
457 return new StringField(name, s, store); | |
458 } | |
459 } else { | |
460 return new StoredField(name, s); | |
461 } | |
462 } else if( value instanceof Integer ) { | |
463 int i = (Integer)value; | |
464 if( indexed.contains(name) ) { | |
465 return new IntField(name, i, store); | |
466 } else { | |
467 return new StoredField(name, i); | |
468 } | |
469 } else if( value instanceof Long ) { | |
470 long i = (Long)value; | |
471 if( indexed.contains(name) ) { | |
472 return new LongField(name, i, store); | |
473 } else { | |
474 return new StoredField(name, i); | |
475 } | |
476 } else if( value instanceof Double ) { | |
477 double i = (Double)value; | |
478 if( indexed.contains(name) ) { | |
479 return new DoubleField(name, i, store); | |
480 } else { | |
481 return new StoredField(name, i); | |
482 } | |
483 } else if( value instanceof byte[] ) { | |
484 byte[] b = (byte[])value; | |
485 return new StoredField(name, b); | |
486 } else | |
487 throw new LuanException(luan,"invalid value type "+value.getClass()+"' for '"+name+"'"); | |
488 } | |
447 | 489 |
448 private Document toLucene(LuanState luan,LuanTable table,Set indexOnly) throws LuanException { | 490 private Document toLucene(LuanState luan,LuanTable table,Set indexOnly) throws LuanException { |
449 Set<String> indexed = mfp.fields.keySet(); | 491 Set<String> indexed = mfp.fields.keySet(); |
450 Document doc = new Document(); | 492 Document doc = new Document(); |
451 for( Map.Entry<Object,Object> entry : table.iterable(luan) ) { | 493 for( Map.Entry<Object,Object> entry : table.iterable(luan) ) { |
453 if( !(key instanceof String) ) | 495 if( !(key instanceof String) ) |
454 throw new LuanException(luan,"key must be string"); | 496 throw new LuanException(luan,"key must be string"); |
455 String name = (String)key; | 497 String name = (String)key; |
456 Object value = entry.getValue(); | 498 Object value = entry.getValue(); |
457 Field.Store store = indexOnly.contains(name) ? Field.Store.NO : Field.Store.YES; | 499 Field.Store store = indexOnly.contains(name) ? Field.Store.NO : Field.Store.YES; |
458 if( value instanceof String ) { | 500 if( !(value instanceof LuanTable) ) { |
459 String s = (String)value; | 501 doc.add(newField(luan, name, value, store, indexed)); |
460 FieldParser fp = mfp.fields.get(name); | 502 } else { // list |
461 if( fp != null ) { | 503 LuanTable list = (LuanTable)value; |
462 if( fp instanceof StringFieldParser && fp != STRING_FIELD_PARSER ) { | 504 for( Object el : list.asList() ) { |
463 doc.add(new TextField(name, s, store)); | 505 doc.add(newField(luan, name, el, store, indexed)); |
464 } else { | |
465 doc.add(new StringField(name, s, store)); | |
466 } | |
467 } else { | |
468 doc.add(new StoredField(name, s)); | |
469 } | 506 } |
470 } else if( value instanceof Integer ) { | 507 } |
471 int i = (Integer)value; | |
472 if( indexed.contains(name) ) { | |
473 doc.add(new IntField(name, i, store)); | |
474 } else { | |
475 doc.add(new StoredField(name, i)); | |
476 } | |
477 } else if( value instanceof Long ) { | |
478 long i = (Long)value; | |
479 if( indexed.contains(name) ) { | |
480 doc.add(new LongField(name, i, store)); | |
481 } else { | |
482 doc.add(new StoredField(name, i)); | |
483 } | |
484 } else if( value instanceof Double ) { | |
485 double i = (Double)value; | |
486 if( indexed.contains(name) ) { | |
487 doc.add(new DoubleField(name, i, store)); | |
488 } else { | |
489 doc.add(new StoredField(name, i)); | |
490 } | |
491 } else if( value instanceof byte[] ) { | |
492 byte[] b = (byte[])value; | |
493 doc.add(new StoredField(name, b)); | |
494 } else | |
495 throw new LuanException(luan,"invalid value type "+value.getClass()+"' for '"+name+"'"); | |
496 } | 508 } |
497 return doc; | 509 return doc; |
510 } | |
511 | |
512 private static Object getValue(LuanState luan,IndexableField ifld) throws LuanException { | |
513 BytesRef br = ifld.binaryValue(); | |
514 if( br != null ) | |
515 return br.bytes; | |
516 Number n = ifld.numericValue(); | |
517 if( n != null ) | |
518 return n; | |
519 String s = ifld.stringValue(); | |
520 if( s != null ) | |
521 return s; | |
522 throw new LuanException(luan,"invalid field type for "+ifld); | |
498 } | 523 } |
499 | 524 |
500 private static LuanTable toTable(LuanState luan,Document doc) throws LuanException { | 525 private static LuanTable toTable(LuanState luan,Document doc) throws LuanException { |
501 if( doc==null ) | 526 if( doc==null ) |
502 return null; | 527 return null; |
503 LuanTable table = new LuanTable(); | 528 LuanTable table = new LuanTable(); |
504 for( IndexableField ifld : doc ) { | 529 for( IndexableField ifld : doc ) { |
505 String name = ifld.name(); | 530 String name = ifld.name(); |
506 BytesRef br = ifld.binaryValue(); | 531 Object value = getValue(luan,ifld); |
507 if( br != null ) { | 532 Object old = table.rawGet(name); |
508 table.rawPut(name,br.bytes); | 533 if( old == null ) { |
509 continue; | 534 table.rawPut(name,value); |
510 } | 535 } else { |
511 Number n = ifld.numericValue(); | 536 LuanTable list; |
512 if( n != null ) { | 537 if( old instanceof LuanTable ) { |
513 table.rawPut(name,n); | 538 list = (LuanTable)old; |
514 continue; | 539 } else { |
515 } | 540 list = new LuanTable(); |
516 String s = ifld.stringValue(); | 541 list.rawPut(1,old); |
517 if( s != null ) { | 542 table.rawPut(name,list); |
518 table.rawPut(name,s); | 543 } |
519 continue; | 544 list.rawPut(list.rawLength()+1,value); |
520 } | 545 } |
521 throw new LuanException(luan,"invalid field type for "+ifld); | |
522 } | 546 } |
523 return table; | 547 return table; |
524 } | 548 } |
525 | 549 |
526 } | 550 } |