Example usage for org.apache.lucene.index IndexReader close

List of usage examples for org.apache.lucene.index IndexReader close

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader close.

Prototype

@Override
public final synchronized void close() throws IOException 

Source Link

Document

Closes files associated with this index.

Usage

From source file:edu.ur.ir.person.service.DefaultNameAuthoritySearchService.java

License:Apache License

/**
 *  Execute the search//from w  w  w.j  a  va2 s.c  o  m
 * @see edu.ur.ir.person.NameAuthoritySearchService#search(Repository, String, int, int)
 */
public SearchResults<PersonNameAuthority> search(File nameAuthorityIndex, String query, int offset,
        int numResults) {

    SearchResults<PersonNameAuthority> nameSearchResults = new SearchResults<PersonNameAuthority>();
    nameSearchResults.setOriginalQuery(query);
    query = SearchHelper.prepareMainSearchString(query, false);
    List<PersonNameAuthority> personNameAurhorities = new LinkedList<PersonNameAuthority>();

    if (log.isDebugEnabled()) {
        log.debug("Name search results executing query " + query);
    }

    // If the name index folder doesnot exist
    // then just return empty results
    if (nameAuthorityIndex == null) {
        return nameSearchResults;
    }

    String indexFolder = nameAuthorityIndex.getAbsolutePath();

    IndexSearcher searcher = null;
    IndexReader reader = null;
    try {
        FSDirectory directory = FSDirectory.open(new File(indexFolder));
        reader = IndexReader.open(directory, true);
        searcher = new IndexSearcher(reader);

        QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);

        Query luceneQuery = parser.parse(query);
        TopDocs hits = searcher.search(luceneQuery, 1000);
        nameSearchResults.setTotalHits(hits.totalHits);

        log.debug(" No. of hits = " + hits.totalHits + " offset=" + offset + "  numResults=" + numResults);
        int position = offset;
        int addedResults = 0;
        while (hits.totalHits > position && (addedResults < numResults)) {
            if (log.isDebugEnabled()) {
                log.debug(" adding document at position " + position);
            }
            Document d = searcher.doc(hits.scoreDocs[position].doc);

            personNameAurhorities.add(getPersonNameAuthority(d));
            position += 1;
            addedResults += 1;
        }
    } catch (Exception e) {
        log.error(e);
    } finally {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
                log.error("the searcher could not be closed", e);
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                log.error("the reader could not be closed", e);
            }
        }
    }
    nameSearchResults.setObjects(personNameAurhorities);

    return nameSearchResults;
}

From source file:edu.ur.ir.researcher.service.DefaultResearcherSearchService.java

License:Apache License

/**
 * Get the facets and results/*w  w w  . j av  a2s.co  m*/
 * @see edu.ur.ir.researcher.ResearcherSearchService#executeSearchWithFacets(java.lang.String, java.lang.String, int, int, int, int)
 */
public FacetSearchHelper executeSearchWithFacets(String mainQueryString, String indexFolder,
        int numberOfHitsToProcessForFacets, int numberOfResultsToCollectForFacets, int numberOfFactsToShow,
        int numberOfIdsToCollect, int idsToCollectStartPosition)
        throws CorruptIndexException, IOException, ParseException {
    if (searchDirectoryIsEmpty(indexFolder) || isInvalidQuery(mainQueryString)) {
        return new FacetSearchHelper(new HashSet<Long>(), 0, new HashMap<String, Collection<FacetResult>>(),
                mainQueryString);
    }

    FSDirectory directory = FSDirectory.open(new File(indexFolder));
    IndexReader reader = IndexReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader);

    QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
    parser.setDefaultOperator(QueryParser.AND_OPERATOR);

    HashMap<String, Collection<FacetResult>> facetResults = new HashMap<String, Collection<FacetResult>>();

    // execute the main query - we will use this to extract data to determine the facet searches
    String executedQuery = SearchHelper.prepareMainSearchString(mainQueryString, true);
    Query mainQuery = parser.parse(executedQuery);
    if (log.isDebugEnabled()) {
        log.debug("main query = " + executedQuery);
    }

    TopDocs hits = searcher.search(mainQuery, maxNumberOfMainQueryHits);

    // determine the set of data we should use to determine facets
    HashMap<String, HashMap<String, FacetResult>> possibleFacets = this.generateFacetSearches(hits,
            numberOfHitsToProcessForFacets, numberOfResultsToCollectForFacets, searcher);

    QueryWrapperFilter mainQueryWrapper = new QueryWrapperFilter(mainQuery);
    log.debug("executeSearchWithFacets 1 query = " + mainQuery);

    // get the bitset for main query
    DocIdSet mainQueryBits = mainQueryWrapper.getDocIdSet(reader);

    // process the data and determine the facets
    FacetSearchHelper helper = this.processPossibleFacets(possibleFacets, reader, mainQueryBits, facetResults,
            hits, numberOfIdsToCollect, idsToCollectStartPosition, numberOfFactsToShow, mainQueryString,
            searcher);
    helper.setExecutedQuery(executedQuery);
    searcher.close();
    reader.close();
    return helper;
}

From source file:edu.ur.ir.researcher.service.DefaultResearcherSearchService.java

License:Apache License

public FacetSearchHelper executeSearchWithFacets(String mainQueryString, List<FacetFilter> filters,
        String indexFolder, int numberOfHitsToProcessForFacets, int numberOfResultsToCollectForFacets,
        int numberOfFactsToShow, int numberOfIdsToCollect, int idsToCollectStartPosition)
        throws CorruptIndexException, IOException, ParseException {

    if (searchDirectoryIsEmpty(indexFolder) || isInvalidQuery(mainQueryString)) {
        return new FacetSearchHelper(new HashSet<Long>(), 0, new HashMap<String, Collection<FacetResult>>(),
                mainQueryString);//  w  w w.  j  a  v a 2 s.  c om
    }

    FSDirectory directory = FSDirectory.open(new File(indexFolder));
    IndexReader reader = IndexReader.open(directory, true);
    IndexSearcher searcher = new IndexSearcher(reader);

    QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
    parser.setDefaultOperator(QueryParser.AND_OPERATOR);

    HashMap<String, Collection<FacetResult>> facetResults = new HashMap<String, Collection<FacetResult>>();

    // execute the main query - we will use this to extract data to determine the facet searches
    String executedQuery = SearchHelper.prepareMainSearchString(mainQueryString, true);

    if (log.isDebugEnabled()) {
        log.debug("parsed query = " + executedQuery.trim());
    }
    Query mainQuery = parser.parse(executedQuery);

    //create a filter for the main query
    QueryWrapperFilter mainQueryWrapper = new QueryWrapperFilter(mainQuery);

    // get the bitset for main query
    DocIdSet mainQueryBits = mainQueryWrapper.getDocIdSet(reader);

    TopDocs hits = null;

    if (filters.size() > 0) {
        // create a filter that will match the main query plus all other filters
        List<Filter> luceneFilters = getSubQueryFilters(filters, searcher);
        Filter filter = new ChainedFilter(luceneFilters.toArray(new Filter[luceneFilters.size()]),
                ChainedFilter.AND);
        if (log.isDebugEnabled()) {
            log.debug("filter = " + filter);
        }

        // apply the facets and include them in the main query bit set
        DocIdSet filterQueryBits = filter.getDocIdSet(reader);

        // apply the facets and include them in the main query bit set
        OpenBitSetDISI mainQueryBitSet = new OpenBitSetDISI(mainQueryBits.iterator(), maxNumberOfMainQueryHits);
        OpenBitSetDISI filterBitSet = new OpenBitSetDISI(filterQueryBits.iterator(), maxNumberOfMainQueryHits);
        mainQueryBitSet.and(filterBitSet);

        hits = searcher.search(mainQuery, filter, maxNumberOfMainQueryHits);
        log.debug(" executeSearchWithFacets 2 = mainQuery = " + executedQuery + " filter = " + filter);
    } else {
        hits = searcher.search(mainQuery, maxNumberOfMainQueryHits);
        log.debug(" executeSearchWithFacets 3 = mainQuery = " + mainQuery);

    }

    // determine the set of data we should use to determine facets
    HashMap<String, HashMap<String, FacetResult>> possibleFacets = this.generateFacetSearches(hits,
            numberOfHitsToProcessForFacets, numberOfResultsToCollectForFacets, searcher);

    FacetSearchHelper helper = processPossibleFacets(possibleFacets, reader, mainQueryBits, facetResults, hits,
            numberOfIdsToCollect, idsToCollectStartPosition, numberOfFactsToShow, mainQueryString, searcher);

    helper.setExecutedQuery(executedQuery);
    helper.setFacetTrail(filters);

    searcher.close();
    reader.close();
    return helper;
}

From source file:edu.ur.ir.user.service.DefaultUserGroupSearchService.java

License:Apache License

/**
 * Returns search results for the user groups.
 * // w  ww.ja v  a2 s .c o m
 * @param userGroupIndexFolder - location where the index folder is location 
 * @param query - query to execute
 * @param offset - offset to start at
 * @param numResults - number of results.
 * 
 * @return - set of users groups found for the query.
 */
public SearchResults<IrUserGroup> search(File userGroupIndexFolder, String query, int offset, int numResults) {
    SearchResults<IrUserGroup> searchResults = new SearchResults<IrUserGroup>();
    searchResults.setOriginalQuery(query);
    query = SearchHelper.prepareMainSearchString(query, true);
    ArrayList<IrUserGroup> userGroups = new ArrayList<IrUserGroup>();
    if (log.isDebugEnabled()) {
        log.debug("User search results executing query " + query + " on index "
                + userGroupIndexFolder.getAbsolutePath());
    }

    IndexSearcher searcher = null;
    IndexReader reader = null;
    try {
        FSDirectory directory = FSDirectory.open(userGroupIndexFolder);
        reader = IndexReader.open(directory, true);
        searcher = new IndexSearcher(reader);

        QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);

        Query luceneQuery = parser.parse(query);
        TopDocs hits = searcher.search(luceneQuery, 1000);

        searchResults.setTotalHits(hits.totalHits);

        int position = offset;
        int addedResults = 0;
        while (hits.totalHits > position && (addedResults < numResults)) {
            if (log.isDebugEnabled()) {
                log.debug(" adding document at position " + position);

            }

            Document d = searcher.doc(hits.scoreDocs[position].doc);

            Long userGroupId = NumericUtils.prefixCodedToLong(d.get(DefaultUserGroupIndexService.ID));
            ;
            if (log.isDebugEnabled()) {
                log.debug("user group id = " + userGroupId);
            }

            IrUserGroup userGroup = userGroupService.get(userGroupId, false);
            userGroups.add(userGroup);
            addedResults += 1;
            position += 1;
        }
    } catch (Exception e) {
        log.error(e);
    } finally {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
                log.error("the searcher could not be closed", e);
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                log.error("the reader could not be closed", e);
            }
        }
    }
    searchResults.setObjects(userGroups);
    return searchResults;
}

From source file:edu.ur.ir.user.service.DefaultUserSearchService.java

License:Apache License

public SearchResults<IrUser> search(File userIndexFolder, String query, int offset, int numResults) {
    SearchResults<IrUser> searchResults = new SearchResults<IrUser>();
    searchResults.setOriginalQuery(query);
    query = SearchHelper.prepareMainSearchString(query, true);
    ArrayList<IrUser> users = new ArrayList<IrUser>();
    if (log.isDebugEnabled()) {
        log.debug("User search results executing query " + query + " on index "
                + userIndexFolder.getAbsolutePath());
    }//w  w  w  . j ava 2s  .  c om

    String indexFolder = userIndexFolder.getAbsolutePath();
    IndexSearcher searcher = null;
    IndexReader reader = null;
    try {
        FSDirectory directory = FSDirectory.open(new File(indexFolder));
        reader = IndexReader.open(directory, true);
        searcher = new IndexSearcher(reader);
        QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);

        Query luceneQuery = parser.parse(query);
        TopDocs hits = searcher.search(luceneQuery, 1000);
        searchResults.setTotalHits(hits.totalHits);

        int position = offset;
        int addedResults = 0;
        while (hits.totalHits > position && (addedResults < numResults)) {
            if (log.isDebugEnabled()) {
                log.debug(" adding document at position " + position);

            }

            Document d = searcher.doc(hits.scoreDocs[position].doc);
            Long userId = NumericUtils.prefixCodedToLong(d.get(DefaultUserIndexService.USER_ID));
            log.debug("user id = " + userId);
            IrUser user = userService.getUser(userId, false);
            users.add(user);
            addedResults += 1;
            position += 1;
        }
    } catch (Exception e) {
        log.error(e);
    } finally {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
                log.error("the searcher could not be closed", e);
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                log.error("the reader could not be closed", e);
            }
        }
    }
    searchResults.setObjects(users);
    return searchResults;
}

From source file:edu.ur.ir.user.service.DefaultUserWorkspaceSearchService.java

License:Apache License

