Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
e5cf2882
Commit
e5cf2882
authored
Aug 06, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix a lucene bug
parent
9ef6de01
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
7 deletions
+67
-7
pom.xml
webmagic-lucene/pom.xml
+1
-1
LucenePipeline.java
...n/java/us/codecraft/webmagic/pipeline/LucenePipeline.java
+5
-6
OschinaBlog.java
...n/test/java/us/codecraft/webmagic/lucene/OschinaBlog.java
+61
-0
No files found.
webmagic-lucene/pom.xml
View file @
e5cf2882
...
...
@@ -24,7 +24,7 @@
</dependency>
<dependency>
<groupId>
us.codecraft
</groupId>
<artifactId>
webmagic-
core
</artifactId>
<artifactId>
webmagic-
extension
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
...
...
webmagic-lucene/src/main/java/us/codecraft/webmagic/pipeline/LucenePipeline.java
View file @
e5cf2882
...
...
@@ -33,16 +33,14 @@ public class LucenePipeline implements Pipeline {
private
Directory
directory
;
private
IndexWriter
indexWriter
;
private
Analyzer
analyzer
;
private
IndexWriterConfig
config
;
private
void
init
()
throws
IOException
{
analyzer
=
new
StandardAnalyzer
(
Version
.
LUCENE_44
);
directory
=
new
RAMDirectory
();
IndexWriterConfig
config
=
new
IndexWriterConfig
(
Version
.
LUCENE_44
,
analyzer
);
indexWriter
=
new
IndexWriter
(
directory
,
config
);
indexWriter
.
close
();
config
=
new
IndexWriterConfig
(
Version
.
LUCENE_44
,
analyzer
);
}
public
LucenePipeline
()
{
...
...
@@ -67,7 +65,6 @@ public class LucenePipeline implements Pipeline {
documents
.
add
(
hitDoc
);
}
ireader
.
close
();
directory
.
close
();
return
documents
;
}
...
...
@@ -85,7 +82,9 @@ public class LucenePipeline implements Pipeline {
doc
.
add
(
new
Field
(
objectEntry
.
getKey
(),
objectEntry
.
getValue
().
toString
(),
TextField
.
TYPE_STORED
));
}
try
{
IndexWriter
indexWriter
=
new
IndexWriter
(
directory
,
config
);
indexWriter
.
addDocument
(
doc
);
indexWriter
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
...
...
webmagic-lucene/src/main/test/java/us/codecraft/webmagic/lucene/OschinaBlog.java
0 → 100644
View file @
e5cf2882
package
us
.
codecraft
.
webmagic
.
lucene
;
import
org.apache.lucene.document.Document
;
import
org.apache.lucene.queryparser.classic.ParseException
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.model.ExtractBy
;
import
us.codecraft.webmagic.model.OOSpider
;
import
us.codecraft.webmagic.model.TargetUrl
;
import
us.codecraft.webmagic.pipeline.LucenePipeline
;
import
java.io.IOException
;
import
java.util.List
;
/**
* @author code4crafter@gmail.com <br>
* @date: 13-8-2 <br>
* Time: 上午7:52 <br>
*/
@TargetUrl
(
"http://my.oschina.net/flashsword/blog/\\d+"
)
public
class
OschinaBlog
{
@ExtractBy
(
"//title"
)
private
String
title
;
@ExtractBy
(
value
=
"div.BlogContent"
,
type
=
ExtractBy
.
Type
.
Css
)
private
String
content
;
@Override
public
String
toString
()
{
return
"OschinaBlog{"
+
"title='"
+
title
+
'\''
+
", content='"
+
content
+
'\''
+
'}'
;
}
public
static
void
main
(
String
[]
args
)
{
LucenePipeline
pipeline
=
new
LucenePipeline
();
OOSpider
.
create
(
Site
.
me
().
addStartUrl
(
"http://my.oschina.net/flashsword/blog"
),
OschinaBlog
.
class
).
pipeline
(
pipeline
).
runAsync
();
while
(
true
)
{
try
{
List
<
Document
>
search
=
pipeline
.
search
(
"title"
,
"webmagic"
);
System
.
out
.
println
(
search
);
Thread
.
sleep
(
3000
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
public
String
getTitle
()
{
return
title
;
}
public
String
getContent
()
{
return
content
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment