Search in sources :

Example 76 with BytesRef

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));
}
Also used : BytesRef(org.apache.lucene.util.BytesRef)

Example 77 with BytesRef

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());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) IndexableFieldType(org.apache.lucene.index.IndexableFieldType) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesRef(org.apache.lucene.util.BytesRef)

Example 78 with BytesRef

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());
    }
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) IndexShard(org.elasticsearch.index.shard.IndexShard) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) TermsEnum(org.apache.lucene.index.TermsEnum) IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) PostingsEnum(org.apache.lucene.index.PostingsEnum) Engine(org.elasticsearch.index.engine.Engine) BytesRef(org.apache.lucene.util.BytesRef)

Example 79 with BytesRef

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);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 80 with BytesRef

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()));
}
Also used : BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesRef (org.apache.lucene.util.BytesRef)1449 Document (org.apache.lucene.document.Document)410 Directory (org.apache.lucene.store.Directory)370 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)266 ArrayList (java.util.ArrayList)186 Test (org.junit.Test)182 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)164 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)152 Term (org.apache.lucene.index.Term)124 Analyzer (org.apache.lucene.analysis.Analyzer)121 IndexReader (org.apache.lucene.index.IndexReader)121 TermsEnum (org.apache.lucene.index.TermsEnum)116 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)110 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)105 IOException (java.io.IOException)104 Field (org.apache.lucene.document.Field)101 StringField (org.apache.lucene.document.StringField)101 CrateUnitTest (io.crate.test.integration.CrateUnitTest)95 TextField (org.apache.lucene.document.TextField)95 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)87