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")));
}
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));
}
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")));
}
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();
}
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;
}
Aggregations