// Path: src/main/java/net/canadensys/dataportal/occurrence/dao/OccurrenceAutoCompleteDAO.java
// public interface OccurrenceAutoCompleteDAO {
// 	
// 	/**
// 	 * Returns suggestions as JSON string for a field and a current value.
// 	 * @param field
// 	 * @param currValue
// 	 * @param useSanitizedValue compare with sanitized value instead of real value
// 	 * @return result as JSON string
// 	 */
// 	public String getSuggestionsFor(String field, String currValue, boolean useSanitizedValue);
// 	
// 	/**
// 	 * Returns all possible values as UniqueValuesModel list.
// 	 * @param field
// 	 * @return
// 	 */
// 	public List<UniqueValuesModel> getAllPossibleValues(String field);
// 
// }
// 
// Path: src/main/java/net/canadensys/dataportal/occurrence/model/UniqueValuesModel.java
// @Entity
// @Table(name = "unique_values")
// public class UniqueValuesModel {
// 	
// 	@Id
// 	private int id;
// 	
// 	@Column(name = "key")
// 	private String key;
// 	
// 	@Column(name = "occurrence_count")
// 	private int occurrence_count;
// 	
// 	@Column(name = "value")
// 	private String value;
// 
// 	@Column(name = "unaccented_value")
// 	private String unaccented_value;
// 	
// 	public int getId() {
// 		return id;
// 	}
// 	public void setId(int id) {
// 		this.id = id;
// 	}
// 	
// 	public String getKey() {
// 		return key;
// 	}
// 	public void setKey(String key) {
// 		this.key = key;
// 	}
// 	
// 	public int getOccurrence_count() {
// 		return occurrence_count;
// 	}
// 	public void setOccurrence_count(int occurrence_count) {
// 		this.occurrence_count = occurrence_count;
// 	}
// 	
// 	public String getValue() {
// 		return value;
// 	}
// 	public void setValue(String value) {
// 		this.value = value;
// 	}
// 	
// 	public String getUnaccented_value() {
// 		return unaccented_value;
// 	}
// 	public void setUnaccented_value(String unaccented_value) {
// 		this.unaccented_value = unaccented_value;
// 	}
// }


import java.util.List;
import net.canadensys.dataportal.occurrence.dao.OccurrenceAutoCompleteDAO;
import net.canadensys.dataportal.occurrence.model.UniqueValuesModel;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
package net.canadensys.dataportal.occurrence.dao.impl;




/**
 * WORK IN PROGRESS
 * Requires ElasticSearch > 1.0
 * @author cgendreau
 *
 */
public class ElasticSearchOccurrenceAutoCompleteDAO implements OccurrenceAutoCompleteDAO{
	
	private static final String INDEX_NAME = "portal";
	private static final String OCCURRENCE_TYPE = "occurrence";
	
	private static final int MAX_SIZE = 10;
	
	@Autowired
	@Qualifier("esClient")
	private Client client;
	
//	curl -X GET 'localhost:9200/portal/_search?pretty' -d '{
//	  "query":{
//	    "match" : {
//	        "country.autocomplete" : "Ca"
//	    }
//	  },
//	  "aggs" : {
//	      "countries" : {
//	          "terms" : { "field" : "country" }
//	      }
//	  }
//	}'

	@Override
	public String getSuggestionsFor(String field, String currValue, boolean useSanitizedValue) {
		
		SearchRequestBuilder srb = client.prepareSearch(INDEX_NAME)
		        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
		        .setQuery(QueryBuilders.matchQuery("country.autocomplete", currValue))
		        //TODO add aggregation, requires ES > 1.0
	            .setSize(MAX_SIZE)
	            .setTypes(OCCURRENCE_TYPE);

		SearchResponse response = srb
		        .execute()
		        .actionGet();
		response.getHits();
		
		return null;
	}

	@Override
// Path: src/main/java/net/canadensys/dataportal/occurrence/dao/OccurrenceAutoCompleteDAO.java
// public interface OccurrenceAutoCompleteDAO {
// 	
// 	/**
// 	 * Returns suggestions as JSON string for a field and a current value.
// 	 * @param field
// 	 * @param currValue
// 	 * @param useSanitizedValue compare with sanitized value instead of real value
// 	 * @return result as JSON string
// 	 */
// 	public String getSuggestionsFor(String field, String currValue, boolean useSanitizedValue);
// 	
// 	/**
// 	 * Returns all possible values as UniqueValuesModel list.
// 	 * @param field
// 	 * @return
// 	 */
// 	public List<UniqueValuesModel> getAllPossibleValues(String field);
// 
// }
// 
// Path: src/main/java/net/canadensys/dataportal/occurrence/model/UniqueValuesModel.java
// @Entity
// @Table(name = "unique_values")
// public class UniqueValuesModel {
// 	
// 	@Id
// 	private int id;
// 	
// 	@Column(name = "key")
// 	private String key;
// 	
// 	@Column(name = "occurrence_count")
// 	private int occurrence_count;
// 	
// 	@Column(name = "value")
// 	private String value;
// 
// 	@Column(name = "unaccented_value")
// 	private String unaccented_value;
// 	
// 	public int getId() {
// 		return id;
// 	}
// 	public void setId(int id) {
// 		this.id = id;
// 	}
// 	
// 	public String getKey() {
// 		return key;
// 	}
// 	public void setKey(String key) {
// 		this.key = key;
// 	}
// 	
// 	public int getOccurrence_count() {
// 		return occurrence_count;
// 	}
// 	public void setOccurrence_count(int occurrence_count) {
// 		this.occurrence_count = occurrence_count;
// 	}
// 	
// 	public String getValue() {
// 		return value;
// 	}
// 	public void setValue(String value) {
// 		this.value = value;
// 	}
// 	
// 	public String getUnaccented_value() {
// 		return unaccented_value;
// 	}
// 	public void setUnaccented_value(String unaccented_value) {
// 		this.unaccented_value = unaccented_value;
// 	}
// }


// Path: src/main/java/net/canadensys/dataportal/occurrence/dao/impl/ElasticSearchOccurrenceAutoCompleteDAO.java
import java.util.List;
import net.canadensys.dataportal.occurrence.dao.OccurrenceAutoCompleteDAO;
import net.canadensys.dataportal.occurrence.model.UniqueValuesModel;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

package net.canadensys.dataportal.occurrence.dao.impl;




/**
 * WORK IN PROGRESS
 * Requires ElasticSearch > 1.0
 * @author cgendreau
 *
 */
public class ElasticSearchOccurrenceAutoCompleteDAO implements OccurrenceAutoCompleteDAO{
	
	private static final String INDEX_NAME = "portal";
	private static final String OCCURRENCE_TYPE = "occurrence";
	
	private static final int MAX_SIZE = 10;
	
	@Autowired
	@Qualifier("esClient")
	private Client client;
	
//	curl -X GET 'localhost:9200/portal/_search?pretty' -d '{
//	  "query":{
//	    "match" : {
//	        "country.autocomplete" : "Ca"
//	    }
//	  },
//	  "aggs" : {
//	      "countries" : {
//	          "terms" : { "field" : "country" }
//	      }
//	  }
//	}'

	@Override
	public String getSuggestionsFor(String field, String currValue, boolean useSanitizedValue) {
		
		SearchRequestBuilder srb = client.prepareSearch(INDEX_NAME)
		        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
		        .setQuery(QueryBuilders.matchQuery("country.autocomplete", currValue))
		        //TODO add aggregation, requires ES > 1.0
	            .setSize(MAX_SIZE)
	            .setTypes(OCCURRENCE_TYPE);

		SearchResponse response = srb
		        .execute()
		        .actionGet();
		response.getHits();
		
		return null;
	}

	@Override
	public List<UniqueValuesModel> getAllPossibleValues(String field) {