Search in sources :

Example 26 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class DocIndexMetaDataTest method testSchemaWithNotNullGeneratedColumn.

@Test
public void testSchemaWithNotNullGeneratedColumn() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("_meta").startObject("generated_columns").field("week", "date_trunc('week', ts)").endObject().startObject("constraints").array("not_null", "week").endObject().endObject().startObject("properties").startObject("ts").field("type", "date").endObject().startObject("week").field("type", "long").endObject().endObject();
    IndexMetaData metaData = getIndexMetaData("test1", builder, Settings.EMPTY, null);
    DocIndexMetaData md = newMeta(metaData, "test1");
    assertThat(md.columns().size(), is(2));
    Reference week = md.references().get(new ColumnIdent("week"));
    assertThat(week, Matchers.notNullValue());
    assertThat(week.isNullable(), is(false));
    assertThat(week, instanceOf(GeneratedReference.class));
    assertThat(((GeneratedReference) week).formattedGeneratedExpression(), is("date_trunc('week', ts)"));
    assertThat(((GeneratedReference) week).generatedExpression(), isFunction("date_trunc", isLiteral("week"), isReference("ts")));
    assertThat(((GeneratedReference) week).referencedReferences(), contains(isReference("ts")));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 27 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class DocIndexMetaDataTest method testExtractColumnPolicy.

@Test
public void testExtractColumnPolicy() throws Exception {
    XContentBuilder ignoredBuilder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).field("dynamic", false).startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("content").field("type", "string").field("index", "no").endObject().endObject().endObject().endObject();
    DocIndexMetaData mdIgnored = newMeta(getIndexMetaData("test_ignored", ignoredBuilder), "test_ignored");
    assertThat(mdIgnored.columnPolicy(), is(ColumnPolicy.IGNORED));
    XContentBuilder strictBuilder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).field("dynamic", "strict").startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("content").field("type", "string").field("index", "no").endObject().endObject().endObject().endObject();
    DocIndexMetaData mdStrict = newMeta(getIndexMetaData("test_strict", strictBuilder), "test_strict");
    assertThat(mdStrict.columnPolicy(), is(ColumnPolicy.STRICT));
    XContentBuilder dynamicBuilder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).field("dynamic", true).startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("content").field("type", "string").field("index", "no").endObject().endObject().endObject().endObject();
    DocIndexMetaData mdDynamic = newMeta(getIndexMetaData("test_dynamic", dynamicBuilder), "test_dynamic");
    assertThat(mdDynamic.columnPolicy(), is(ColumnPolicy.DYNAMIC));
    XContentBuilder missingBuilder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("content").field("type", "string").field("index", "no").endObject().endObject().endObject().endObject();
    DocIndexMetaData mdMissing = newMeta(getIndexMetaData("test_missing", missingBuilder), "test_missing");
    assertThat(mdMissing.columnPolicy(), is(ColumnPolicy.DYNAMIC));
    XContentBuilder wrongBuilder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).field("dynamic", "wrong").startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("content").field("type", "string").field("index", "no").endObject().endObject().endObject().endObject();
    DocIndexMetaData mdWrong = newMeta(getIndexMetaData("test_wrong", wrongBuilder), "test_wrong");
    assertThat(mdWrong.columnPolicy(), is(ColumnPolicy.DYNAMIC));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 28 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class DocIndexMetaDataTest method testExtractMultiplePrimaryKeys.

@Test
public void testExtractMultiplePrimaryKeys() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).startObject("_meta").array("primary_keys", "id", "title").endObject().startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("title").field("type", "string").field("index", "no").endObject().endObject().endObject().endObject();
    IndexMetaData metaData = getIndexMetaData("test_multi_pk", builder);
    DocIndexMetaData md = newMeta(metaData, "test_multi_pk");
    assertThat(md.primaryKey().size(), is(2));
    assertThat(md.primaryKey(), hasItems(ColumnIdent.fromPath("id"), ColumnIdent.fromPath("title")));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 29 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project rssriver by dadoonet.

the class RssToJson method toJson.

public static XContentBuilder toJson(SyndEntry message, String riverName, String feedname, boolean raw) throws IOException {
    XContentBuilder out = jsonBuilder().startObject().field(Rss.FEEDNAME, feedname).field(Rss.TITLE, message.getTitle()).field(Rss.AUTHOR, message.getAuthor()).field(Rss.DESCRIPTION, message.getDescription() != null ? message.getDescription().getValue() : null).field(Rss.LINK, message.getLink()).field(Rss.PUBLISHED_DATE, message.getPublishedDate()).field(Rss.SOURCE, message.getSource());
    if (raw) {
        if (message.getContents() != null) {
            out.startObject(Rss.RAW);
            for (Object o : message.getContents()) {
                if (o instanceof SyndContentImpl) {
                    SyndContentImpl content = (SyndContentImpl) o;
                    out.field(content.getType(), content.getValue());
                }
            }
            out.endObject();
        }
    }
    GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(message);
    if (geoRSSModule != null) {
        final Position position = geoRSSModule.getPosition();
        if (position != null) {
            out.startObject(Rss.LOCATION);
            out.field(Rss.Location.LAT, position.getLatitude());
            out.field(Rss.Location.LON, position.getLongitude());
            out.endObject();
        }
    }
    if (message.getCategories() != null && message.getCategories().size() > 0) {
        out.startArray(Rss.CATEGORIES);
        for (Object oCategory : message.getCategories()) {
            if (oCategory instanceof SyndCategory) {
                SyndCategory category = (SyndCategory) oCategory;
                out.value(category.getName());
            }
        }
        out.endArray();
    }
    if (message.getEnclosures() != null && message.getEnclosures().size() > 0) {
        out.startArray(Rss.ENCLOSURES);
        for (Object oEnclosure : message.getEnclosures()) {
            if (oEnclosure instanceof SyndEnclosure) {
                out.startObject();
                SyndEnclosure enclosure = (SyndEnclosure) oEnclosure;
                out.field(Rss.Enclosures.URL, enclosure.getUrl());
                out.field(Rss.Enclosures.TYPE, enclosure.getType());
                out.field(Rss.Enclosures.LENGTH, enclosure.getLength());
                out.endObject();
            }
        }
        out.endArray();
    }
    MediaEntryModule mediaEntryModule = (MediaEntryModule) message.getModule(MediaEntryModule.URI);
    if (mediaEntryModule != null) {
        out.startArray(Rss.MEDIAS);
        for (MediaContent mediaContent : mediaEntryModule.getMediaContents()) {
            out.startObject();
            addFieldIfNotNull(out, Rss.Medias.TYPE, mediaContent.getType());
            if (mediaContent.getReference() != null) {
                URI url = null;
                if (mediaContent.getReference() instanceof PlayerReference) {
                    url = ((PlayerReference) mediaContent.getReference()).getUrl();
                }
                if (mediaContent.getReference() instanceof UrlReference) {
                    url = ((UrlReference) mediaContent.getReference()).getUrl();
                }
                if (url != null) {
                    addFieldIfNotNull(out, Rss.Medias.REFERENCE, url.toString());
                }
            }
            addFieldIfNotNull(out, Rss.Medias.LANGUAGE, mediaContent.getLanguage());
            addFieldIfNotNull(out, Rss.Medias.TITLE, mediaContent.getMetadata().getTitle());
            addFieldIfNotNull(out, Rss.Medias.DESCRIPTION, mediaContent.getMetadata().getDescription());
            addFieldIfNotNull(out, Rss.Medias.DURATION, mediaContent.getDuration());
            addFieldIfNotNull(out, Rss.Medias.WIDTH, mediaContent.getWidth());
            addFieldIfNotNull(out, Rss.Medias.HEIGHT, mediaContent.getHeight());
            out.endObject();
        }
        out.endArray();
    }
    if (riverName != null) {
        out.field("river", riverName);
    }
    return out.endObject();
}
Also used : UrlReference(com.rometools.modules.mediarss.types.UrlReference) PlayerReference(com.rometools.modules.mediarss.types.PlayerReference) SyndCategory(com.rometools.rome.feed.synd.SyndCategory) MediaEntryModule(com.rometools.modules.mediarss.MediaEntryModule) Position(com.rometools.modules.georss.geometries.Position) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) GeoRSSModule(com.rometools.modules.georss.GeoRSSModule) SyndEnclosure(com.rometools.rome.feed.synd.SyndEnclosure) MediaContent(com.rometools.modules.mediarss.types.MediaContent) URI(java.net.URI) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 30 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project rssriver by dadoonet.

