// Path: src/main/java/org/dice/solrenhancements/TermExtractionHelper.java
// public class TermExtractionHelper {
// 
//     public static List<String> getTermsFromField(Analyzer analyzer, IndexableField field) throws IOException {
//         TokenStream ts = null;
//         try {
//             ArrayList<String> terms = new ArrayList<String>();
// 
//             ts = field.tokenStream(analyzer, ts);
//             CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
//             ts.reset();
//             while (ts.incrementToken()) {
//                 terms.add(termAtt.toString());
//             }
//             ts.end();
//             return terms;
//         }
//         finally {
//             if(ts != null){
//                 IOUtils.closeWhileHandlingException(ts);
//             }
//         }
//     }
// 
//     public static List<String> getTermsFromString(Analyzer analyzer, String fieldName, String fieldValue)
//             throws IOException {
// 
//         final StringReader reader = new StringReader(fieldValue);
//         ArrayList<String> terms = new ArrayList<String>();
//         TokenStream ts = analyzer.tokenStream(fieldName, reader);
//         try {
//             // for every token
//             CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
//             ts.reset();
//             while (ts.incrementToken()) {
//                 String term = termAtt.toString();
//                 terms.add(term);
//             }
//             ts.end();
//         } finally {
//             IOUtils.closeWhileHandlingException(ts);
//         }
//         return terms;
//     }
// 
//     public static List<String> getTermsFromTermVectorField(Terms vector) throws IOException {
//         ArrayList<String> terms = new ArrayList<String>();
// 
//         final TermsEnum termsEnum = vector.iterator();
//         CharsRefBuilder spare = new CharsRefBuilder();
//         BytesRef text;
// 
//         while((text = termsEnum.next()) != null) {
//             spare.copyUTF8Bytes(text);
//             final String term = spare.toString();
//             terms.add(term);
//         }
//         return terms;
//     }
// }


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.*;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.docvalues.FloatDocValues;
import org.dice.solrenhancements.TermExtractionHelper;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
package org.dice.solrenhancements.functionqueries;



/**
 * Created by simon.hughes on 2/20/17.
 */
public class TermIntersectsValueSource extends ValueSource {

    private final String indexedField;
    private final Analyzer analyzer;
    private final Set<String> valuesToIntersect;
    private final String similarity;

    public TermIntersectsValueSource(String indexedField, Analyzer analyzer, Set<String> valuesToIntersect, String similarity) {
        this.indexedField = indexedField;
        this.analyzer = analyzer;
        this.valuesToIntersect = valuesToIntersect;
        this.similarity = similarity;
    }

    public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
        final IndexReader ir = readerContext.reader();
        final String indexedField = this.indexedField;

        return new FloatDocValues(this) {
            public float floatVal(int docNum) {
                if(valuesToIntersect.size() == 0){
                    return 0;
                }

                HashSet<String> fieldValues = null;
                try {
                    final Fields vectors= ir.getTermVectors(docNum);
                    if (vectors != null) {
                        if (vectors != null) {
                            Terms vector = vectors.terms(indexedField);
                            if (vector != null) {
// Path: src/main/java/org/dice/solrenhancements/TermExtractionHelper.java
// public class TermExtractionHelper {
// 
//     public static List<String> getTermsFromField(Analyzer analyzer, IndexableField field) throws IOException {
//         TokenStream ts = null;
//         try {
//             ArrayList<String> terms = new ArrayList<String>();
// 
//             ts = field.tokenStream(analyzer, ts);
//             CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
//             ts.reset();
//             while (ts.incrementToken()) {
//                 terms.add(termAtt.toString());
//             }
//             ts.end();
//             return terms;
//         }
//         finally {
//             if(ts != null){
//                 IOUtils.closeWhileHandlingException(ts);
//             }
//         }
//     }
// 
//     public static List<String> getTermsFromString(Analyzer analyzer, String fieldName, String fieldValue)
//             throws IOException {
// 
//         final StringReader reader = new StringReader(fieldValue);
//         ArrayList<String> terms = new ArrayList<String>();
//         TokenStream ts = analyzer.tokenStream(fieldName, reader);
//         try {
//             // for every token
//             CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
//             ts.reset();
//             while (ts.incrementToken()) {
//                 String term = termAtt.toString();
//                 terms.add(term);
//             }
//             ts.end();
//         } finally {
//             IOUtils.closeWhileHandlingException(ts);
//         }
//         return terms;
//     }
// 
//     public static List<String> getTermsFromTermVectorField(Terms vector) throws IOException {
//         ArrayList<String> terms = new ArrayList<String>();
// 
//         final TermsEnum termsEnum = vector.iterator();
//         CharsRefBuilder spare = new CharsRefBuilder();
//         BytesRef text;
// 
//         while((text = termsEnum.next()) != null) {
//             spare.copyUTF8Bytes(text);
//             final String term = spare.toString();
//             terms.add(term);
//         }
//         return terms;
//     }
// }


// Path: src/main/java/org/dice/solrenhancements/functionqueries/TermIntersectsValueSource.java
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.*;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.docvalues.FloatDocValues;
import org.dice.solrenhancements.TermExtractionHelper;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

package org.dice.solrenhancements.functionqueries;



/**
 * Created by simon.hughes on 2/20/17.
 */
public class TermIntersectsValueSource extends ValueSource {

    private final String indexedField;
    private final Analyzer analyzer;
    private final Set<String> valuesToIntersect;
    private final String similarity;

    public TermIntersectsValueSource(String indexedField, Analyzer analyzer, Set<String> valuesToIntersect, String similarity) {
        this.indexedField = indexedField;
        this.analyzer = analyzer;
        this.valuesToIntersect = valuesToIntersect;
        this.similarity = similarity;
    }

    public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
        final IndexReader ir = readerContext.reader();
        final String indexedField = this.indexedField;

        return new FloatDocValues(this) {
            public float floatVal(int docNum) {
                if(valuesToIntersect.size() == 0){
                    return 0;
                }

                HashSet<String> fieldValues = null;
                try {
                    final Fields vectors= ir.getTermVectors(docNum);
                    if (vectors != null) {
                        if (vectors != null) {
                            Terms vector = vectors.terms(indexedField);
                            if (vector != null) {
                                fieldValues = new HashSet<String>(TermExtractionHelper.getTermsFromTermVectorField(vector));