-
Notifications
You must be signed in to change notification settings - Fork 21
/
pubLoadSolr
executable file
·65 lines (51 loc) · 1.94 KB
/
pubLoadSolr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# a command line tool to load a pubtools database into solr
# load default python packages
from __future__ import print_function
import logging, optparse, os, shutil, glob, tempfile, sys
from os.path import *
# add <scriptDir>/lib/ to package search path
sys.path.insert(0, join(dirname(abspath(__file__)),"lib"))
# load our own libraries
import pubConf, pubGeneric, pubStore, maxCommon
#import scorched
from mysolr import Solr
# ===== FUNCTIONS =======
def loadIntoSolr(inDirs):
" load data from inDirs into solr "
solrUrl = pubConf.solrUrl
solr = Solr(solrUrl, version=4)
i = 0
docs = []
for inDir in inDirs:
for article, fileList in pubStore.iterArticleDirList(inDir):
i+=1
articleInfo = article._asdict()
for fileData in fileList:
doc = dict(fileData._asdict())
doc.update(articleInfo)
newDoc = {}
for key, val in doc.iteritems():
if key=="fileId":
newDoc["id"] = val
else:
newDoc[key+"_t"]=val
docs.append(newDoc)
if len(docs)>10000:
print("COMMIT", i)
print(solr.update(docs))
docs = []
def main(args, options):
pubGeneric.setupLoggingOptions(options)
inDirs = pubGeneric.resolveDatasetDesc(args[0])
loadIntoSolr(inDirs)
# === COMMAND LINE INTERFACE, OPTIONS AND HELP ===
parser = optparse.OptionParser("""usage: %prog [options] <inDirOrFiles> - load articles meta info and files into a solr server configured in pubConf.
""")
parser.add_option("-d", "--debug", dest="debug", action="store_true", help="show debug messages")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="show more debug messages")
(options, args) = parser.parse_args()
if args==[]:
parser.print_help()
exit(1)
main(args, options)