/**
 *  Execute the search/*  w  w w .j  a  v a  2 s  .  co m*/
 * @see edu.ur.ir.user.UserWorkspaceSearchService#search(java.io.File, java.lang.String, int, int)
 */
public SearchResults<FileSystem> search(File personalIndexFolder, String query, int offset, int numResults) {

    SearchResults<FileSystem> searchResults = new SearchResults<FileSystem>();
    searchResults.setOriginalQuery(query);

    query = SearchHelper.prepareMainSearchString(query, true);
    ArrayList<FileSystem> fileSystemObjects = new ArrayList<FileSystem>();
    if (log.isDebugEnabled()) {
        log.debug("User search results executing query " + query + " on index "
                + personalIndexFolder.getAbsolutePath());
    }

    String indexFolder = personalIndexFolder.getAbsolutePath();
    IndexSearcher searcher = null;
    IndexReader reader = null;
    try {
        FSDirectory directory = FSDirectory.open(new File(indexFolder));
        reader = IndexReader.open(directory, true);
        searcher = new IndexSearcher(reader);
        QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);

        Query luceneQuery = parser.parse(query);
        TopDocs hits = searcher.search(luceneQuery, 1000);
        searchResults.setTotalHits(hits.totalHits);

        int position = offset;
        int addedResults = 0;
        while (hits.totalHits > position && (addedResults <= numResults)) {
            if (log.isDebugEnabled()) {
                log.debug(" adding document at position " + position);
            }
            Document d = searcher.doc(hits.scoreDocs[position].doc);
            fileSystemObjects.add(getFileSystemObject(d));
            addedResults += 1;
            position += 1;
        }
    } catch (Exception e) {
        log.error(e);
    } finally {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
                log.error("the searcher could not be closed", e);
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                log.error("the reader could not be closed", e);
            }
        }
    }
    searchResults.setObjects(fileSystemObjects);

    return searchResults;
}

From source file:edu.usc.ir.geo.gazetteer.GeoNameResolver.java

License:Apache License

/**
 * Search corresponding GeoName for each location entity
 * @param count/*  www.  j av a  2  s . c o m*/
 *            Number of results for one locations
 * @param querystr
 *            it's the NER actually
 *
 * @return HashMap each name has a list of resolved entities
 * @throws IOException
 * @throws RuntimeException
 */

public HashMap<String, List<Location>> searchGeoName(String indexerPath, List<String> locationNameEntities,
        int count) throws IOException {

    if (locationNameEntities.size() == 0 || locationNameEntities.get(0).length() == 0)
        return new HashMap<String, List<Location>>();
    IndexReader reader = createIndexReader(indexerPath);
    HashMap<String, List<Location>> resolvedEntities = resolveEntities(locationNameEntities, count, reader);
    reader.close();
    return resolvedEntities;

}

From source file:edu.wayne.cs.severe.ir4se.lucene.SearchFiles.java

License:Apache License

@SuppressWarnings("deprecation")
public static void search(String system, int documentType, int queryNumber, String indexDirectoryPath,
        String queryString, String fileOutput, String[] targetClasses, boolean runIndividualTerms,
        boolean append) throws Exception {

    String index = indexDirectoryPath;
    FileWriter f = new FileWriter(index + "../NotFound.txt", true);

    for (int i = 0; i < targetClasses.length; i++) {
        String target = targetClasses[i];
        boolean found = Indexer.isFileDocInIndex(indexDirectoryPath, target);
        if (!found)
            f.append("Target doc " + i + " - " + target + " not found in index!\n");
    }/*from w  w  w .  j a  v a  2 s.c o  m*/
    f.close();
    IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)), true);

    int numDocs = reader.numDocs();
    System.out.println("The number of documents in the index is: " + numDocs);

    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);

    String[] fields;
    fields = new String[1];
    fields[0] = "contents";

    if (!runIndividualTerms) {
        MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
        int hitsPerPage = numDocs;
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        Query query = parser.parse(queryString);
        searcher.search(query, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        System.out.println("The number of hits is: " + hits.length);

        // file with the results (score and position) only for the relevant
        // documents
        // the file contains entries in the following format:
        // (queryNumber,relDoc1,posRelDoc1,scoreRelDoc1,relDoc2,posRelDoc2,scoreRelDoc2,...)
        FileWriter fwRelevant = new FileWriter(fileOutput, append);

        String path = "";
        String docName = "";
        String docPathAndName = "";
        for (String target : targetClasses) {
            boolean found = false;
            for (int i = 0; i < hits.length; i++) {
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);
                path = d.get("path");

                float score = hits[i].score;

                if (documentType == 2) {
                    docName = d.get("docName");

                    docPathAndName = path.toLowerCase() + "." + docName.toLowerCase();

                    if (target.equalsIgnoreCase(docPathAndName)) {
                        fwRelevant.write(system + ";" + queryNumber + ";" + target + ";" + (i + 1) + ";"
                                + hits.length + ";" + numDocs + ";" + score + "\n");
                        found = true;
                        break;
                    }
                } else if (documentType == 1) {
                    File pathDir = new File(path.trim());
                    String fileName = pathDir.getName();
                    docName = fileName.replaceAll(".txt", "");
                    fwRelevant.write((i + 1) + ". doc = " + docName + " score = " + score + "\n");
                }
            }
            if (found == false)
                fwRelevant.write(system + ";" + queryNumber + ";" + target + "; NOT_RETRIEVED" + "\n");

        }
        // fw.close();
        fwRelevant.close();
        reader.close();
    } else // runIndividualTerms = true
    {
        /**
         * each query will be divided in its constituent terms and each term
         * will be run as a separate query
         **/
        /**
         * this is useful to determine the similarity of each of the terms
         * in a query to a target document so that we determine which terms
         * in the query tend to lead to the best results, i.e., to finding
         * the targets sooner
         **/

        SearchFiles.search(system, documentType, queryNumber, indexDirectoryPath, queryString,
                fileOutput.replaceAll(".txt", "_wholeQuery.txt"), targetClasses, false, append);

        FileWriter fw = new FileWriter(fileOutput.replaceAll(".txt", "_terms.txt"));
        fw.write(
                "\n\n\n------------------------------------------------------------------------------------\n\n");
        fw.write("                               Results for query " + queryNumber + "\n");
        fw.write("------------------------------------------------------------------------------------\n\n");

        // file with the results (score and position) only for the relevant
        // documents
        // the file contains entries in the following format:
        // (queryNumber,term1,term1TF,term1DF,relDoc1,posRelDoc1Term1,scoreRelDoc1Term1,relDoc2,posRelDoc2Term1,scoreRelDoc2Term1,...)
        // (queryNumber,term2,term2TF,term2DF,relDoc1,posRelDoc1Term2,scoreRelDoc1Term2,relDoc2,posRelDoc2Term2,scoreRelDoc2Term2,...)
        // ...
        FileWriter fwRelevant = new FileWriter(
                fileOutput.replaceAll(".txt", "_terms_RelevantDocsPositions.txt"));

        String[] queryTerms = queryString.split(" ");
        for (int l = 0; l < queryTerms.length; l++) {
            MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
            int hitsPerPage = numDocs;
            TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);

            String q = queryTerms[l];
            Query query = parser.parse(q);
            searcher.search(query, collector);
            ScoreDoc[] hits = collector.topDocs().scoreDocs;
            fw.write("TERM " + (l + 1) + ": " + q + "\n\n");
            fwRelevant.write("\n" + queryNumber + "," + q);
            for (int i = 0; i < hits.length; i++) {
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);
                String path = d.get("path");
                float score = hits[i].score;
                if (documentType == 2) {
                    String docName = d.get("docName");
                    fw.write((i + 1) + ". doc = " + path + " " + docName + " - score = " + score + "\n");
                    for (int k = 0; k < targetClasses.length; k++) {
                        if (docName.equalsIgnoreCase(targetClasses[k])) {
                            String contents = d.get("contents");
                            int frequency = countOccurrences(contents, q);// tf
                            fwRelevant.write("," + frequency);

                            fwRelevant.write("," + reader.docFreq(new Term("contents", q)));// df
                            fwRelevant.write("," + path + "." + docName + "," + (i + 1) + "," + score);
                            break;
                        }
                    }
                } else if (documentType == 1) {
                    File pathDir = new File(path);
                    String fileName = pathDir.getName();
                    String docName = fileName.replaceAll(".txt", "");
                    fw.write((i + 1) + ". doc = " + docName + " score = " + score + "\n");
                }
            }
            fw.write("\n\n\n");
        }
        fw.close();
        f.close();
        fwRelevant.close();
        reader.close();
    }
}

From source file:engine.easy.search.RelevanceFeedBackUtil.java

License:Apache License

/**
 * This method will perform the thumbs up action. And generate the new query
 * based on top specific highest terms. It also increase the relevant
 * document boost so that their ranking is higher in search results for the
 * similar terms./*  ww w.j a va 2s . c  o  m*/
 */
public static Query performThumbsUp(List<Integer> luceneDocIds) throws IOException {

    Query q = null;

    try {
        final Map<String, Integer> frequencyMap = new HashMap<String, Integer>();
        Map<Integer, Document> documentMap = new HashMap<Integer, Document>();
        List<String> termsList = new ArrayList<String>();

        Directory indexDir = FSDirectory.open(new File(AppConstants.INDEX_DIR_PATH));
        IndexReader indexReader = IndexReader.open(indexDir);
        EasySearchIndexReader esiReader = new EasySearchIndexReader(indexReader);

        for (Integer docId : luceneDocIds) {

            TermFreqVector tfv = indexReader.getTermFreqVector(docId, "CONTENT");
            Document doc = indexReader.document(docId);
            float boost = doc.getBoost() + AppConstants.THUMBS_UP;
            doc.setBoost(boost);

            System.out.print("DOC : " + docId + " Field : " + tfv.getField() + "\n");

            for (int i = 0; i < tfv.getTermFrequencies().length; i++) {
                if (!termsList.contains(tfv.getTerms()[i]))
                    termsList.add(tfv.getTerms()[i]);

                System.out.println("TERM : " + tfv.getTerms()[i] + " FREQ : " + tfv.getTermFrequencies()[i]);
                frequencyMap.put(tfv.getTerms()[i], tfv.getTermFrequencies()[i]);
            }

            //put the document with doc id.
            documentMap.put(docId, doc);
        }

        //close the index reader;
        indexReader.close();

        //Boost the terms visibility in documents, so these documents more frequently for specific search terms.
        q = computeTopTermQuery(termsList, frequencyMap, AppConstants.TOP_DOCUMENTS);
        q.setBoost(2.0F);

        //Update the documents with their boost.
        //EasySearchIndexBuilder.updateDocuments(documentMap);

    } catch (Exception e) {
        System.out.println("Exception: performThumbsUp" + e.toString());
    }

    return q;
}

From source file:engine.easy.search.RelevanceFeedBackUtil.java

License:Apache License

/**
 * This method will perform the thumbs down action. And generate the new
 * query based on top specific highest terms. It also decrease the relevant
 * document boost so that their ranking is lower in search results for the
 * similar terms.//  w  w w  .  j  a  va2s  . c  o  m
 */
public static Query performThumbsDown(List<Integer> luceneDocIds) {

    Query q = null;

    try {
        final Map<String, Integer> frequencyMap = new HashMap<String, Integer>();
        Map<Integer, Document> documentMap = new HashMap<Integer, Document>();
        List<String> termsList = new ArrayList<String>();

        Directory indexDir = FSDirectory.open(new File(AppConstants.INDEX_DIR_PATH));
        IndexReader indexReader = IndexReader.open(indexDir);
        EasySearchIndexReader esiReader = new EasySearchIndexReader(indexReader);

        for (Integer docId : luceneDocIds) {

            TermFreqVector tfv = indexReader.getTermFreqVector(docId, "CONTENT");
            Document doc = indexReader.document(docId);
            float boost = doc.getBoost() + AppConstants.THUMBS_UP;
            doc.setBoost(boost);

            System.out.print("DOC : " + docId + " Field : " + tfv.getField() + "\n");

            for (int i = 0; i < tfv.getTermFrequencies().length; i++) {
                if (!termsList.contains(tfv.getTerms()[i]))
                    termsList.add(tfv.getTerms()[i]);

                System.out.println("TERM : " + tfv.getTerms()[i] + " FREQ : " + tfv.getTermFrequencies()[i]);
                frequencyMap.put(tfv.getTerms()[i], tfv.getTermFrequencies()[i]);
            }

            //put the document with doc id.
            documentMap.put(docId, doc);
        }

        //close the index reader;
        indexReader.close();

        //Boost the terms visibility in documents, so these documents more frequently for specific search terms.
        q = computeTopTermQuery(termsList, frequencyMap, AppConstants.TOP_DOCUMENTS);
        q.setBoost(-2.0F);

        //Update the documents with their boost.
        //EasySearchIndexBuilder.updateDocuments(documentMap);

    } catch (Exception e) {
        System.out.println("Exception: performThumbsUp" + e.toString());
    }

    return q;
}