Skip to content

Instantly share code, notes, and snippets.

@Vadi
Created May 10, 2014 12:43
Show Gist options
  • Select an option

  • Save Vadi/31b298ce68e4e6b122df to your computer and use it in GitHub Desktop.

Select an option

Save Vadi/31b298ce68e4e6b122df to your computer and use it in GitHub Desktop.
Shows how to implement grouping queries in Lucene
package com.exzeo.spock;
import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.search.grouping.GroupDocs;
import org.apache.lucene.search.grouping.GroupingSearch;
import org.apache.lucene.search.grouping.TopGroups;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
/**
* Shows how Lucene used for implementing grouping options
*
* @author Vadi (vadi@exzeo.com)
* @since 02/03/14
*/
public class GroupingApproach extends TestCase {
File path = new File("target/grouping");
Directory directory;
Directory directoryNew;
IndexWriter writer;
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new SimpleAnalyzer(Version.LUCENE_46));
@Override
public void setUp() throws IOException {
if (path.exists()) {
FileUtils.deleteDirectory(path);
}
directory = new SimpleFSDirectory(path);
directoryNew = new SimpleFSDirectory(new File("target/grouping/new"));
writer = new IndexWriter(directory, config);
}
@Override
public void tearDown() throws IOException {
if (writer != null)
writer.close();
if (directory != null)
directory.close();
FileUtils.deleteDirectory(path);
}
public void testGrouping() throws IOException {
addDocument(writer, "w1", "s1", "ram", 101);
addDocument(writer, "w2", "s1", "ram", 102);
addDocument(writer, "w3", "s2", "ram", 103);
addDocument(writer, "w1", "s1", "sita", 104);
writer.commit();
writer.close();
IndexReader reader = DirectoryReader.open(directoryNew);
IndexSearcher searcher = new IndexSearcher(reader);
final TermQuery termQuery = new TermQuery(new Term("s", "s1"));
GroupingSearch search = new GroupingSearch("activity.owner");
search.setGroupSort(Sort.RELEVANCE);
search.setGroupDocsLimit(1);
search.setAllGroups(true);
search.setFillSortFields(true);
final TopGroups<BytesRef> search1 = search.search(searcher, null, new MatchAllDocsQuery(), 0, 5);
printDocs(search1.groups, reader);
}
void printDocs(GroupDocs<BytesRef>[] search1, IndexReader reader) throws IOException {
for (GroupDocs<BytesRef> group : search1) {
System.out.println(group.groupValue.utf8ToString() + " " + group.totalHits);
for (ScoreDoc scoreDoc : group.scoreDocs) {
final Document document = reader.document(scoreDoc.doc);
printDoc(document);
}
for (Object groupSortValue : group.groupSortValues) {
System.out.println(groupSortValue);
}
}
}
void printDoc(Document document) {
for (IndexableField indexableField : document) {
System.out.print(indexableField.name() + " : " + indexableField.stringValue() + ",");
}
System.out.println();
}
public void addDocument(IndexWriter writer, String workkard, String status, String assignee, int id) throws IOException {
Document document = new Document();
document.add(new StringField("w", workkard, Field.Store.YES));
document.add(new StringField("s", status, Field.Store.YES));
document.add(new StringField("a", assignee, Field.Store.YES));
document.add(new IntField("id", id, Field.Store.YES));
document.add(new SortedDocValuesField("a_dv", new BytesRef(assignee)));
writer.addDocument(document);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment