Search in sources :

Example 26 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class BooleanFieldMapperTests method setup.

@Before
public void setup() {
    indexService = createIndex("test");
    parser = indexService.mapperService().documentMapperParser();
    IndexService preEs6IndexService = createIndex("legacy", Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_5_0_0).build());
    preEs6Parser = preEs6IndexService.mapperService().documentMapperParser();
}
Also used : IndexService(org.elasticsearch.index.IndexService) Before(org.junit.Before)

Example 27 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class DoubleIndexingDocTests method testDoubleIndexingSameDoc.

public void testDoubleIndexingSameDoc() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(random(), Lucene.STANDARD_ANALYZER));
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").endObject().endObject().endObject().string();
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("type").setSource(mapping, XContentType.JSON).get();
    DocumentMapper mapper = index.mapperService().documentMapper("type");
    QueryShardContext context = index.newQueryShardContext(0, null, () -> 0L);
    ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field1", "value1").field("field2", 1).field("field3", 1.1).field("field4", "2010-01-01").startArray("field5").value(1).value(2).value(3).endArray().endObject().bytes());
    assertNotNull(doc.dynamicMappingsUpdate());
    client().admin().indices().preparePutMapping("test").setType("type").setSource(doc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    mapper = index.mapperService().documentMapper("type");
    writer.addDocument(doc.rootDoc());
    writer.addDocument(doc.rootDoc());
    IndexReader reader = DirectoryReader.open(writer);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(mapper.mappers().smartNameFieldMapper("field1").fieldType().termQuery("value1", context), 10);
    assertThat(topDocs.totalHits, equalTo(2));
    topDocs = searcher.search(mapper.mappers().smartNameFieldMapper("field2").fieldType().termQuery("1", context), 10);
    assertThat(topDocs.totalHits, equalTo(2));
    topDocs = searcher.search(mapper.mappers().smartNameFieldMapper("field3").fieldType().termQuery("1.1", context), 10);
    assertThat(topDocs.totalHits, equalTo(2));
    topDocs = searcher.search(mapper.mappers().smartNameFieldMapper("field4").fieldType().termQuery("2010-01-01", context), 10);
    assertThat(topDocs.totalHits, equalTo(2));
    topDocs = searcher.search(mapper.mappers().smartNameFieldMapper("field5").fieldType().termQuery("1", context), 10);
    assertThat(topDocs.totalHits, equalTo(2));
    topDocs = searcher.search(mapper.mappers().smartNameFieldMapper("field5").fieldType().termQuery("2", context), 10);
    assertThat(topDocs.totalHits, equalTo(2));
    topDocs = searcher.search(mapper.mappers().smartNameFieldMapper("field5").fieldType().termQuery("3", context), 10);
    assertThat(topDocs.totalHits, equalTo(2));
    writer.close();
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexWriter(org.apache.lucene.index.IndexWriter) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) IndexReader(org.apache.lucene.index.IndexReader) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Directory(org.apache.lucene.store.Directory)

Example 28 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testEmptyName.

public void testEmptyName() throws IOException {
    IndexService indexService = createIndex("test");
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("").field("type", "completion").endObject().endObject().endObject().endObject().string();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
    assertThat(e.getMessage(), containsString("name cannot be empty string"));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 29 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class DocumentMapperParserTests method testFieldNameWithDots.

public void testFieldNameWithDots() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapperParser mapperParser = indexService.mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("foo.bar").field("type", "text").endObject().startObject("foo.baz").field("type", "keyword").endObject().endObject().endObject().endObject().string();
    DocumentMapper docMapper = mapperParser.parse("type", new CompressedXContent(mapping));
    assertNotNull(docMapper.mappers().getMapper("foo.bar"));
    assertNotNull(docMapper.mappers().getMapper("foo.baz"));
    assertNotNull(docMapper.objectMappers().get("foo"));
}
Also used : IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser)

Example 30 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class CopyToMapperTests method testCopyToNestedField.

public void testCopyToNestedField() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    XContentBuilder mapping = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("n1").field("type", "nested").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("n2").field("type", "nested").startObject("properties").startObject("target").field("type", "long").field("doc_values", false).endObject().startObject("source").field("type", "long").field("doc_values", false).startArray("copy_to").value(// should go to the root doc
    "target").value(// should go to the parent doc
    "n1.target").value(// should go to the current doc
    "n1.n2.target").endArray().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject();
    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.string()));
    XContentBuilder jsonDoc = XContentFactory.jsonBuilder().startObject().startArray("n1").startObject().startArray("n2").startObject().field("source", 3).endObject().startObject().field("source", 5).endObject().endArray().endObject().startObject().startArray("n2").startObject().field("source", 7).endObject().endArray().endObject().endArray().endObject();
    ParsedDocument doc = mapper.parse("test", "type", "1", jsonDoc.bytes());
    assertEquals(6, doc.docs().size());
    Document nested = doc.docs().get(0);
    assertFieldValue(nested, "n1.n2.target", 7L);
    assertFieldValue(nested, "n1.target");
    assertFieldValue(nested, "target");
    nested = doc.docs().get(2);
    assertFieldValue(nested, "n1.n2.target", 5L);
    assertFieldValue(nested, "n1.target");
    assertFieldValue(nested, "target");
    nested = doc.docs().get(3);
    assertFieldValue(nested, "n1.n2.target", 3L);
    assertFieldValue(nested, "n1.target");
    assertFieldValue(nested, "target");
    Document parent = doc.docs().get(1);
    assertFieldValue(parent, "target");
    assertFieldValue(parent, "n1.target", 7L);
    assertFieldValue(parent, "n1.n2.target");
    parent = doc.docs().get(4);
    assertFieldValue(parent, "target");
    assertFieldValue(parent, "n1.target", 3L, 5L);
    assertFieldValue(parent, "n1.n2.target");
    Document root = doc.docs().get(5);
    assertFieldValue(root, "target", 3L, 5L, 7L);
    assertFieldValue(root, "n1.target");
    assertFieldValue(root, "n1.n2.target");
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Document(org.elasticsearch.index.mapper.ParseContext.Document) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

IndexService (org.elasticsearch.index.IndexService)212 IndexShard (org.elasticsearch.index.shard.IndexShard)77 IndicesService (org.elasticsearch.indices.IndicesService)56 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)50 ShardId (org.elasticsearch.index.shard.ShardId)38 Index (org.elasticsearch.index.Index)36 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)33 Settings (org.elasticsearch.common.settings.Settings)31 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)22 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)22 HashMap (java.util.HashMap)21 Map (java.util.Map)19 ClusterService (org.elasticsearch.cluster.service.ClusterService)19 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)18 ElasticsearchException (org.elasticsearch.ElasticsearchException)17 ClusterState (org.elasticsearch.cluster.ClusterState)16 List (java.util.List)14 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)13 IndexSettings (org.elasticsearch.index.IndexSettings)13