當前位置: 首頁>>代碼示例>>Java>>正文


Java StringField類代碼示例

本文整理匯總了Java中org.apache.lucene.document.StringField的典型用法代碼示例。如果您正苦於以下問題:Java StringField類的具體用法?Java StringField怎麽用?Java StringField使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


StringField類屬於org.apache.lucene.document包,在下文中一共展示了StringField類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: indexDoc

import org.apache.lucene.document.StringField; //導入依賴的package包/類
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
	try (InputStream stream = Files.newInputStream(file)) {
		Document doc = new Document();
		Field pathField = new StringField("path", file.toString(), Field.Store.YES);
		doc.add(pathField);
		doc.add(new LongPoint("modified", lastModified));
		doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));

		if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
			System.out.println("adding " + file);
			writer.addDocument(doc);
		} else {
			System.out.println("updating " + file);
			writer.updateDocument(new Term("path", file.toString()), doc);
		}
	}
}
 
開發者ID:PacktPublishing,項目名稱:Java-Data-Science-Cookbook,代碼行數:18,代碼來源:IndexFiles.java

示例2: setup

import org.apache.lucene.document.StringField; //導入依賴的package包/類
@BeforeClass
public static void setup() throws IOException {
    dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    final int numDocs = TestUtil.nextInt(random(), 1, 20);
    for (int i = 0; i < numDocs; ++i) {
        final int numHoles = random().nextInt(5);
        for (int j = 0; j < numHoles; ++j) {
            w.addDocument(new Document());
        }
        Document doc = new Document();
        doc.add(new StringField("foo", "bar", Store.NO));
        w.addDocument(doc);
    }
    reader = w.getReader();
    w.close();
    Engine.Searcher engineSearcher = new Engine.Searcher("test", new IndexSearcher(reader));
    searcher = new ContextIndexSearcher(engineSearcher, IndexSearcher.getDefaultQueryCache(), MAYBE_CACHE_POLICY);
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:QueryProfilerTests.java

示例3: testEmpty

import org.apache.lucene.document.StringField; //導入依賴的package包/類
public void testEmpty() throws Exception {
    Document d = new Document();
    d.add(new StringField("field", "value", Field.Store.NO));
    writer.addDocument(d);
    refreshReader();

    IndexFieldData fieldData = getForField("non_existing_field");
    int max = randomInt(7);
    for (LeafReaderContext readerContext : readerContexts) {
        AtomicFieldData previous = null;
        for (int i = 0; i < max; i++) {
            AtomicFieldData current = fieldData.load(readerContext);
            assertThat(current.ramBytesUsed(), equalTo(0L));
            if (previous != null) {
                assertThat(current, not(sameInstance(previous)));
            }
            previous = current;
        }
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:21,代碼來源:AbstractFieldDataTestCase.java

示例4: testCanOpenIndex

import org.apache.lucene.document.StringField; //導入依賴的package包/類
public void testCanOpenIndex() throws IOException {
    final ShardId shardId = new ShardId("index", "_na_", 1);
    IndexWriterConfig iwc = newIndexWriterConfig();
    Path tempDir = createTempDir();
    final BaseDirectoryWrapper dir = newFSDirectory(tempDir);
    assertFalse(Store.canOpenIndex(logger, tempDir, shardId, (id, l) -> new DummyShardLock(id)));
    IndexWriter writer = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new StringField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    writer.commit();
    writer.close();
    assertTrue(Store.canOpenIndex(logger, tempDir, shardId, (id, l) -> new DummyShardLock(id)));

    DirectoryService directoryService = new DirectoryService(shardId, INDEX_SETTINGS) {

        @Override
        public Directory newDirectory() throws IOException {
            return dir;
        }
    };
    Store store = new Store(shardId, INDEX_SETTINGS, directoryService, new DummyShardLock(shardId));
    store.markStoreCorrupted(new CorruptIndexException("foo", "bar"));
    assertFalse(Store.canOpenIndex(logger, tempDir, shardId, (id, l) -> new DummyShardLock(id)));
    store.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:27,代碼來源:StoreTests.java

示例5: createIndex

import org.apache.lucene.document.StringField; //導入依賴的package包/類
@Override
public void createIndex(NitriteId id, String field, String text) {
    try {
        Document document = new Document();
        String jsonId = keySerializer.writeValueAsString(id);
        Field contentField = new TextField(field, text, Field.Store.NO);
        Field idField = new StringField(CONTENT_ID, jsonId, Field.Store.YES);

        document.add(idField);
        document.add(contentField);

        synchronized (this) {
            indexWriter.addDocument(document);
            commit();
        }
    } catch (IOException ioe) {
        throw new IndexingException(errorMessage(
                "could not write full-text index data for " + text, 0), ioe);
    } catch (VirtualMachineError vme) {
        handleVirtualMachineError(vme);
    }
}
 
開發者ID:dizitart,項目名稱:nitrite-database,代碼行數:23,代碼來源:LuceneService.java

示例6: updateIndex

import org.apache.lucene.document.StringField; //導入依賴的package包/類
@Override
public void updateIndex(NitriteId id, String field, String text) {
    try {
        Document document = new Document();
        String jsonId = keySerializer.writeValueAsString(id);
        Field contentField = new TextField(field, text, Field.Store.NO);
        Field idField = new StringField(CONTENT_ID, jsonId, Field.Store.YES);

        document.add(idField);
        document.add(contentField);

        synchronized (this) {
            indexWriter.updateDocument(new Term(CONTENT_ID, jsonId), document);
            commit();
        }
    } catch (IOException ioe) {
        throw new IndexingException(errorMessage(
                "could not update full-text index for " + text, 0), ioe);
    } catch (VirtualMachineError vme) {
        handleVirtualMachineError(vme);
    }
}
 
開發者ID:dizitart,項目名稱:nitrite-database,代碼行數:23,代碼來源:LuceneService.java

示例7: startElement

import org.apache.lucene.document.StringField; //導入依賴的package包/類
@Override
public void startElement(String u, String l, String qName, Attributes attributes) throws SAXException {
    if (qName.equals("GGS:SpanAnnotation") || qName.equals("GGS:Annotation")) {
        Document doc = new Document();
        for (int i = 0; i < attributes.getLength(); i++) {
            doc.add(new StringField(attributes.getLocalName(i), attributes.getValue(i), Field.Store.YES));
        }
        try {
            annotationsWriter.addDocument(doc);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }

    if (qName.equals(sent) || nodesStack.size() > 0) {
        nodesStack.push(qName);
        segmentStartsStack.push(totalWords);
        if (attributes != null && attributes.getLength() > 0) {
            attributesStack.push(new AttributesImpl(attributes));
        } else {
            attributesStack.push(new AttributesImpl());
        }
        sb = new StringBuilder();
    }
}
 
開發者ID:radsimu,項目名稱:UaicNlpToolkit,代碼行數:27,代碼來源:IndexedLuceneCorpus.java

示例8: noteToDocument

import org.apache.lucene.document.StringField; //導入依賴的package包/類
private Document noteToDocument(Note note, String noteHtmlContents) {
	Document d = new Document ();
	
	String id = note.getId ();
	
	Project project = note.getProject();
	
	String projectId = project.getID();
	String projectName = project.getTitle();
	String title = note.getTitle ();		
	String date = note.getDate() != null ? new SimpleDateFormat ("yyyy-MM-dd").format(note.getDate().getDate()) : null;
	
	d.add (new StringField ("id", id, Field.Store.YES));
	d.add (new StringField ("project_id", projectId, Field.Store.YES));
	d.add (new StoredField ("project_name", projectName));
	d.add (new TextField ("title", title, Field.Store.YES));
	d.add (new TextField ("title_cs", title, Field.Store.YES));
	d.add (new TextField ("date", date != null ? date : "", Field.Store.YES));
	d.add (new TextField ("body", noteHtmlContents, Field.Store.YES));
			
	return d;
}
 
開發者ID:ser316asu,項目名稱:Reinickendorf_SER316,代碼行數:23,代碼來源:NoteIndexer.java

示例9: eventToDocument

import org.apache.lucene.document.StringField; //導入依賴的package包/類
private Document eventToDocument(Event newEvent) {
	Document d = new Document ();
	
	String eventId = newEvent.getId ();
	String eventText = newEvent.getText();
	
	String eventStartDate = newEvent.getStartDate() != null ? new SimpleDateFormat ("yyyy-MM-dd").format(newEvent.getStartDate().getDate()) : null;
	
	String eventStartTime = newEvent.getTimeString();
	
	if (eventStartDate != null) eventStartTime = eventStartDate + " @ " + eventStartTime;

	d.add (new StringField ("id", eventId, Field.Store.YES));
	d.add (new TextField ("text", eventText, Field.Store.YES));
	d.add (new TextField ("text_cs", eventText, Field.Store.YES));
	d.add (new StoredField ("original_start_date", eventStartTime != null ? eventStartTime : ""));
	return d;
}
 
開發者ID:ser316asu,項目名稱:Reinickendorf_SER316,代碼行數:19,代碼來源:EventIndexer.java

示例10: addToDoc

import org.apache.lucene.document.StringField; //導入依賴的package包/類
void addToDoc(Document doc, String... values){
  Preconditions.checkArgument(valueType == String.class);
  if (isSorted()) {
    Preconditions.checkArgument(values.length < 2, "sorted fields cannot have multiple values");
  }

  // add distinct elements to doc
  final Iterable<String> nonNull = FluentIterable.from(Arrays.asList(values))
      .filter(new Predicate<String>() {
        @Override
        public boolean apply(@Nullable final String input) {
          return input != null;
        }
      });

  for (final String value : ImmutableSet.copyOf(nonNull)) {
    final String truncatedValue = StringUtils.abbreviate(value, MAX_STRING_LENGTH);
    doc.add(new StringField(indexFieldName, truncatedValue, stored ? Store.YES : Store.NO));
  }

  if (isSorted() && values.length == 1) {
    Preconditions.checkArgument(sortedValueType == SearchFieldSorting.FieldType.STRING);
    doc.add(new SortedDocValuesField(indexFieldName, new BytesRef(values[0])));
  }
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:26,代碼來源:IndexKey.java

示例11: run

import org.apache.lucene.document.StringField; //導入依賴的package包/類
@Override
public void run() {
  try {
    for (int i = 0; i < 10000; ++i) {
      final Document document = new Document();
      final String key = "key" + i;
      final String val = "value" + i;
      document.add(new StringField(key, val, Field.Store.YES));
      document.add(new SortedDocValuesField(key, new BytesRef(val.getBytes())));
      index.add(document);
      data.put(key, val);
      sleep(1);
    }
  } catch (InterruptedException e) {
  }
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:17,代碼來源:TestLuceneIndexer.java

示例12: addIndex

import org.apache.lucene.document.StringField; //導入依賴的package包/類
public void addIndex(UUser user) throws Exception {
  IndexWriter writer = getWriter();
  Document doc = new Document();
  /*
 * yes是會將數據存進索引,如果查詢結果中需要將記錄顯示出來就要存進去,如果查詢結果
 * 隻是顯示標題之類的就可以不用存,而且內容過長不建議存進去
 * 使用TextField類是可以用於查詢的。
 */
  try {
    doc.add(new StringField("userid", String.valueOf(user.getId()), Field.Store.YES));
    doc.add(new TextField("username", user.getUsername(), Field.Store.YES));

    writer.addDocument(doc);
  } catch (Exception e) {
    e.printStackTrace();
    throw e;
  } finally {
    writer.close();
  }
}
 
開發者ID:MiniPa,項目名稱:cjs_ssms,代碼行數:21,代碼來源:LuceneIndex.java

示例13: initializeProfanitySet

import org.apache.lucene.document.StringField; //導入依賴的package包/類
/**
 * Initializes profanity set.
 * 
 * @param dictFilePath
 *          dictionary file path
 */
private void initializeProfanitySet(String dictFilePath) {
  if (dictFilePath != null) {
    File file = new File(dictFilePath);
    if (file.exists() && file.isFile()) {
      try {
        IndexWriterConfig config = new IndexWriterConfig(LUCENE_VERSION, analyzer);
        IndexWriter indexWriter = new IndexWriter(directory, config);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        Set<String> bannedWords = new HashSet<String>();
        String line = null;
        while ((line = reader.readLine()) != null) {
          bannedWords.add(line.trim());
          Document doc = new Document();
          doc.add(new StringField(LUCENE_FIELD_NAME, line, Store.NO));
          indexWriter.addDocument(doc);
        }
        this.bannedWords = bannedWords;
        indexWriter.close();
        reader.close();
      } catch (Exception ex) {
        LOG.error("Error reading file", ex);
      }
    }
  }
}
 
開發者ID:sematext,項目名稱:related-searches,代碼行數:32,代碼來源:AbstractProfanityRemovingOutputWriter.java

示例14: InMemoryIndex

import org.apache.lucene.document.StringField; //導入依賴的package包/類
public InMemoryIndex(Map<String,String> id2Text){
    Analyzer analyzer = new EnglishAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    try {
        IndexWriter writer = new IndexWriter(directory, iwc);
        for (String id:id2Text.keySet()) {
            Document doc=new Document();
            doc.add(new StringField("id", id, Field.Store.YES));
            doc.add(new TextField("content", id2Text.get(id), Field.Store.YES));
            writer.addDocument(doc);
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
開發者ID:linzeqipku,項目名稱:SnowGraph,代碼行數:18,代碼來源:InMemoryIndex.java

示例15: transform

import org.apache.lucene.document.StringField; //導入依賴的package包/類
@Override
public Document transform(final Example input) throws TransformException {
    final Document doc = new Document();

    doc.add(new Field(ExampleField.ID.getName(), input.getId(), StringField.TYPE_STORED));
    doc.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef(input.getId())));

    doc.add(new Field(ExampleField.TITLE.getName(), input.getTitle(), TextField.TYPE_STORED));
    doc.add(new Field(ExampleField.BODY.getName(), input.getBody(), TextField.TYPE_STORED));

    doc.add(new Field(ExampleField.COLOR.getName(), input.getColor(), StringField.TYPE_STORED));
    doc.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), input.getColor()));

    final Date createDate = input.getCreateDate();
    doc.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate.getTime()));
    doc.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate.getTime()));

    return doc;
}
 
開發者ID:bbende,項目名稱:tripod,代碼行數:20,代碼來源:ExampleIndexTransformer.java


注:本文中的org.apache.lucene.document.StringField類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。