Search in sources :

Example 26 with MapperParsingException

use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testPercolatorFieldMapper_noQuery.

public void testPercolatorFieldMapper_noQuery() throws Exception {
    addQueryMapping();
    ParsedDocument doc = mapperService.documentMapper(typeName).parse("test", typeName, "1", XContentFactory.jsonBuilder().startObject().endObject().bytes());
    assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0));
    try {
        mapperService.documentMapper(typeName).parse("test", typeName, "1", XContentFactory.jsonBuilder().startObject().nullField(fieldName).endObject().bytes());
    } catch (MapperParsingException e) {
        assertThat(e.getDetailedMessage(), containsString("query malformed, must start with start_object"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument)

Example 27 with MapperParsingException

use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.

the class PercolatorQuerySearchIT method testWithMultiplePercolatorFields.

public void testWithMultiplePercolatorFields() throws Exception {
    String queryFieldName = randomAsciiOfLength(8);
    createIndex("test1", client().admin().indices().prepareCreate("test1").addMapping("doc_type", "field", "type=keyword").addMapping("query_type", queryFieldName, "type=percolator"));
    createIndex("test2", client().admin().indices().prepareCreate("test2").addMapping("doc_type", "field", "type=keyword").addMapping("query_type", jsonBuilder().startObject().startObject("query_type").startObject("properties").startObject("object_field").field("type", "object").startObject("properties").startObject(queryFieldName).field("type", "percolator").endObject().endObject().endObject().endObject().endObject().endObject()));
    // Acceptable:
    client().prepareIndex("test1", "query_type", "1").setSource(jsonBuilder().startObject().field(queryFieldName, matchQuery("field", "value")).endObject()).get();
    client().prepareIndex("test2", "query_type", "1").setSource(jsonBuilder().startObject().startObject("object_field").field(queryFieldName, matchQuery("field", "value")).endObject().endObject()).get();
    client().admin().indices().prepareRefresh().get();
    BytesReference source = jsonBuilder().startObject().field("field", "value").endObject().bytes();
    SearchResponse response = client().prepareSearch().setQuery(new PercolateQueryBuilder(queryFieldName, "doc_type", source, XContentType.JSON)).setIndices("test1").get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getType(), equalTo("query_type"));
    assertThat(response.getHits().getAt(0).getIndex(), equalTo("test1"));
    response = client().prepareSearch().setQuery(new PercolateQueryBuilder("object_field." + queryFieldName, "doc_type", source, XContentType.JSON)).setIndices("test2").get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getType(), equalTo("query_type"));
    assertThat(response.getHits().getAt(0).getIndex(), equalTo("test2"));
    // Unacceptable:
    MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
        client().prepareIndex("test2", "query_type", "1").setSource(jsonBuilder().startObject().startArray("object_field").startObject().field(queryFieldName, matchQuery("field", "value")).endObject().startObject().field(queryFieldName, matchQuery("field", "value")).endObject().endArray().endObject()).get();
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    assertThat(e.getCause().getMessage(), equalTo("a document can only contain one percolator query"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 28 with MapperParsingException

use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.

the class NullValueTests method testNullNullValue.

public void testNullNullValue() throws Exception {
    IndexService indexService = createIndex("test", Settings.builder().build());
    String[] typesToTest = { "integer", "long", "double", "float", "short", "date", "ip", "keyword", "boolean", "byte" };
    for (String type : typesToTest) {
        String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("numeric").field("type", type).field("null_value", (String) null).endObject().endObject().endObject().endObject().string();
        try {
            indexService.mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
            fail("Test should have failed because [null_value] was null.");
        } catch (MapperParsingException e) {
            assertThat(e.getMessage(), equalTo("Property [null_value] cannot be null."));
        }
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 29 with MapperParsingException

use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.

the class RoutingFieldMapperTests method testIncludeInObjectNotAllowed.

public void testIncludeInObjectNotAllowed() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    try {
        docMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("_routing", "foo").endObject().bytes());
        fail("Expected failure to parse metadata field");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("Field [_routing] is a metadata field and cannot be added inside a document"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 30 with MapperParsingException

use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.

the class ObjectMapperTests method testFieldsWithFilledArrayShouldThrowException.

public void testFieldsWithFilledArrayShouldThrowException() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("tweet").startObject("properties").startArray("fields").startObject().field("test", "string").endObject().startObject().field("test2", "string").endObject().endArray().endObject().endObject().endObject().string();
    try {
        createIndex("test").mapperService().documentMapperParser().parse("tweet", new CompressedXContent(mapping));
        fail("Expected MapperParsingException");
    } catch (MapperParsingException e) {
        assertThat(e.getMessage(), containsString("Expected map for property [fields]"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)35 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)16 Matchers.containsString (org.hamcrest.Matchers.containsString)11 IndexService (org.elasticsearch.index.IndexService)7 Settings (org.elasticsearch.common.settings.Settings)5 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)5 Index (org.elasticsearch.index.Index)4 MapperService (org.elasticsearch.index.mapper.MapperService)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)2 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)2 ValidationException (org.elasticsearch.common.ValidationException)2 IndexScopedSettings (org.elasticsearch.common.settings.IndexScopedSettings)2 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)2