Skip to content

Instantly share code, notes, and snippets.

@eddking
Created October 13, 2013 18:24
Show Gist options
  • Save eddking/6965578 to your computer and use it in GitHub Desktop.
Save eddking/6965578 to your computer and use it in GitHub Desktop.
Sample usage code for a Rigel presentation at Semantico
package com.semantico.rigel;
import static com.semantico.rigel.transformers.CommonTransformers.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.google.common.collect.ListMultimap;
import com.semantico.rigel.fields.types.DateField;
import com.semantico.rigel.fields.types.IntegerField;
import com.semantico.rigel.fields.types.StringField;
import com.semantico.rigel.filters.Filter;
import com.semantico.rigel.test.items.Author;
public class PresentationSamples {
public static final StringField ID = new StringField("s2_id");
public static final StringField TYPE = new StringField("s2_type");
public static final StringField TAG = new StringField("tag");
public static final StringField AUTHOR = new StringField("dc_creator");
public static final IntegerField SCENE_COUNT = new IntegerField("scene_count");
public static final DateField DATE = new DateField("dc_issued");
public static class Play extends ContentItem {
public static class Schema extends ContentItem.Schema<Play> {
public final FieldKey<?, String> type;
public final FieldKey<?, List<Author>> author;
public final FieldKey<?, Optional<Date>> date;
public final FieldKey<?, Integer> sceneCount;
public final FieldKey<?, Set<String>> tags;
public Schema() {
super(ID);
type = field(TYPE).build();
author = field(AUTHOR.multivalued())
.transform(asList(Author.parse()))
.build();
date = field(DATE)
.transform(optional(Date.class))
.build();
sceneCount = field(SCENE_COUNT)
.transform(defaultValue(0))
.build();
tags = field(TAG.multivalued())
.transform(asSet(String.class))
.build();
filter(TYPE.equalTo("play"));
}
@Override
public Play create(Map<DataKey<?>, ? super Object> data) {
return new Play(this, data);
}
}
private final Schema schema;
public Play(Schema schema, Map<DataKey<?>, ? super Object> data) {
super(schema, data);
this.schema = schema;
}
public String getType() {
return get(schema.type);
}
public List<Author> getAuthors() {
return get(schema.author);
}
public Optional<Date> getDate() {
return get(schema.date);
}
public Integer getSceneCount() {
return get(schema.sceneCount);
}
public Set<String> getTags() {
return get(schema.tags);
}
}
public void doThings() {
Play.Schema play = new Play.Schema();
RigelContext rigel = RigelContext.builder()
.registerSchemas(play)
.build();
ContentRepository<Play> plays = rigel.getContentRepository(play);
List<Play> allPlays = plays.all().get();
List<Play> top20NewPlays = plays.all()
.filter(TAG.equalTo("new"))
.orderBy(DATE, ORDER.desc)
.limit(20)
.get();
List<Play> lotsOfScenes = plays.all()
.filter(SCENE_COUNT.greaterThan(10))
.get();
List<Play> playsByEdd = plays.all()
.filter(AUTHOR.equalTo("Edd"))
.get();
Set<String> ids = asSet(play.id).apply(playsByEdd);
Optional<Play> play1 = plays.id("play1").get();
Map<String, Optional<Play>> playsByid = plays.ids("play1", "play2").get();
ListMultimap<String, Play> shortPlaysByAuthor = plays.groupBy(AUTHOR)
.limitResultsPerGroup(3)
.filter(SCENE_COUNT.atLeast(1).andAtMost(10))
.get();
SCENE_COUNT.lessThan(5).and(AUTHOR.startsWith("Edd")).group().or(SCENE_COUNT.atLeast(5));
DATE.greaterThan(new Date()).or(TAG.equalTo("Future").or(TAG.equalTo("Year 3000")).group());
}
public class ExampleSearch extends Search {
public ExampleSearch() {
facet(DATE).build();
rangeFacet(SCENE_COUNT)
.start(0)
.gap(1)
.end(10);
}
}
public class Schema extends ContentItem.Schema<PlayCollection> {
public Schema() {
plays = field(CHILD_IDS.multivalued())
.transform(asList(foreignKeyField(Play.class)))
.build()
}
public List<Play> getPlays() {
return get(schema.plays);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment