add two basic solr index service

This commit is contained in:
pelick
2013-03-10 14:32:06 +08:00
parent 5c767dd6d5
commit 9948c68d0d
5 changed files with 269 additions and 1 deletions
@@ -0,0 +1,81 @@
package zbf.search.academic.publication;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import zbf.search.academic.publication.PublicationIndex;
import zbf.search.academic.publication.PublicationModel;
import zbf.search.util.StdOutUtil;
import com.mongodb.BasicDBList;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class PublicationIndex {
private String indexPath;
private IKAnalyzer analyzer = new IKAnalyzer(true);
public PublicationIndex(String indexPath) {
this.indexPath = indexPath;
}
public void build() throws IOException {
FSDirectory directory = null;
IndexWriterConfig conf = null;
IndexWriter writer = null;
directory = FSDirectory.open(new File(indexPath));
conf = new IndexWriterConfig(Version.LUCENE_36, analyzer);
conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
writer = new IndexWriter(directory, conf);
// data
MongoClient mongoClient = new MongoClient("localhost", 30000);
DB db = mongoClient.getDB("academic");
DBCollection coll = db.getCollection("publications");
DBCursor cursor = coll.find();
try {
while (cursor.hasNext()) {
PublicationModel model = new PublicationModel();
DBObject obj = cursor.next();
model.setTitle((String) obj.get("title"));
model.setPub_abstract((String) obj.get("abstract"));
model.setConference((String) obj.get("conference"));
model.setView_url((String) obj.get("view_url"));
BasicDBList list = (BasicDBList) obj.get("author");
model.setAuthor(list.toString());
Document doc = new Document();
doc.add(new Field("title", model.getTitle(), Store.YES, Index.ANALYZED));
doc.add(new Field("pub_abstract", model.getPub_abstract(), Store.YES, Index.ANALYZED));
doc.add(new Field("conference", model.getConference(), Store.YES, Index.ANALYZED));
doc.add(new Field("view_url", model.getView_url(), Store.YES, Index.ANALYZED));
doc.add(new Field("author", model.getAuthor(), Store.YES, Index.ANALYZED));
writer.addDocument(doc);
StdOutUtil.out(model.getTitle());
}
} finally {
cursor.close();
}
writer.close();
}
public static void main(String[] args) throws IOException {
PublicationIndex pi = new PublicationIndex("E://softs2/apache-solr-3.6.2/example/multicore/core1/data/index");
pi.build();
}
}
@@ -0,0 +1,44 @@
package zbf.search.academic.publication;
public class PublicationModel {
private String title = "";
private String author = "";
private String pub_abstract = "";
private String conference = "";
private String view_url = "";
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPub_abstract() {
return pub_abstract;
}
public void setPub_abstract(String pub_abstract) {
if (pub_abstract != null)
this.pub_abstract = pub_abstract;
}
public String getConference() {
return conference;
}
public void setConference(String conference) {
if (conference != null)
this.conference = conference;
}
public String getView_url() {
return view_url;
}
public void setView_url(String view_url) {
if (view_url != null)
this.view_url = view_url;
}
}
@@ -0,0 +1,80 @@
package zbf.search.academic.researcher;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import zbf.search.util.StdOutUtil;
import com.mongodb.BasicDBList;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class ResearcherIndex {
private String indexPath;
private IKAnalyzer analyzer = new IKAnalyzer(true);
public ResearcherIndex(String indexPath) {
this.indexPath = indexPath;
}
public void build() throws IOException {
FSDirectory directory = null;
IndexWriterConfig conf = null;
IndexWriter writer = null;
directory = FSDirectory.open(new File(indexPath));
conf = new IndexWriterConfig(Version.LUCENE_36, analyzer);
conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
writer = new IndexWriter(directory, conf);
// data
MongoClient mongoClient = new MongoClient("localhost", 30000);
DB db = mongoClient.getDB("academic");
DBCollection coll = db.getCollection("researchers");
DBCursor cursor = coll.find();
try {
while (cursor.hasNext()) {
ResearcherModel model = new ResearcherModel();
DBObject obj = cursor.next();
model.setName((String) obj.get("name"));
model.setWorkplace((String) obj.get("workplace"));
model.setHomepage((String) obj.get("homepage"));
BasicDBList list = (BasicDBList) obj.get("field");
model.setField(list.toString());
Document doc = new Document();
doc.add(new Field("name", model.getName(), Store.YES, Index.ANALYZED));
doc.add(new Field("workplace", model.getWorkplace(), Store.YES, Index.ANALYZED));
doc.add(new Field("homepage", model.getHomepage(), Store.YES, Index.ANALYZED));
doc.add(new Field("field", model.getField(), Store.YES, Index.ANALYZED));
writer.addDocument(doc);
// StdOutUtil.out(model.getHomepage());
StdOutUtil.out(model.getName());
// StdOutUtil.out(model.getWorkplace());
// StdOutUtil.out(model.getField().toString());
}
} finally {
cursor.close();
}
writer.close();
}
public static void main(String[] args) throws IOException {
ResearcherIndex ri = new ResearcherIndex(
"E://softs2/apache-solr-3.6.2/example/multicore/core0/data/index");
ri.build();
}
}
@@ -0,0 +1,64 @@
package zbf.search.academic.researcher;
public class ResearcherModel {
private String name = "";
private String microul = "";
private String workplace = "";
private String field = "";
private String homepage = "";
private String keywords_url = "";
private String publications_url = "";
private String citations_url = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMicroul() {
return microul;
}
public void setMicroul(String microul) {
this.microul = microul;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
if (workplace != null)
this.workplace = workplace;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getHomepage() {
return homepage;
}
public void setHomepage(String homepage) {
if (homepage != null)
this.homepage = homepage;
}
public String getKeywords_url() {
return keywords_url;
}
public void setKeywords_url(String keywords_url) {
this.keywords_url = keywords_url;
}
public String getPublications_url() {
return publications_url;
}
public void setPublications_url(String publications_url) {
this.publications_url = publications_url;
}
public String getCitations_url() {
return citations_url;
}
public void setCitations_url(String citations_url) {
this.citations_url = citations_url;
}
}
@@ -1,6 +1,5 @@
package zbf.search.mongodb;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;