the class RssToJson method buildRssMapping.

/**
     * Build the mapping for RSS content
     * @param type elasticsearch type you will use
     * @return a mapping
     * @throws Exception
     */
public static XContentBuilder buildRssMapping(String type, boolean raw) throws Exception {
    XContentBuilder xbMapping = jsonBuilder().prettyPrint().startObject();
    // Type
    xbMapping.startObject(type);
    xbMapping.startObject("properties");
    // feed document
    addNotAnalyzedString(xbMapping, Rss.FEEDNAME);
    addAnalyzedString(xbMapping, Rss.TITLE);
    addAnalyzedString(xbMapping, Rss.AUTHOR);
    addAnalyzedString(xbMapping, Rss.DESCRIPTION);
    addNotIndexedString(xbMapping, Rss.LINK);
    addAnalyzedString(xbMapping, Rss.SOURCE);
    addDate(xbMapping, Rss.PUBLISHED_DATE);
    addGeopoint(xbMapping, Rss.LOCATION);
    addNotAnalyzedString(xbMapping, Rss.CATEGORIES);
    // Enclosures
    xbMapping.startObject(Rss.ENCLOSURES).startObject("properties");
    addNotIndexedString(xbMapping, Rss.Enclosures.URL);
    addNotAnalyzedString(xbMapping, Rss.Enclosures.TYPE);
    addNotIndexedLong(xbMapping, Rss.Enclosures.LENGTH);
    // End Enclosures
    xbMapping.endObject().endObject();
    // Medias
    xbMapping.startObject(Rss.MEDIAS).startObject("properties");
    addNotAnalyzedString(xbMapping, Rss.Medias.TYPE);
    addNotIndexedString(xbMapping, Rss.Medias.REFERENCE);
    addNotAnalyzedString(xbMapping, Rss.Medias.LANGUAGE);
    addAnalyzedString(xbMapping, Rss.Medias.TITLE);
    addAnalyzedString(xbMapping, Rss.Medias.DESCRIPTION);
    addNotIndexedLong(xbMapping, Rss.Medias.DURATION);
    addNotIndexedLong(xbMapping, Rss.Medias.WIDTH);
    addNotIndexedLong(xbMapping, Rss.Medias.HEIGHT);
    // End Medias
    xbMapping.endObject().endObject();
    // Raw content:encoded
    if (raw) {
        xbMapping.startObject(Rss.RAW).startObject("properties");
        addNotIndexedString(xbMapping, Rss.Raw.HTML);
        // End Raw
        xbMapping.endObject().endObject();
    }
    // End Type
    xbMapping.endObject().endObject().endObject();
    return xbMapping;
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)618 IOException (java.io.IOException)126 XContentParser (org.elasticsearch.common.xcontent.XContentParser)122 Settings (org.elasticsearch.common.settings.Settings)60 ArrayList (java.util.ArrayList)59 SearchResponse (org.elasticsearch.action.search.SearchResponse)56 Matchers.containsString (org.hamcrest.Matchers.containsString)53 HashMap (java.util.HashMap)47 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)43 Map (java.util.Map)41 RestRequest (org.elasticsearch.rest.RestRequest)37 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 Test (org.junit.Test)34 RestController (org.elasticsearch.rest.RestController)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)32 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)32 GeoPoint (org.elasticsearch.common.geo.GeoPoint)31 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28