// Path: lucene/src/main/java/de/fhopf/lucenesolrtalk/Result.java
// public class Result {
// 
//     private final String title;
//     private final String excerpt;
//     private final List<String> categories;
//     private final List<String> speakers;
//     private final Date date;
// 
//     public Result(String title, String excerpt, List<String> categories, List<String> speakers, Date date) {
//         this.title = title;
//         this.excerpt = excerpt;
//         this.categories = categories;
//         this.speakers = speakers;
//         this.date = date;
//     }
// 
//     public String getTitle() {
//         return title;
//     }
// 
//     public String getExcerpt() {
//         return excerpt;
//     }
// 
//     public List<String> getCategories() {
//         return categories;
//     }
// 
//     public List<String> getSpeakers() {
//         return speakers;
//     }
// 
//     public Date getDate() {
//         return date;
//     }
// }
// 
// Path: web/src/main/java/de/fhopf/lucenesolrtalk/web/Facet.java
// public class Facet {
//     
//     private final String term;
//     private final long count;
// 
//     private final String fq;
// 
//     private Facet(String displayTerm, long count, String fq) {
//         this.term = displayTerm;
//         this.count = count;
//         this.fq = fq;
//     }
// 
//     public static Facet termFacet(String term, long count, String fieldname) {
//         return new Facet(term, count, fieldname.concat(":").concat(ClientUtils.escapeQueryChars(term)));
//     }
//     
//     public static Facet withFilterQuery(String term, long count, String fq) {
//         return new Facet(term, count, fq);
//     }
//     
//     public String getTerm() {
//         return term;
//     }
// 
//     public long getCount() {
//         return count;
//     }
// 
//     public String getFq() {
//         return fq;
//     }
// }
// 
// Path: web/src/main/java/de/fhopf/lucenesolrtalk/web/Faceting.java
// public class Faceting {
// 
//     private final List<Facet> categoryFacet;
//     private final List<Facet> speakerFacet;
//     private final List<Facet> dateFacet;
//     private final List<Facet> organizerFacet;
// 
//     public Faceting(List<Facet> categoryFacet, List<Facet> speakerFacet, List<Facet> dateFacet, List<Facet> organizerFacet) {
//         this.categoryFacet = categoryFacet;
//         this.speakerFacet = speakerFacet;
//         this.dateFacet = dateFacet;
//         this.organizerFacet = organizerFacet;
//     }
// 
//     public List<Facet> getCategoryFacet() {
//         return categoryFacet;
//     }
// 
//     public List<Facet> getSpeakerFacet() {
//         return speakerFacet;
//     }
//     
//     public List<Facet> getDateFacet() {
//         return dateFacet;
//     }
// 
//     public List<Facet> getOrganizerFacet() {
//         return organizerFacet;
//     }
// }


import de.fhopf.lucenesolrtalk.Result;
import de.fhopf.lucenesolrtalk.web.Facet;
import de.fhopf.lucenesolrtalk.web.Faceting;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.joda.time.format.ISODateTimeFormat;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.facet.FacetBuilders;
import org.elasticsearch.search.facet.datehistogram.DateHistogramFacet;
import org.elasticsearch.search.facet.terms.TermsFacet;
import org.elasticsearch.search.highlight.HighlightField;
package de.fhopf.lucenesolrtalk.web.elasticsearch;


/**
 * Retrieves results from elasticsearch.
 */
public class ElasticsearchSearcher {

    private final Client client;
    static final String INDEX = "jug";

    public ElasticsearchSearcher(Client client) {
        this.client = client;
    }

    public SearchResponse search(String token) {
        return search(token, Collections.<String>emptyList());
    }

    public List<Result> getResults(SearchResponse response) {
        List<Result> results = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            Result res = new Result(getTitle(hit), getExcerpt(hit), getCategories(hit), getAuthors(hit), getDate(hit));
            results.add(res);
        }

