List of usage examples for org.apache.lucene.util BytesRef BytesRef
public BytesRef(CharSequence text)
From source file:com.stratio.cassandra.lucene.service.ClusteringKeyMapper.java
License:Apache License
/** * Adds to the specified document the clustering key contained in the specified cell name. * * @param document The document where the clustering key is going to be added. * @param cellName A cell name containing the clustering key to be added. *//*from w w w .j av a 2 s .c o m*/ public void addFields(Document document, CellName cellName) { String serializedKey = ByteBufferUtils.toString(cellName.toByteBuffer()); BytesRef bytesRef = new BytesRef(serializedKey); document.add(new StringField(FIELD_NAME, serializedKey, Field.Store.YES)); document.add(new SortedDocValuesField(FIELD_NAME, bytesRef)); }
From source file:com.stratio.cassandra.lucene.service.LuceneIndexTest.java
License:Apache License
@Test public void testCRUD() throws IOException, InterruptedException { Path path = Paths.get(folder.newFolder("directory" + UUID.randomUUID()).getPath()); LuceneIndex index = new LuceneIndex("ks", "cf", "idx", path, IndexConfig.DEFAULT_RAM_BUFFER_MB, IndexConfig.DEFAULT_MAX_MERGE_MB, IndexConfig.DEFAULT_MAX_CACHED_MB, new StandardAnalyzer(), REFRESH_SECONDS, null);// w w w . ja va2 s. c o m Sort sort = new Sort(new SortField("field", SortField.Type.STRING)); assertEquals(0, index.getNumDocs()); Term term1 = new Term("field", "value1"); Document document1 = new Document(); document1.add(new StringField("field", "value1", Field.Store.NO)); document1.add(new SortedDocValuesField("field", new BytesRef("value1"))); index.upsert(term1, document1); Term term2 = new Term("field", "value2"); Document document2 = new Document(); document2.add(new StringField("field", "value2", Field.Store.NO)); document2.add(new SortedDocValuesField("field", new BytesRef("value2"))); index.upsert(term2, document2); index.commit(); Thread.sleep(REFRESH_MILLISECONDS); assertEquals(2, index.getNumDocs()); Query query = new WildcardQuery(new Term("field", "value*")); Set<String> fields = Sets.newHashSet("field"); Map<Document, ScoreDoc> results; // Search SearcherManager searcherManager = index.getSearcherManager(); IndexSearcher searcher = searcherManager.acquire(); try { results = index.search(searcher, query, null, null, 1, fields); assertEquals(1, results.size()); ScoreDoc last1 = results.values().iterator().next(); results = index.search(searcher, query, null, last1, 1, fields); assertEquals(1, results.size()); results = index.search(searcher, query, null, null, 1, fields); assertEquals(1, results.size()); ScoreDoc last2 = results.values().iterator().next(); results = index.search(searcher, query, null, last2, 1, fields); assertEquals(1, results.size()); results = index.search(searcher, query, sort, null, 1, fields); assertEquals(1, results.size()); ScoreDoc last3 = results.values().iterator().next(); results = index.search(searcher, query, sort, last3, 1, fields); assertEquals(1, results.size()); } finally { searcherManager.release(searcher); } // Delete by term index.delete(term1); index.commit(); Thread.sleep(WAIT_MILLISECONDS); assertEquals(1, index.getNumDocs()); // Delete by query index.upsert(term1, document1); index.commit(); Thread.sleep(WAIT_MILLISECONDS); assertEquals(2, index.getNumDocs()); index.delete(new TermQuery(term1)); Thread.sleep(WAIT_MILLISECONDS); assertEquals(1, index.getNumDocs()); // Upsert index.upsert(term1, document1); index.upsert(term2, document2); index.upsert(term2, document2); index.commit(); Thread.sleep(WAIT_MILLISECONDS); assertEquals(2, index.getNumDocs()); // Truncate index.truncate(); index.commit(); Thread.sleep(WAIT_MILLISECONDS); assertEquals(0, index.getNumDocs()); // Delete index.delete(); // Cleanup folder.delete(); }
From source file:com.stratio.cassandra.lucene.service.TokenMapperGeneric.java
License:Apache License
/** {@inheritDoc} */ @Override/*from ww w .j a v a 2s. co m*/ public void addFields(Document document, DecoratedKey partitionKey) { ByteBuffer bb = factory.toByteArray(partitionKey.getToken()); String serialized = ByteBufferUtils.toString(bb); BytesRef bytesRef = new BytesRef(serialized); document.add(new StringField(FIELD_NAME, serialized, Store.NO)); document.add(new SortedDocValuesField(FIELD_NAME, bytesRef)); }
From source file:com.stratio.cassandra.lucene.service.TokenMapperGeneric.java
License:Apache License
/** * Returns the Lucene {@link BytesRef} represented by the specified Cassandra {@link Token}. * * @param token A Cassandra {@link Token}. * @return The Lucene {@link BytesRef} represented by the specified Cassandra {@link Token}. *///from w ww . j a va 2 s. c o m public BytesRef bytesRef(Token token) { ByteBuffer bb = factory.toByteArray(token); byte[] bytes = ByteBufferUtils.asArray(bb); return new BytesRef(bytes); }
From source file:com.stratio.cassandra.lucene.util.ByteBufferUtils.java
License:Apache License
/** * Returns the {@link BytesRef} representation of the specified {@link ByteBuffer}. * * @param bb the byte buffer// w w w .j a v a 2s .co m * @return the {@link BytesRef} representation of the byte buffer */ public static BytesRef bytesRef(ByteBuffer bb) { byte[] bytes = asArray(bb); return new BytesRef(bytes); }
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/* ww w .j av 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();/* w ww . j a v a 2 s . c o m*/ } // 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.tuplejump.stargate.Fields.java
License:Apache License
private static Field stringDocValuesField(String name, final AbstractType abstractType, final ByteBuffer byteBufferValue) { Object value = abstractType.compose(byteBufferValue); BytesRef bytesRef = new BytesRef(value.toString()); final String stripedName = striped + name; return new BinaryDocValuesField(stripedName, bytesRef); }
From source file:com.tuplejump.stargate.lucene.json.dewey.DeweyTokenizer.java
License:Apache License
@Override public boolean incrementToken() throws IOException { clearAttributes();//from w w w . j a v a 2 s . c om if (js.nextToken() == null) return false; JsonToken evt = js.nextToken(); if (evt == null) return false; if (evt == JsonToken.START_OBJECT || evt == JsonToken.START_ARRAY || evt == JsonToken.FIELD_NAME) { levelStack.push(siblingId); siblingId = 0; lastEvent = evt; if (!(evt == JsonToken.FIELD_NAME)) return true; } if (evt == JsonToken.END_OBJECT || evt == JsonToken.END_ARRAY) { siblingId = levelStack.size() > 0 ? levelStack.pop() : 0; siblingId++; lastEvent = evt; return true; } lastEvent = evt; switch (evt) { case FIELD_NAME: setTerm(js.getText()); typeAtt.setType(FIELD); break; case VALUE_STRING: setTerm(js.getText()); typeAtt.setType(STRING); break; case VALUE_NUMBER_INT: case VALUE_NUMBER_FLOAT: setTerm(js.getNumberValue().toString()); typeAtt.setType(NUMBER); break; case VALUE_FALSE: setTerm("false"); typeAtt.setType(BOOLEAN); break; case VALUE_TRUE: setTerm("true"); typeAtt.setType(BOOLEAN); break; case VALUE_NULL: setTerm("null"); typeAtt.setType(NULL); break; default: break; } //set the dewey id as payload payloadAtt.setPayload(new BytesRef(encodeLevel())); return true; }
From source file:com.tuplejump.stargate.lucene.LuceneUtils.java
License:Apache License
public static Field pkNameDocValue(final String pkName) { BytesRef bytesRef = new BytesRef(pkName.getBytes(StandardCharsets.UTF_8)); return new SortedDocValuesField(PK_NAME_STORED, bytesRef) { @Override/* ww w . j av a2 s. co m*/ public String toString() { return String.format("PK Name String->BinaryDocValuesField<%s>", pkName); } }; }