use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.
the class IpFieldTypeTests method testValueFormat.
public void testValueFormat() throws Exception {
MappedFieldType ft = createDefaultFieldType();
String ip = "2001:db8::2:1";
BytesRef asBytes = new BytesRef(InetAddressPoint.encode(InetAddress.getByName(ip)));
assertEquals(ip, ft.docValueFormat(null, null).format(asBytes));
ip = "192.168.1.7";
asBytes = new BytesRef(InetAddressPoint.encode(InetAddress.getByName(ip)));
assertEquals(ip, ft.docValueFormat(null, null).format(asBytes));
}
use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.
the class KeywordFieldMapperTests method testDefaults.
public void testDefaults() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "keyword").endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
assertEquals(mapping, mapper.mappingSource().toString());
ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "1234").endObject().bytes());
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(2, fields.length);
assertEquals(new BytesRef("1234"), fields[0].binaryValue());
IndexableFieldType fieldType = fields[0].fieldType();
assertThat(fieldType.omitNorms(), equalTo(true));
assertFalse(fieldType.tokenized());
assertFalse(fieldType.stored());
assertThat(fieldType.indexOptions(), equalTo(IndexOptions.DOCS));
assertThat(fieldType.storeTermVectors(), equalTo(false));
assertThat(fieldType.storeTermVectorOffsets(), equalTo(false));
assertThat(fieldType.storeTermVectorPositions(), equalTo(false));
assertThat(fieldType.storeTermVectorPayloads(), equalTo(false));
assertEquals(DocValuesType.NONE, fieldType.docValuesType());
assertEquals(new BytesRef("1234"), fields[1].binaryValue());
fieldType = fields[1].fieldType();
assertThat(fieldType.indexOptions(), equalTo(IndexOptions.NONE));
assertEquals(DocValuesType.SORTED_SET, fieldType.docValuesType());
}
use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.
the class TextFieldMapperTests method testDefaultPositionIncrementGap.
public void testDefaultPositionIncrementGap() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "text").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(TextFieldMapper.Defaults.POSITION_INCREMENT_GAP + 1, postings.nextPosition());
}
}
use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.
the class TypeFieldTypeTests method testTermsQuery.
public void testTermsQuery() throws Exception {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
IndexReader reader = openReaderWithNewType("my_type", w);
TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
ft.setName(TypeFieldMapper.NAME);
Query query = ft.termQuery("my_type", null);
assertEquals(new MatchAllDocsQuery(), query.rewrite(reader));
// Make sure that Lucene actually simplifies the query when there is a single type
Query userQuery = new PhraseQuery("body", "quick", "fox");
Query filteredQuery = new BooleanQuery.Builder().add(userQuery, Occur.MUST).add(query, Occur.FILTER).build();
Query rewritten = new IndexSearcher(reader).rewrite(filteredQuery);
assertEquals(userQuery, rewritten);
// ... and does not rewrite it if there is more than one type
reader.close();
reader = openReaderWithNewType("my_type2", w);
Query expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.NAME, "my_type")), Occur.SHOULD).build());
assertEquals(expected, query.rewrite(reader));
BytesRef[] types = new BytesRef[] { new BytesRef("my_type"), new BytesRef("my_type2"), new BytesRef("my_type3") };
// the query should match all documents
query = new TypeFieldMapper.TypesQuery(types);
assertEquals(new MatchAllDocsQuery(), query.rewrite(reader));
reader.close();
reader = openReaderWithNewType("unknown_type", w);
// the query cannot rewrite to a match all docs sinc unknown_type is not queried.
query = new TypeFieldMapper.TypesQuery(types);
expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, types[0])), Occur.SHOULD).add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, types[1])), Occur.SHOULD).build());
rewritten = query.rewrite(reader);
assertEquals(expected, rewritten);
// make sure that redundant types does not rewrite to MatchAllDocsQuery
query = new TypeFieldMapper.TypesQuery(new BytesRef("my_type"), new BytesRef("my_type"), new BytesRef("my_type"));
expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, "my_type")), Occur.SHOULD).build());
rewritten = query.rewrite(reader);
assertEquals(expected, rewritten);
IOUtils.close(reader, w, dir);
}
use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.
the class UidTests method testCreateAndSplitId.
public void testCreateAndSplitId() {
BytesRef createUid = Uid.createUidAsBytes("foo", "bar");
BytesRef[] splitUidIntoTypeAndId = splitUidIntoTypeAndId(createUid);
assertThat("foo", equalTo(splitUidIntoTypeAndId[0].utf8ToString()));
assertThat("bar", equalTo(splitUidIntoTypeAndId[1].utf8ToString()));
// split also with an offset
BytesRef ref = new BytesRef(createUid.length + 10);
ref.offset = 9;
ref.length = createUid.length;
System.arraycopy(createUid.bytes, createUid.offset, ref.bytes, ref.offset, ref.length);
splitUidIntoTypeAndId = splitUidIntoTypeAndId(ref);
assertThat("foo", equalTo(splitUidIntoTypeAndId[0].utf8ToString()));
assertThat("bar", equalTo(splitUidIntoTypeAndId[1].utf8ToString()));
}
Aggregations