List of usage examples for org.apache.lucene.document StoredField StoredField
public StoredField(String name, double value)
From source file:com.shaie.annots.example.AnnotatorTeeSinkFilterExample.java
License:Apache License
@SuppressWarnings("resource") private static void addDocument(IndexWriter writer, String text) throws IOException { final Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(text)); final TeeSinkTokenFilter textStream = new TeeSinkTokenFilter(tokenizer); final TokenStream colorsStream = new AnnotatorTokenFilter(textStream.newSinkTokenStream(), ColorAnnotator.withDefaultColors()); final TokenStream animalsStream = new AnnotatorTokenFilter(textStream.newSinkTokenStream(), AnimalAnnotator.withDefaultAnimals()); final Document doc = new Document(); doc.add(new StoredField(TEXT_FIELD, text)); doc.add(new TextField(TEXT_FIELD, textStream)); doc.add(new TextField(COLOR_FIELD, colorsStream)); doc.add(new TextField(ANIMAL_FIELD, animalsStream)); writer.addDocument(doc);//from w w w . java 2 s . c o m }
From source file:com.shaie.annots.example.PreAnnotatedTokenFilterExample.java
License:Apache License
@SuppressWarnings("resource") private static void addDocument(IndexWriter writer, String text, int... colorAnnotations) throws IOException { final Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(text)); final TeeSinkTokenFilter textStream = new TeeSinkTokenFilter(tokenizer); final TokenStream colorsStream = new PreAnnotatedTokenFilter(textStream.newSinkTokenStream(), colorAnnotations);/*from w ww . j a va2 s .c om*/ final Document doc = new Document(); doc.add(new StoredField(TEXT_FIELD, text)); doc.add(new TextField(TEXT_FIELD, textStream)); doc.add(new TextField(COLOR_FIELD, colorsStream)); writer.addDocument(doc); }
From source file:com.shaie.annots.example.SimplePreAnnotatedTokenFilterExample.java
License:Apache License
@SuppressWarnings("resource") private static void addDocument(IndexWriter writer, String text, int... colorAnnotations) throws IOException { final Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(text)); final TeeSinkTokenFilter textStream = new TeeSinkTokenFilter(tokenizer); final TokenStream colorsStream = new AnyAnnotationTokenFilter( new SimplePreAnnotatedTokenFilter(textStream.newSinkTokenStream(), colorAnnotations)); final Document doc = new Document(); doc.add(new StoredField(TEXT_FIELD, text)); doc.add(new TextField(TEXT_FIELD, textStream)); doc.add(new TextField(COLOR_FIELD, colorsStream)); writer.addDocument(doc);/*from ww w . j a va 2 s.co m*/ }
From source file:com.shaie.facet.NotDrillDownExample.java
License:Apache License
private static void createIndex() throws IOException { try (Analyzer analyzer = new WhitespaceAnalyzer(); IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(analyzer)); TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir)) { for (final String author : new String[] { "Bob", "Lisa" }) { final Document doc = new Document(); doc.add(new FacetField(AUTHOR_FACET, author)); doc.add(new StoredField(AUTHOR_FACET, author)); indexWriter.addDocument(config.build(taxoWriter, doc)); }/*ww w. j a v a 2s. c o m*/ } }
From source file:com.tripod.lucene.example.TestExampleLuceneBase.java
License:Apache License
@Before public void setupBase() throws IOException, ParseException { analyzer = new StandardAnalyzer(); directory = new RAMDirectory(); facetsConfig = new FacetsConfig(); facetsConfig.setIndexFieldName(ExampleField.COLOR.getName(), ExampleField.COLOR.getName()); IndexWriterConfig config = new IndexWriterConfig(analyzer); try (IndexWriter writer = new IndexWriter(directory, config)) { final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); // Doc1/*from www .j a v a 2 s .com*/ Document doc1 = new Document(); doc1.add(new Field(ExampleField.ID.getName(), "1", StringField.TYPE_STORED)); doc1.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("1"))); doc1.add(new Field(ExampleField.TITLE.getName(), "Title 1", TextField.TYPE_STORED)); doc1.add(new Field(ExampleField.BODY.getName(), "Body 1 Solr is cool", TextField.TYPE_STORED)); doc1.add(new Field(ExampleField.COLOR.getName(), "BLUE", StringField.TYPE_STORED)); doc1.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "BLUE")); Date createDate1 = dateFormat.parse("2016-10-01T01:00:00Z"); doc1.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate1.getTime())); doc1.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate1.getTime())); writer.addDocument(facetsConfig.build(doc1)); // Doc2 Document doc2 = new Document(); doc2.add(new Field(ExampleField.ID.getName(), "2", StringField.TYPE_STORED)); doc2.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("2"))); doc2.add(new Field(ExampleField.TITLE.getName(), "Title 2", TextField.TYPE_STORED)); doc2.add(new Field(ExampleField.BODY.getName(), "Body 2 Lucene is cool", TextField.TYPE_STORED)); doc2.add(new Field(ExampleField.COLOR.getName(), "RED", StringField.TYPE_STORED)); doc2.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "RED")); Date createDate2 = dateFormat.parse("2016-10-01T02:00:00Z"); doc2.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate2.getTime())); doc2.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate2.getTime())); writer.addDocument(facetsConfig.build(doc2)); // Doc3 Document doc3 = new Document(); doc3.add(new Field(ExampleField.ID.getName(), "3", StringField.TYPE_STORED)); doc3.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("3"))); doc3.add(new Field(ExampleField.TITLE.getName(), "Title 3", TextField.TYPE_STORED)); doc3.add(new Field(ExampleField.BODY.getName(), "Body 3 Solr is Great, Solr is Fun", TextField.TYPE_STORED)); doc3.add(new Field(ExampleField.COLOR.getName(), "GREEN", StringField.TYPE_STORED)); doc3.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "GREEN")); Date createDate3 = dateFormat.parse("2016-10-01T03:00:00Z"); doc3.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate3.getTime())); doc3.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate3.getTime())); writer.addDocument(facetsConfig.build(doc3)); // Doc4 Document doc4 = new Document(); doc4.add(new Field(ExampleField.ID.getName(), "4", StringField.TYPE_STORED)); doc4.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("4"))); doc4.add(new Field(ExampleField.TITLE.getName(), "Title 4", TextField.TYPE_STORED)); doc4.add(new Field(ExampleField.BODY.getName(), "Body 4", TextField.TYPE_STORED)); doc4.add(new Field(ExampleField.COLOR.getName(), "BLUE", StringField.TYPE_STORED)); doc4.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "BLUE")); Date createDate4 = dateFormat.parse("2016-10-01T04:00:00Z"); doc4.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate4.getTime())); doc4.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate4.getTime())); writer.addDocument(facetsConfig.build(doc4)); // Doc5 Document doc5 = new Document(); doc5.add(new Field(ExampleField.ID.getName(), "5", StringField.TYPE_STORED)); doc5.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("5"))); doc5.add(new Field(ExampleField.TITLE.getName(), "Title 5", TextField.TYPE_STORED)); doc5.add(new Field(ExampleField.BODY.getName(), "Body 5", TextField.TYPE_STORED)); doc5.add(new Field(ExampleField.COLOR.getName(), "RED", StringField.TYPE_STORED)); doc5.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "RED")); Date createDate5 = dateFormat.parse("2016-10-01T05:00:00Z"); doc5.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate5.getTime())); doc5.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate5.getTime())); writer.addDocument(facetsConfig.build(doc5)); // commit docs writer.commit(); } // needs to be opened after the writer is closed otherwise it won't see the test data searcherManager = new SearcherManager(directory, null); }
From source file:com.tripod.lucene.example.TestExampleSummaryQueryService.java
License:Apache License
@Test public void testRefreshingSearcherManager() throws IOException, ParseException, QueryException, InterruptedException { // Add a new document final IndexWriterConfig config = new IndexWriterConfig(analyzer); try (IndexWriter writer = new IndexWriter(directory, config)) { final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); Document doc = new Document(); doc.add(new Field(ExampleField.ID.getName(), "99", StringField.TYPE_STORED)); doc.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("1"))); doc.add(new Field(ExampleField.TITLE.getName(), "Title 99", TextField.TYPE_STORED)); doc.add(new Field(ExampleField.BODY.getName(), "Body 99", TextField.TYPE_STORED)); doc.add(new Field(ExampleField.COLOR.getName(), "BLUE", StringField.TYPE_STORED)); doc.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "BLUE")); Date createDate1 = dateFormat.parse("2016-11-01T01:00:00Z"); doc.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate1.getTime())); doc.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate1.getTime())); writer.addDocument(facetsConfig.build(doc)); writer.commit();/*from www. j a v a2s. com*/ } // Query for the new document and shouldn't get it LuceneQuery query = new LuceneQuery("id:99"); QueryResults<ExampleSummary> results = queryService.search(query); assertNotNull(results); assertNotNull(results.getResults()); assertEquals(0, results.getResults().size()); // Start a refresher for the SearchManager SearcherManagerRefresher refresher = new SearcherManagerRefresher(searcherManager, 2000); try { // Start the refresher and then wait slightly longer than refresh interval refresher.start(); Thread.sleep(3000); // Query again should get a result now query = new LuceneQuery("id:99"); results = queryService.search(query); assertNotNull(results); assertNotNull(results.getResults()); assertEquals(1, results.getResults().size()); } finally { refresher.stop(); } }
From source file:com.vmware.dcp.services.common.LuceneDocumentIndexService.java
License:Open Source License
protected void handleDocumentPostOrDelete(Operation postOrDelete) throws Throwable { UpdateIndexRequest r = postOrDelete.getBody(UpdateIndexRequest.class); ServiceDocument s = r.document;/*from w w w . j a v a 2 s. c o m*/ ServiceDocumentDescription desc = r.description; if (postOrDelete.isRemote()) { postOrDelete.fail(new IllegalStateException("Remote requests not allowed")); return; } if (s == null) { postOrDelete.fail(new IllegalArgumentException("document is required")); return; } String link = s.documentSelfLink; if (link == null) { postOrDelete.fail(new IllegalArgumentException("documentSelfLink is required")); return; } if (s.documentUpdateAction == null) { postOrDelete.fail(new IllegalArgumentException("documentUpdateAction is required")); return; } if (desc == null) { postOrDelete.fail(new IllegalArgumentException("description is required")); return; } s.documentDescription = null; Document doc = new Document(); Field refererField = new StringField(LUCENE_FIELD_NAME_REFERER, postOrDelete.getReferer().toString(), Field.Store.NO); doc.add(refererField); Field updateActionField = new StoredField(ServiceDocument.FIELD_NAME_UPDATE_ACTION, s.documentUpdateAction); doc.add(updateActionField); addBinaryStateFieldToDocument(s, desc, doc); Field selfLinkField = new StringField(ServiceDocument.FIELD_NAME_SELF_LINK, link.intern(), Field.Store.YES); doc.add(selfLinkField); Field sortedSelfLinkField = new SortedDocValuesField(ServiceDocument.FIELD_NAME_SELF_LINK, new BytesRef(link.intern().toString())); doc.add(sortedSelfLinkField); if (s.documentKind != null) { Field kindField = new StringField(ServiceDocument.FIELD_NAME_KIND, s.documentKind, Field.Store.NO); doc.add(kindField); } if (s.documentAuthPrincipalLink != null) { Field principalField = new StringField(ServiceDocument.FIELD_NAME_AUTH_PRINCIPAL_LINK, s.documentAuthPrincipalLink, Field.Store.NO); doc.add(principalField); } if (s.documentTransactionId != null) { Field transactionField = new StringField(ServiceDocument.FIELD_NAME_TRANSACTION_ID, s.documentTransactionId, Field.Store.NO); doc.add(transactionField); } Field timestampField = new LongField(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, s.documentUpdateTimeMicros, this.longStoredField); doc.add(timestampField); if (s.documentExpirationTimeMicros > 0) { Field expirationTimeMicrosField = new LongField(ServiceDocument.FIELD_NAME_EXPIRATION_TIME_MICROS, s.documentExpirationTimeMicros, this.longStoredField); doc.add(expirationTimeMicrosField); } Field versionField = new LongField(ServiceDocument.FIELD_NAME_VERSION, s.documentVersion, this.longStoredField); doc.add(versionField); if (desc.propertyDescriptions == null || desc.propertyDescriptions.isEmpty()) { // no additional property type information, so we will add the // document with common fields indexed plus the full body addDocumentToIndex(postOrDelete, doc, s, desc); return; } addIndexableFieldsToDocument(doc, s, desc); addDocumentToIndex(postOrDelete, doc, s, desc); if (hasOption(ServiceOption.INSTRUMENTATION)) { int fieldCount = doc.getFields().size(); ServiceStat st = getStat(STAT_NAME_INDEXED_FIELD_COUNT); adjustStat(st, fieldCount); st = getHistogramStat(STAT_NAME_FIELD_COUNT_PER_DOCUMENT); setStat(st, fieldCount); } }
From source file:com.vmware.demo.sgf.lucene.impl.LuceneGemFireRepositoryImpl.java
License:Apache License
@Override public Object save(Object entity) { if (!(entity instanceof SearchableGemFireEntity)) throw new IllegalArgumentException( "Can only accept SearchableGemFireEntity data type for this repository."); SearchableGemFireEntity gfEntity = (SearchableGemFireEntity) entity; if (searchableFields == null) setupAttributeReflection(gfEntity.getValue()); try {// ww w . j av a2s .co m Document doc = new Document(); // Add reflected fields to document iff search value is not null for (java.lang.reflect.Field f : searchableFields) { String fieldName = f.getName().intern(); String searchText = (String) f.get(gfEntity.getValue()); if (searchText != null) doc.add(new Field(fieldName, searchText, TextField.TYPE_STORED)); } doc.add(new Field(savedField, savedFieldValue, TextField.TYPE_STORED)); doc.add(new StoredField(GEMFIRE_KEY, new BytesRef(ObjectSerializer.serialize(gfEntity.getKey())))); indexWriter.addDocument(doc); searchManager.maybeRefresh(); indexWriter.commit(); } catch (IOException e) { gfEntity = null; e.printStackTrace(); } catch (Exception e) { gfEntity = null; e.printStackTrace(); } return gfEntity; }
From source file:com.vmware.demo.sgf.lucene.impl.LuceneGemFireRepositoryImpl.java
License:Apache License
@Override public Iterable save(Iterable entities) { boolean failed = false; try {//from w w w . j a v a 2 s.co m for (Object entity : entities) { if (!(entity instanceof SearchableGemFireEntity)) throw new IllegalArgumentException( "Can only accept SearchableGemFireEntity data type for this repository."); SearchableGemFireEntity gfEntity = (SearchableGemFireEntity) entity; if (searchableFields == null) setupAttributeReflection(gfEntity.getValue()); Document doc = new Document(); // Add reflected fields to document iff search value is not null for (java.lang.reflect.Field f : searchableFields) { String fieldName = f.getName().intern(); String searchText = (String) f.get(gfEntity.getValue()); if (searchText != null) doc.add(new Field(fieldName, searchText, TextField.TYPE_STORED)); } doc.add(new Field(savedField, savedFieldValue, TextField.TYPE_STORED)); doc.add(new StoredField(GEMFIRE_KEY, new BytesRef(ObjectSerializer.serialize(gfEntity.getKey())))); // StraightBytesDocValuesField indexWriter.addDocument(doc); } searchManager.maybeRefresh(); indexWriter.commit(); } catch (IOException e) { e.printStackTrace(); try { indexWriter.rollback(); } catch (IOException e1) { e1.printStackTrace(); } failed = true; } catch (Exception e) { e.printStackTrace(); try { indexWriter.rollback(); } catch (IOException e1) { e1.printStackTrace(); } failed = true; } return (failed) ? null : entities; }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java
License:Open Source License
protected void updateIndex(Operation updateOp) throws Throwable { UpdateIndexRequest r = updateOp.getBody(UpdateIndexRequest.class); ServiceDocument s = r.document;//from ww w . j a v a 2 s . c o m ServiceDocumentDescription desc = r.description; if (updateOp.isRemote()) { updateOp.fail(new IllegalStateException("Remote requests not allowed")); return; } if (s == null) { updateOp.fail(new IllegalArgumentException("document is required")); return; } String link = s.documentSelfLink; if (link == null) { updateOp.fail(new IllegalArgumentException("documentSelfLink is required")); return; } if (s.documentUpdateAction == null) { updateOp.fail(new IllegalArgumentException("documentUpdateAction is required")); return; } if (desc == null) { updateOp.fail(new IllegalArgumentException("description is required")); return; } s.documentDescription = null; Document doc = new Document(); Field updateActionField = new StoredField(ServiceDocument.FIELD_NAME_UPDATE_ACTION, s.documentUpdateAction); doc.add(updateActionField); addBinaryStateFieldToDocument(s, r.serializedDocument, desc, doc); Field selfLinkField = new StringField(ServiceDocument.FIELD_NAME_SELF_LINK, link, Field.Store.YES); doc.add(selfLinkField); Field sortedSelfLinkField = new SortedDocValuesField(ServiceDocument.FIELD_NAME_SELF_LINK, new BytesRef(link)); doc.add(sortedSelfLinkField); String kind = s.documentKind; if (kind != null) { Field kindField = new StringField(ServiceDocument.FIELD_NAME_KIND, kind, Field.Store.NO); doc.add(kindField); } if (s.documentAuthPrincipalLink != null) { Field principalField = new StringField(ServiceDocument.FIELD_NAME_AUTH_PRINCIPAL_LINK, s.documentAuthPrincipalLink, Field.Store.NO); doc.add(principalField); } if (s.documentTransactionId != null) { Field transactionField = new StringField(ServiceDocument.FIELD_NAME_TRANSACTION_ID, s.documentTransactionId, Field.Store.NO); doc.add(transactionField); } addNumericField(doc, ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, s.documentUpdateTimeMicros, true); if (s.documentExpirationTimeMicros > 0) { addNumericField(doc, ServiceDocument.FIELD_NAME_EXPIRATION_TIME_MICROS, s.documentExpirationTimeMicros, true); } addNumericField(doc, ServiceDocument.FIELD_NAME_VERSION, s.documentVersion, true); if (desc.propertyDescriptions == null || desc.propertyDescriptions.isEmpty()) { // no additional property type information, so we will add the // document with common fields indexed plus the full body addDocumentToIndex(updateOp, doc, s, desc); return; } addIndexableFieldsToDocument(doc, s, desc); addDocumentToIndex(updateOp, doc, s, desc); if (hasOption(ServiceOption.INSTRUMENTATION)) { int fieldCount = doc.getFields().size(); ServiceStat st = getStat(STAT_NAME_INDEXED_FIELD_COUNT); adjustStat(st, fieldCount); st = getHistogramStat(STAT_NAME_FIELD_COUNT_PER_DOCUMENT); setStat(st, fieldCount); } }