// Path: cassandra-spring/cassandra-spring-lucene/src/main/java/org/javabahia/cassandra/spring/LuceneUtil.java
// public enum LuceneUtil {
// INSTANCE;
// 
// private Directory directory;
// private IndexWriter indexWriter;
// private Analyzer analyzer;
// public Directory getDirectory() {
// 	return directory;
// }
// 
// public Analyzer getAnalyzer() {
// 	return analyzer;
// }
// 
// public IndexWriter getIndexWriter() {
// 	
// 	IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_46, analyzer);
// 
// 	try {
// 		indexWriter = new IndexWriter(directory, indexWriterConfig);
// 	} catch (IOException exception) {
// 		exception.printStackTrace();
// 	}
// 	return indexWriter;
// }
// 
// 
// {
// 		analyzer = new StandardAnalyzer(Version.LUCENE_46);
// 		directory = new RAMDirectory();
// 		Directory hd = getDirecotoryHD();
// 		backup(hd, directory);
// 	
// 		
// }
// 
// 	private Directory getDirecotoryHD() {
// 		File file = new File(System.getProperty("user.home").concat("/lucene/music/"));
// 		if (!file.exists()) {
// 			file.mkdirs();
// 		}
// 		try {
// 			return FSDirectory.open(file);
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 
// 		return null;
// 	}
// 
// public void backupToHD() {
// 	Directory hd = getDirecotoryHD();
// 	backup(directory, hd);
// }
// 
// 	private void backup(Directory deDiretorio, Directory paraDiretoria) {
// 
// 		try {
// 			for (String file : deDiretorio.listAll()) {
// 				deDiretorio.copy(paraDiretoria, file, file, IOContext.DEFAULT);
// 			}
// 		} catch (IOException ioException) {
// 			ioException.printStackTrace();
// 		}
// 	}
// }
// 
// Path: cassandra-spring/cassandra-spring-lucene/src/main/java/org/javabahia/cassandra/spring/model/Music.java
// @Entity
// public class Music implements Serializable{
// 	
// 	private static final long serialVersionUID = -8245568483951712497L;
// 
// 	@Id
// 	private String name;
// 	@Column
// 	private String author;
// 	@Column
// 	private String lyric;
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public void setName(String nome) {
// 		this.name = nome;
// 	}
// 
// 	public String getAuthor() {
// 		return author;
// 	}
// 
// 	public void setAuthor(String autor) {
// 		this.author = autor;
// 	}
// 
// 	public String getLyric() {
// 		return lyric;
// 	}
// 
// 	public void setLyric(String lyric) {
// 		this.lyric = lyric;
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		return ToStringBuilder.reflectionToString(this,
// 				ToStringStyle.SHORT_PREFIX_STYLE);
// 	}
// 
// }


import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.util.Version;
import org.javabahia.cassandra.spring.LuceneUtil;
import org.javabahia.cassandra.spring.model.Music;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
package org.javabahia.cassandra.spring.lucene;




@Service
@Scope("prototype")
public class MusicSearch {

	

	private static final String COLUNM_LYRIC = "lyric";
	private static final String COLUNM_AUTHOR = "Author";
	private static final String COLUMN_NAME = "name";

	public List<String> findMusicByLyric(String lyric) throws ParseException, IOException {
		
		Query query = new QueryParser(Version.LUCENE_46, COLUNM_LYRIC,
				LuceneUtil.INSTANCE.getAnalyzer()).parse(lyric);
		
		return returnMusics(query);
	}
	
	public List<String> findMusicByAuthor(String author) throws ParseException, IOException {
		Term term = new Term(COLUNM_AUTHOR, author);
		Query query = new TermQuery(term);
		return returnMusics(query);
	}

	private List<String> returnMusics(Query query) throws IOException {
		int hitsPerPage = 10;
		IndexReader reader = DirectoryReader.open(LuceneUtil.INSTANCE.getDirectory());
		IndexSearcher searcher = new IndexSearcher(reader);
		TopScoreDocCollector collector = TopScoreDocCollector.create(
				hitsPerPage, true);
		searcher.search(query, collector);
		ScoreDoc[] hits = collector.topDocs().scoreDocs;
		
		   
		   List<String> musics = new LinkedList<>();
		    for(int i=0;i<hits.length;++i) {
// Path: cassandra-spring/cassandra-spring-lucene/src/main/java/org/javabahia/cassandra/spring/LuceneUtil.java
// public enum LuceneUtil {
// INSTANCE;
// 
// private Directory directory;
// private IndexWriter indexWriter;
// private Analyzer analyzer;
// public Directory getDirectory() {
// 	return directory;
// }
// 
// public Analyzer getAnalyzer() {
// 	return analyzer;
// }
// 
// public IndexWriter getIndexWriter() {
// 	
// 	IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_46, analyzer);
// 
// 	try {
// 		indexWriter = new IndexWriter(directory, indexWriterConfig);
// 	} catch (IOException exception) {
// 		exception.printStackTrace();
// 	}
// 	return indexWriter;
// }
// 
// 
// {
// 		analyzer = new StandardAnalyzer(Version.LUCENE_46);
// 		directory = new RAMDirectory();
// 		Directory hd = getDirecotoryHD();
// 		backup(hd, directory);
// 	
// 		
// }
// 
// 	private Directory getDirecotoryHD() {
// 		File file = new File(System.getProperty("user.home").concat("/lucene/music/"));
// 		if (!file.exists()) {
// 			file.mkdirs();
// 		}
// 		try {
// 			return FSDirectory.open(file);
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 
// 		return null;
// 	}
// 
// public void backupToHD() {
// 	Directory hd = getDirecotoryHD();
// 	backup(directory, hd);
// }
// 
// 	private void backup(Directory deDiretorio, Directory paraDiretoria) {
// 
// 		try {
// 			for (String file : deDiretorio.listAll()) {
// 				deDiretorio.copy(paraDiretoria, file, file, IOContext.DEFAULT);
// 			}
// 		} catch (IOException ioException) {
// 			ioException.printStackTrace();
// 		}
// 	}
// }
// 
// Path: cassandra-spring/cassandra-spring-lucene/src/main/java/org/javabahia/cassandra/spring/model/Music.java
// @Entity
// public class Music implements Serializable{
// 	
// 	private static final long serialVersionUID = -8245568483951712497L;
// 
// 	@Id
// 	private String name;
// 	@Column
// 	private String author;
// 	@Column
// 	private String lyric;
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public void setName(String nome) {
// 		this.name = nome;
// 	}
// 
// 	public String getAuthor() {
// 		return author;
// 	}
// 
// 	public void setAuthor(String autor) {
// 		this.author = autor;
// 	}
// 
// 	public String getLyric() {
// 		return lyric;
// 	}
// 
// 	public void setLyric(String lyric) {
// 		this.lyric = lyric;
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		return ToStringBuilder.reflectionToString(this,
// 				ToStringStyle.SHORT_PREFIX_STYLE);
// 	}
// 
// }


// Path: cassandra-spring/cassandra-spring-lucene/src/main/java/org/javabahia/cassandra/spring/lucene/MusicSearch.java
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.util.Version;
import org.javabahia.cassandra.spring.LuceneUtil;
import org.javabahia.cassandra.spring.model.Music;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

package org.javabahia.cassandra.spring.lucene;




@Service
@Scope("prototype")
public class MusicSearch {

	

	private static final String COLUNM_LYRIC = "lyric";
	private static final String COLUNM_AUTHOR = "Author";
	private static final String COLUMN_NAME = "name";

	public List<String> findMusicByLyric(String lyric) throws ParseException, IOException {
		
		Query query = new QueryParser(Version.LUCENE_46, COLUNM_LYRIC,
				LuceneUtil.INSTANCE.getAnalyzer()).parse(lyric);
		
		return returnMusics(query);
	}
	
	public List<String> findMusicByAuthor(String author) throws ParseException, IOException {
		Term term = new Term(COLUNM_AUTHOR, author);
		Query query = new TermQuery(term);
		return returnMusics(query);
	}

	private List<String> returnMusics(Query query) throws IOException {
		int hitsPerPage = 10;
		IndexReader reader = DirectoryReader.open(LuceneUtil.INSTANCE.getDirectory());
		IndexSearcher searcher = new IndexSearcher(reader);
		TopScoreDocCollector collector = TopScoreDocCollector.create(
				hitsPerPage, true);
		searcher.search(query, collector);
		ScoreDoc[] hits = collector.topDocs().scoreDocs;
		
		   
		   List<String> musics = new LinkedList<>();
		    for(int i=0;i<hits.length;++i) {
		      int docId = hits[i].doc;