        return results;
    }

    public Faceting getFacets(SearchResponse response) {

// Path: lucene/src/main/java/de/fhopf/lucenesolrtalk/Result.java
// public class Result {
// 
//     private final String title;
//     private final String excerpt;
//     private final List<String> categories;
//     private final List<String> speakers;
//     private final Date date;
// 
//     public Result(String title, String excerpt, List<String> categories, List<String> speakers, Date date) {
//         this.title = title;
//         this.excerpt = excerpt;
//         this.categories = categories;
//         this.speakers = speakers;
//         this.date = date;
//     }
// 
//     public String getTitle() {
//         return title;
//     }
// 
//     public String getExcerpt() {
//         return excerpt;
//     }
// 
//     public List<String> getCategories() {
//         return categories;
//     }
// 
//     public List<String> getSpeakers() {
//         return speakers;
//     }
// 
//     public Date getDate() {
//         return date;
//     }
// }
// 
// Path: web/src/main/java/de/fhopf/lucenesolrtalk/web/Facet.java
// public class Facet {
//     
//     private final String term;
//     private final long count;
// 
//     private final String fq;
// 
//     private Facet(String displayTerm, long count, String fq) {
//         this.term = displayTerm;
//         this.count = count;
//         this.fq = fq;
//     }
// 
//     public static Facet termFacet(String term, long count, String fieldname) {
//         return new Facet(term, count, fieldname.concat(":").concat(ClientUtils.escapeQueryChars(term)));
//     }
//     
//     public static Facet withFilterQuery(String term, long count, String fq) {
//         return new Facet(term, count, fq);
//     }
//     
//     public String getTerm() {
//         return term;
//     }
// 
//     public long getCount() {
//         return count;
//     }
// 
//     public String getFq() {
//         return fq;
//     }
// }
// 
// Path: web/src/main/java/de/fhopf/lucenesolrtalk/web/Faceting.java
// public class Faceting {
// 
//     private final List<Facet> categoryFacet;
//     private final List<Facet> speakerFacet;
//     private final List<Facet> dateFacet;
//     private final List<Facet> organizerFacet;
// 
//     public Faceting(List<Facet> categoryFacet, List<Facet> speakerFacet, List<Facet> dateFacet, List<Facet> organizerFacet) {
//         this.categoryFacet = categoryFacet;
//         this.speakerFacet = speakerFacet;
//         this.dateFacet = dateFacet;
//         this.organizerFacet = organizerFacet;
//     }
// 
//     public List<Facet> getCategoryFacet() {
//         return categoryFacet;
//     }
// 
//     public List<Facet> getSpeakerFacet() {
//         return speakerFacet;
//     }
//     
//     public List<Facet> getDateFacet() {
//         return dateFacet;
//     }
// 
//     public List<Facet> getOrganizerFacet() {
//         return organizerFacet;
//     }
// }


// Path: web/src/main/java/de/fhopf/lucenesolrtalk/web/elasticsearch/ElasticsearchSearcher.java
import de.fhopf.lucenesolrtalk.Result;
import de.fhopf.lucenesolrtalk.web.Facet;
import de.fhopf.lucenesolrtalk.web.Faceting;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.joda.time.format.ISODateTimeFormat;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.facet.FacetBuilders;
import org.elasticsearch.search.facet.datehistogram.DateHistogramFacet;
import org.elasticsearch.search.facet.terms.TermsFacet;
import org.elasticsearch.search.highlight.HighlightField;

package de.fhopf.lucenesolrtalk.web.elasticsearch;


/**
 * Retrieves results from elasticsearch.
 */
public class ElasticsearchSearcher {

    private final Client client;
    static final String INDEX = "jug";

    public ElasticsearchSearcher(Client client) {
        this.client = client;
    }

    public SearchResponse search(String token) {
        return search(token, Collections.<String>emptyList());
    }

    public List<Result> getResults(SearchResponse response) {
        List<Result> results = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            Result res = new Result(getTitle(hit), getExcerpt(hit), getCategories(hit), getAuthors(hit), getDate(hit));
            results.add(res);
        }

        return results;
    }

    public Faceting getFacets(SearchResponse response) {

        return new Faceting(buildFacets(response, "category"), buildFacets(response, "speaker"), buildYearFacets(response, "date"), buildFacets(response, "organizer"));