Skip to content

Instantly share code, notes, and snippets.

Phrase Search in Documents using Java. For more information, please follow link: https://kb.groupdocs.com/search/java/phrase-search-in-documents-using-java/
import com.groupdocs.search.Index;
import com.groupdocs.search.SearchQuery;
import com.groupdocs.search.licenses.License;
import com.groupdocs.search.results.SearchResult;
public class PhraseSearchinDocumentsusingJava {
public static void main(String[] args) throws Exception {
// Apply the license to remove the restrictions
// imposed by the Search library
License license = new License();
license.setLicense("GroupDocs.Search.lic");
// The path where the index will be stored
String indexFolder = "c:\\MyIndex\\";
// The folder containing the documents you want to search
String documentsFolder = "c:\\MyDocuments\\";
// Creating an index in the specified folder
Index index = new Index(indexFolder);
// Indexing documents from the specified folder
index.add(documentsFolder);
// Search for the phrase 'theory of relativity' in text form
String query1 = "\"theory of relativity\"";
SearchResult result1 = index.search(query1);
// Search for the phrase 'theory of relativity' in object form
SearchQuery word1 = SearchQuery.createWordQuery("theory");
SearchQuery word2 = SearchQuery.createWordQuery("of");
SearchQuery word3 = SearchQuery.createWordQuery("relativity");
SearchQuery query2 = SearchQuery.createPhraseSearchQuery(word1, word2, word3);
SearchResult result2 = index.search(query2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment