private BytesReference getRandomizedBytesReference(int length) throws IOException { // we know bytes stream output always creates a paged bytes reference, we use it to create randomized content ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays); for (int i = 0; i < length; i++) { out.writeByte((byte) random().nextInt(1 << 8)); } assertEquals(out.size(), length); BytesReference ref = out.bytes(); assertEquals(ref.length(), length); if (randomBoolean()) { return new BytesArray(ref.toBytesRef()); } else if (randomBoolean()) { BytesRef bytesRef = ref.toBytesRef(); return Netty4Utils.toBytesReference(Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length)); } else { return ref; } }
public void testIteratorRandom() throws IOException { int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8)); BytesReference pbr = newBytesReference(length); if (randomBoolean()) { int sliceOffset = randomIntBetween(0, pbr.length()); int sliceLength = randomIntBetween(0, pbr.length() - sliceOffset); pbr = pbr.slice(sliceOffset, sliceLength); } if (randomBoolean()) { pbr = new BytesArray(pbr.toBytesRef()); } BytesRefIterator iterator = pbr.iterator(); BytesRef ref = null; BytesRefBuilder builder = new BytesRefBuilder(); while((ref = iterator.next()) != null) { builder.append(ref); } assertArrayEquals(BytesReference.toBytes(pbr), BytesRef.deepCopyOf(builder.toBytesRef()).bytes); }
@Override public long lookupTerm(BytesRef key) { try { switch (te.seekCeil(key)) { case FOUND: assert te.ord() >= 0; return te.ord(); case NOT_FOUND: assert te.ord() >= 0; return -te.ord()-1; default: /* END */ return -numTerms()-1; } } catch (IOException e) { throw new RuntimeException(e); } }
public static PartitionName toPartitionName(TableIdent tableIdent, @Nullable DocTableInfo docTableInfo, List<Assignment> partitionProperties, Object[] parameters) { if (docTableInfo != null) { return toPartitionName(docTableInfo, partitionProperties, parameters); } // Because only TableIdent is available, types of partitioned columns must be guessed Map<ColumnIdent, Object> properties = assignmentsToMap(partitionProperties, parameters); BytesRef[] values = new BytesRef[properties.size()]; int idx = 0; for (Object o : properties.values()) { values[idx++] = DataTypes.STRING.value(o); } return new PartitionName(tableIdent, Arrays.asList(values)); }
public void testSortMetaField() throws Exception { createIndex("test"); ensureGreen(); final int numDocs = randomIntBetween(10, 20); IndexRequestBuilder[] indexReqs = new IndexRequestBuilder[numDocs]; for (int i = 0; i < numDocs; ++i) { indexReqs[i] = client().prepareIndex("test", "type", Integer.toString(i)) .setSource(); } indexRandom(true, indexReqs); SortOrder order = randomFrom(SortOrder.values()); SearchResponse searchResponse = client().prepareSearch() .setQuery(matchAllQuery()) .setSize(randomIntBetween(1, numDocs + 5)) .addSort("_uid", order) .execute().actionGet(); assertNoFailures(searchResponse); SearchHit[] hits = searchResponse.getHits().getHits(); BytesRef previous = order == SortOrder.ASC ? new BytesRef() : UnicodeUtil.BIG_TERM; for (int i = 0; i < hits.length; ++i) { final BytesRef uid = new BytesRef(Uid.createUid(hits[i].getType(), hits[i].getId())); assertThat(previous, order == SortOrder.ASC ? lessThan(uid) : greaterThan(uid)); previous = uid; } }
public static BytesRef[] splitUidIntoTypeAndId(BytesRef uid) { int loc = -1; final int limit = uid.offset + uid.length; for (int i = uid.offset; i < limit; i++) { if (uid.bytes[i] == Uid.DELIMITER_BYTE) { // 0x23 is equal to '#' loc = i; break; } } if (loc == -1) { return null; } int idStart = loc + 1; return new BytesRef[] { new BytesRef(uid.bytes, uid.offset, loc - uid.offset), new BytesRef(uid.bytes, idStart, limit - idStart) }; }
private Query getRangeQuerySingle(String field, String part1, String part2, boolean startInclusive, boolean endInclusive, QueryShardContext context) { currentFieldType = context.fieldMapper(field); if (currentFieldType != null) { try { BytesRef part1Binary = part1 == null ? null : getAnalyzer().normalize(field, part1); BytesRef part2Binary = part2 == null ? null : getAnalyzer().normalize(field, part2); Query rangeQuery; if (currentFieldType instanceof DateFieldMapper.DateFieldType && settings.timeZone() != null) { DateFieldMapper.DateFieldType dateFieldType = (DateFieldMapper.DateFieldType) this.currentFieldType; rangeQuery = dateFieldType.rangeQuery(part1Binary, part2Binary, startInclusive, endInclusive, settings.timeZone(), null, context); } else { rangeQuery = currentFieldType.rangeQuery(part1Binary, part2Binary, startInclusive, endInclusive, context); } return rangeQuery; } catch (RuntimeException e) { if (settings.lenient()) { return null; } throw e; } } return newRangeQuery(field, part1, part2, startInclusive, endInclusive); }
private BinaryDocValues getVariableBinary(FieldInfo field, final BinaryEntry bytes) throws IOException { final IndexInput data = this.data.clone(); final MonotonicBlockPackedReader addresses = getAddressInstance(data, field, bytes); return new LongBinaryDocValues() { final BytesRef term = new BytesRef(Math.max(0, bytes.maxLength)); @Override public BytesRef get(long id) { long startAddress = bytes.offset + (id == 0 ? 0 : addresses.get(id-1)); long endAddress = bytes.offset + addresses.get(id); int length = (int) (endAddress - startAddress); try { data.seek(startAddress); data.readBytes(term.bytes, 0, length); term.length = length; return term; } catch (IOException e) { throw new RuntimeException(e); } } }; }
@Override public BytesRef indexedValueForSearch(Object value) { if (value == null) { return Values.FALSE; } if (value instanceof Boolean) { return ((Boolean) value) ? Values.TRUE : Values.FALSE; } String sValue; if (value instanceof BytesRef) { sValue = ((BytesRef) value).utf8ToString(); } else { sValue = value.toString(); } if (sValue.length() == 0) { return Values.FALSE; } if (sValue.length() == 1 && sValue.charAt(0) == 'F') { return Values.FALSE; } if (Booleans.parseBoolean(sValue, false)) { return Values.TRUE; } return Values.FALSE; }
@Override public Filter getFilter(Element e) throws ParserException { List<BytesRef> terms = new ArrayList<>(); String text = DOMUtils.getNonBlankTextOrFail(e); String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); TokenStream ts = null; try { ts = analyzer.tokenStream(fieldName, text); TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class); BytesRef bytes = termAtt.getBytesRef(); ts.reset(); while (ts.incrementToken()) { termAtt.fillBytesRef(); terms.add(BytesRef.deepCopyOf(bytes)); } ts.end(); } catch (IOException ioe) { throw new RuntimeException("Error constructing terms from index:" + ioe); } finally { IOUtils.closeWhileHandlingException(ts); } return new TermsFilter(fieldName, terms); }
private void appendRandomData(IndexOutput output) throws IOException { int numBytes = randomIntBetween(1, 1024); final BytesRef ref = new BytesRef(scaledRandomIntBetween(1, numBytes)); ref.length = ref.bytes.length; while (numBytes > 0) { if (random().nextInt(10) == 0) { output.writeByte(randomByte()); numBytes--; } else { for (int i = 0; i<ref.length; i++) { ref.bytes[i] = randomByte(); } final int min = Math.min(numBytes, ref.bytes.length); output.writeBytes(ref.bytes, ref.offset, min); numBytes -= min; } } }
/** * 将Product对象序列化存入payload * [这里仅仅是个示例,其实这种做法不可取,一般不会把整个对象存入payload,这样索引体积会很大,浪费硬盘空间] */ @Override public BytesRef payload() { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(currentProduct); out.close(); return new BytesRef(bos.toByteArray()); } catch (IOException e) { throw new RuntimeException("Well that's unfortunate."); } }
/** * Given a document and a term, return the term itself if it exists or * <tt>null</tt> otherwise. */ private BytesRef getComparableBytes(int doc, BytesRef term) { if (term.length == 0 && isNull(doc, term)) { return null; } return term; }
protected ClusterLoggingOverridesChildExpression(final Map.Entry<String, String> setting) { childImplementations.put(NAME, new SimpleObjectExpression<BytesRef>() { @Override public BytesRef value() { return new BytesRef(setting.getKey()); } }); childImplementations.put(LEVEL, new SimpleObjectExpression<BytesRef>() { @Override public BytesRef value() { return new BytesRef(setting.getValue().toUpperCase(Locale.ENGLISH)); } }); }
private static BytesRef evaluate(@Nonnull BytesRef inputStr, int beginIdx, int len) { final int startPos = Math.max(0, beginIdx - 1); if (startPos > inputStr.length - 1) { return EMPTY_BYTES_REF; } int endPos = inputStr.length; if (startPos + len < endPos) { endPos = startPos + len; } return substring(inputStr, startPos, endPos); }
/** * 文章标题 */ @Override public Set<BytesRef> contexts() { try { Set<BytesRef> regions = new HashSet<BytesRef>(); regions.add(new BytesRef(currentBlog.getTitle().getBytes("UTF8"))); return regions; } catch (UnsupportedEncodingException e) { throw new RuntimeException("Couldn't convert to UTF-8"); } }
@Override protected BytesRef valueOf(String value, String optionalFormat) { if (optionalFormat != null) { throw new UnsupportedOperationException("custom format isn't supported"); } return new BytesRef(value); }
public void testPositionIncrementGap() throws IOException { final int positionIncrementGap = randomIntBetween(1, 1000); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties").startObject("field") .field("type", "text") .field("position_increment_gap", positionIncrementGap) .endObject().endObject() .endObject().endObject().string(); DocumentMapper mapper = indexService.mapperService().merge("type", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE, false); assertEquals(mapping, mapper.mappingSource().toString()); ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder() .startObject() .array("field", new String[] {"a", "b"}) .endObject() .bytes()); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(2, fields.length); assertEquals("a", fields[0].stringValue()); assertEquals("b", fields[1].stringValue()); IndexShard shard = indexService.getShard(0); shard.index(new Engine.Index(new Term("_uid", doc.uid()), doc)); shard.refresh("test"); try (Engine.Searcher searcher = shard.acquireSearcher("test")) { LeafReader leaf = searcher.getDirectoryReader().leaves().get(0).reader(); TermsEnum terms = leaf.terms("field").iterator(); assertTrue(terms.seekExact(new BytesRef("b"))); PostingsEnum postings = terms.postings(null, PostingsEnum.POSITIONS); assertEquals(0, postings.nextDoc()); assertEquals(positionIncrementGap + 1, postings.nextPosition()); } }
Query createCandidateQuery(IndexReader indexReader) throws IOException { List<BytesRef> extractedTerms = new ArrayList<>(); LeafReader reader = indexReader.leaves().get(0).reader(); Fields fields = reader.fields(); for (String field : fields) { Terms terms = fields.terms(field); if (terms == null) { continue; } BytesRef fieldBr = new BytesRef(field); TermsEnum tenum = terms.iterator(); for (BytesRef term = tenum.next(); term != null; term = tenum.next()) { BytesRefBuilder builder = new BytesRefBuilder(); builder.append(fieldBr); builder.append(FIELD_VALUE_SEPARATOR); builder.append(term); extractedTerms.add(builder.toBytesRef()); } } Query extractionSuccess = new TermInSetQuery(queryTermsField.name(), extractedTerms); // include extractionResultField:failed, because docs with this term have no extractedTermsField // and otherwise we would fail to return these docs. Docs that failed query term extraction // always need to be verified by MemoryIndex: Query extractionFailure = new TermQuery(new Term(extractionResultField.name(), EXTRACTION_FAILED)); return new BooleanQuery.Builder() .add(extractionSuccess, Occur.SHOULD) .add(extractionFailure, Occur.SHOULD) .build(); }
public Term newTerm(String value) { try { final BytesRef bytesRef = fieldType.indexedValueForSearch(value); return new Term(field, bytesRef); } catch (Exception ex) { // we can't parse it just use the incoming value -- it will // just have a DF of 0 at the end of the day and will be ignored } return new Term(field, value); }
private void writePayload(BytesRef payload) throws IOException { if (payload != null) { output.writeVInt(payload.length); output.writeBytes(payload.bytes, payload.offset, payload.length); } else { output.writeVInt(0); } }
@Nullable @Override public BytesRef apply(@Nullable Input<?> input) { if (input == null) { return null; } return BytesRefs.toBytesRef(input.value()); }
@SuppressWarnings("unused") static String brToString(BytesRef b) { try { return b.utf8ToString() + " " + b; } catch (Throwable t) { // If BytesRef isn't actually UTF8, or it's eg a // prefix of UTF8 that ends mid-unicode-char, we // fallback to hex: return b.toString(); } }
public BinaryDocValues iterator() { final BytesRef term = new BytesRef(); return new BinaryDocValues() { @Override public BytesRef get(int docID) { final int pointer = (int) docToOffset.get(docID); if (pointer == 0) { term.length = 0; } else { bytes.fill(term, pointer); } return term; } }; }
static HttpEntity initialSearchEntity(SearchRequest searchRequest, BytesReference query) { // EMPTY is safe here because we're not calling namedObject try (XContentBuilder entity = JsonXContent.contentBuilder(); XContentParser queryParser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, query)) { entity.startObject(); entity.field("query"); { /* We're intentionally a bit paranoid here - copying the query as xcontent rather than writing a raw field. We don't want * poorly written queries to escape. Ever. */ entity.copyCurrentStructure(queryParser); XContentParser.Token shouldBeEof = queryParser.nextToken(); if (shouldBeEof != null) { throw new ElasticsearchException( "query was more than a single object. This first token after the object is [" + shouldBeEof + "]"); } } if (searchRequest.source().fetchSource() != null) { entity.field("_source", searchRequest.source().fetchSource()); } else { entity.field("_source", true); } entity.endObject(); BytesRef bytes = entity.bytes().toBytesRef(); return new ByteArrayEntity(bytes.bytes, bytes.offset, bytes.length, ContentType.APPLICATION_JSON); } catch (IOException e) { throw new ElasticsearchException("unexpected error building entity", e); } }
private boolean isSameIndex(Object value, String indexName) { if (value instanceof BytesRef) { BytesRef indexNameRef = new BytesRef(indexName); return (indexNameRef.bytesEquals((BytesRef) value)); } else { return indexName.equals(value.toString()); } }
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (context.highlight() != null) { context.parsedQuery(new ParsedQuery(query)); context.hitContext().cache().clear(); } if (Lucene.exists(searcher, existsQuery)) { if (!limit || counter < size) { matches.add(BytesRef.deepCopyOf(current)); scores.add(scorer.score()); if (context.highlight() != null) { highlightPhase.hitExecute(context, context.hitContext()); hls.add(context.hitContext().hit().getHighlightFields()); } } counter++; postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
public void testStoreStats() throws IOException { final ShardId shardId = new ShardId("index", "_na_", 1); DirectoryService directoryService = new LuceneManagedDirectoryService(random()); Settings settings = Settings.builder() .put(IndexMetaData.SETTING_VERSION_CREATED, org.elasticsearch.Version.CURRENT) .put(Store.INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING.getKey(), TimeValue.timeValueMinutes(0)).build(); Store store = new Store(shardId, IndexSettingsModule.newIndexSettings("index", settings), directoryService, new DummyShardLock(shardId)); long initialStoreSize = 0; for (String extraFiles : store.directory().listAll()) { assertTrue("expected extraFS file but got: " + extraFiles, extraFiles.startsWith("extra")); initialStoreSize += store.directory().fileLength(extraFiles); } StoreStats stats = store.stats(); assertEquals(stats.getSize().getBytes(), initialStoreSize); Directory dir = store.directory(); final long length; try (IndexOutput output = dir.createOutput("foo.bar", IOContext.DEFAULT)) { int iters = scaledRandomIntBetween(10, 100); for (int i = 0; i < iters; i++) { BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024)); output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length); } length = output.getFilePointer(); } assertTrue(numNonExtraFiles(store) > 0); stats = store.stats(); assertEquals(stats.getSizeInBytes(), length + initialStoreSize); deleteContent(store.directory()); IOUtils.close(store); }
public static BytesRef createUidAsBytes(BytesRef type, BytesRef id) { final BytesRef ref = new BytesRef(type.length + 1 + id.length); System.arraycopy(type.bytes, type.offset, ref.bytes, 0, type.length); ref.offset = type.length; ref.bytes[ref.offset++] = DELIMITER_BYTE; System.arraycopy(id.bytes, id.offset, ref.bytes, ref.offset, id.length); ref.offset = 0; ref.length = ref.bytes.length; return ref; }
/** * @return the number of bytes for the term based on the length and ordinal overhead */ @Override public long bytesPerValue(BytesRef term) { if (term == null) { return 0; } long bytes = term.length; // 64 bytes for miscellaneous overhead bytes += 64; // Seems to be about a 1.5x compression per term/ord, plus 1 for some wiggle room bytes = (long) ((double) bytes / 1.5) + 1; return bytes; }
public static BytesRef getSinglePageOrNull(BytesReference ref) throws IOException { if (ref.length() > 0) { BytesRefIterator iterator = ref.iterator(); BytesRef next = iterator.next(); BytesRef retVal = next.clone(); if (iterator.next() == null) { return retVal; } } else { return new BytesRef(); } return null; }
static void assertLateParsingQuery(Query query, String type, String id) throws IOException { assertThat(query, instanceOf(HasChildQueryBuilder.LateParsingQuery.class)); HasChildQueryBuilder.LateParsingQuery lateParsingQuery = (HasChildQueryBuilder.LateParsingQuery) query; assertThat(lateParsingQuery.getInnerQuery(), instanceOf(BooleanQuery.class)); BooleanQuery booleanQuery = (BooleanQuery) lateParsingQuery.getInnerQuery(); assertThat(booleanQuery.clauses().size(), equalTo(2)); //check the inner ids query, we have to call rewrite to get to check the type it's executed against assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST)); assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class)); TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).getQuery(); Query rewrittenTermsQuery = termsQuery.rewrite(null); assertThat(rewrittenTermsQuery, instanceOf(ConstantScoreQuery.class)); ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) rewrittenTermsQuery; assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class)); BooleanQuery booleanTermsQuery = (BooleanQuery) constantScoreQuery.getQuery(); assertThat(booleanTermsQuery.clauses().toString(), booleanTermsQuery.clauses().size(), equalTo(1)); assertThat(booleanTermsQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.SHOULD)); assertThat(booleanTermsQuery.clauses().get(0).getQuery(), instanceOf(TermQuery.class)); TermQuery termQuery = (TermQuery) booleanTermsQuery.clauses().get(0).getQuery(); assertThat(termQuery.getTerm().field(), equalTo(UidFieldMapper.NAME)); //we want to make sure that the inner ids query gets executed against the child type rather than the main type we initially set to the context BytesRef[] ids = Uid.createUidsForTypesAndIds(Collections.singletonList(type), Collections.singletonList(id)); assertThat(termQuery.getTerm().bytes(), equalTo(ids[0])); //check the type filter assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER)); assertEquals(new TypeFieldMapper.TypesQuery(new BytesRef(type)), booleanQuery.clauses().get(1).getQuery()); }
@Override public final boolean incrementToken() throws IOException { if (input.incrementToken()) { String type = typeAtt.type(); if (type != null && !type.isEmpty()) { payloadAtt.setPayload(new BytesRef(type)); } return true; } else { return false; } }
/** * Returns a DocIdSet per segments containing the matching docs for the specified slice. */ private DocIdSet build(LeafReader reader) throws IOException { final DocIdSetBuilder builder = new DocIdSetBuilder(reader.maxDoc()); final Terms terms = reader.terms(getField()); final TermsEnum te = terms.iterator(); PostingsEnum docsEnum = null; for (BytesRef term = te.next(); term != null; term = te.next()) { int hashCode = term.hashCode(); if (contains(hashCode)) { docsEnum = te.postings(docsEnum, PostingsEnum.NONE); builder.add(docsEnum); } } return builder.build(); }
@Override public BytesRef copy(BytesRef value, BytesRef reuse) { if (value == null) { return null; } if (reuse != null) { reuse.bytes = ArrayUtil.grow(reuse.bytes, value.length); reuse.offset = 0; reuse.length = value.length; System.arraycopy(value.bytes, value.offset, reuse.bytes, 0, value.length); return reuse; } else { return BytesRef.deepCopyOf(value); } }
public SortedDocValues iterator() { final BytesRef term = new BytesRef(); return new SortedDocValues() { @Override public int getValueCount() { return numOrd; } @Override public int getOrd(int docID) { // Subtract 1, matching the 1+ord we did when // storing, so that missing values, which are 0 in the // packed ints, are returned as -1 ord: return (int) docToTermOrd.get(docID)-1; } @Override public BytesRef lookupOrd(int ord) { if (ord < 0) { throw new IllegalArgumentException("ord must be >=0 (got ord=" + ord + ")"); } bytes.fill(term, termOrdToBytesOffset.get(ord)); return term; } }; }
@Override protected AcceptStatus accept(BytesRef arg0) throws IOException { int docFreq = docFreq(); if (docFreq >= minFreq && docFreq <= maxFreq) { return AcceptStatus.YES; } return AcceptStatus.NO; }
public static BytesRef toBytesRef(Object value, BytesRefBuilder spare) { if (value == null) { return null; } if (value instanceof BytesRef) { return (BytesRef) value; } spare.copyChars(value.toString()); return spare.get(); }
@Override public BytesRef binaryValue() { CollectionUtils.sortAndDedup(values); final byte[] bytes = new byte[values.size() * 8]; for (int i = 0; i < values.size(); ++i) { ByteUtils.writeDoubleLE(values.get(i), bytes, i * 8); } return new BytesRef(bytes); }
protected IllegalArgumentException illegalMatchType(String matchType) { throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Unknown matchType \"%s\". Possible matchTypes are: %s", matchType, Joiner.on(", ").join(Iterables.transform(SUPPORTED_TYPES.keySet(), new Function<BytesRef, String>() { @Nullable @Override public String apply(@Nullable BytesRef input) { return BytesRefs.toString(input); } } )))); }