Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit fb89840

Browse files
author
Hüseyin Mert
authored
Merge pull request #25 from academic/feature/pdf-parser
Feature/pdf parser
2 parents 76c75ee + d9a360c commit fb89840

6 files changed

Lines changed: 176 additions & 43 deletions

File tree

src/main/java/io/academic/entity/Article.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
@Entity
1212
public class Article extends AbstractAuditingEntity {
1313

14+
public Article(){
15+
16+
}
17+
1418
@Column
1519
@Type(type = "text")
1620
private String title;
@@ -43,6 +47,18 @@ public class Article extends AbstractAuditingEntity {
4347
@Type(type = "text")
4448
private String type;
4549

50+
@Column
51+
@Type(type = "text")
52+
private String base64;
53+
54+
@Column
55+
@Type(type = "text")
56+
private String articleIdentifier;
57+
58+
@Column
59+
@Type(type = "text")
60+
private String relation;
61+
4662
public String getTitle() {
4763
return title;
4864
}
@@ -106,4 +122,28 @@ public String getType() {
106122
public void setType(String type) {
107123
this.type = type;
108124
}
125+
126+
public String getBase64() {
127+
return base64;
128+
}
129+
130+
public void setBase64(String base64) {
131+
this.base64 = base64;
132+
}
133+
134+
public String getArticleIdentifier() {
135+
return articleIdentifier;
136+
}
137+
138+
public void setArticleIdentifier(String articleIdentifier) {
139+
this.articleIdentifier = articleIdentifier;
140+
}
141+
142+
public String getRelation() {
143+
return relation;
144+
}
145+
146+
public void setRelation(String relation) {
147+
this.relation = relation;
148+
}
109149
}

src/main/java/io/academic/entity/OaiDataProvider.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ public OaiDataProvider(){
1616

1717
}
1818

19-
public OaiDataProvider(String name, String url, String identifier) {
19+
public OaiDataProvider(String name, String url,String downloadUrl, String identifier) {
2020
this.name = name;
2121
this.url = url;
22+
this.downloadUrl = downloadUrl;
2223
this.identifier = identifier;
2324
}
2425

26+
public OaiDataProvider(String url,String downloadUrl) {
27+
this.url = url;
28+
this.downloadUrl = downloadUrl;
29+
}
30+
2531
public OaiDataProvider(String url) {
2632
this.url = url;
2733
}
@@ -32,6 +38,9 @@ public OaiDataProvider(String url) {
3238
@Column
3339
private String url;
3440

41+
@Column
42+
private String downloadUrl;
43+
3544
@Column
3645
private String identifier;
3746

@@ -69,5 +78,12 @@ public OaiDataProvider setIdentifier(String identifier) {
6978
return this;
7079
}
7180

81+
public String getDownloadUrl() {
82+
return downloadUrl;
83+
}
84+
85+
public void setDownloadUrl(String downloadUrl) {
86+
this.downloadUrl = downloadUrl;
87+
}
7288

7389
}

src/main/java/io/academic/service/AcademicSearchService.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.google.gson.JsonParser;
77
import org.elasticsearch.action.search.*;
88
import org.elasticsearch.common.unit.TimeValue;
9+
import org.elasticsearch.index.query.QueryBuilder;
910
import org.elasticsearch.index.query.QueryBuilders;
1011
import org.elasticsearch.search.Scroll;
1112
import org.elasticsearch.search.SearchHit;
@@ -16,6 +17,8 @@
1617
import org.springframework.stereotype.Service;
1718

1819
import java.io.IOException;
20+
import java.util.ArrayList;
21+
import java.util.List;
1922

2023
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
2124

@@ -30,8 +33,11 @@ public AcademicSearchService() {
3033

3134
public String search(String q) throws IOException {
3235

36+
ArrayList<String> criterias = new ArrayList<String>();
37+
criterias.add("dc");
38+
criterias.add("content");
3339
SearchRequest searchRequest = new SearchRequest("harvester");
34-
searchRequest.source(buildSource("term","dc",q,false));
40+
searchRequest.source(buildSource("term",criterias,q,false));
3541

3642
//this values are necessary if we need scrollable results (in other words if our result have more than 10 hits)
3743
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1));
@@ -47,8 +53,10 @@ public String search(String q) throws IOException {
4753

4854
public String searchBy(String q, String criteria) throws IOException {
4955

56+
ArrayList<String> criterias = new ArrayList<String>();
57+
criterias.add(criteria);
5058
SearchRequest searchRequest = new SearchRequest("harvester");
51-
searchRequest.source(buildSource("match",criteria,q,false));
59+
searchRequest.source(buildSource("match",criterias,q,false));
5260

5361
//this values are necessary if we need scrollable results (in other words if our result have more than 10 hits)
5462
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1));
@@ -66,8 +74,10 @@ public String searchBy(String q, String criteria) throws IOException {
6674

6775
public String getAll() throws IOException {
6876

77+
ArrayList<String> criterias = new ArrayList<String>();
78+
criterias.add("");
6979
SearchRequest searchRequest = new SearchRequest("harvester");
70-
searchRequest.source(buildSource("matchAll","","",true));
80+
searchRequest.source(buildSource("matchAll",criterias,"",true));
7181

7282
//this values are necessary if we need scrollable results (in other words if our result have more than 10 hits)
7383
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1));
@@ -128,16 +138,16 @@ public String toJson(String nonJsonString){
128138

129139

130140

131-
public SearchSourceBuilder buildSource(String queryType, String criteria, String q, Boolean showAllFields){
141+
public SearchSourceBuilder buildSource(String queryType, ArrayList<String> criteria, String q, Boolean showAllFields){
132142
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
133143

134144
if (queryType.equals("match"))
135145
{
136-
searchSourceBuilder.query(matchQuery(criteria,q));
146+
searchSourceBuilder.query(QueryBuilders.matchQuery(criteria.get(0),q));
137147
}
138148
else if (queryType.equals("term"))
139149
{
140-
searchSourceBuilder.query(QueryBuilders.termQuery(criteria,q));
150+
searchSourceBuilder.query(QueryBuilders.multiMatchQuery(q,criteria.toArray(new String[criteria.size()])));
141151
}
142152
else
143153
{
@@ -147,7 +157,8 @@ else if (queryType.equals("term"))
147157
searchSourceBuilder.sort(new FieldSortBuilder("title.keyword").order(SortOrder.DESC));
148158
if (!showAllFields)
149159
{
150-
String[] includeFields = new String[] {"title",criteria};
160+
criteria.add("title");
161+
String[] includeFields = criteria.toArray(new String[criteria.size()]);
151162
String[] excludeFields = new String[] {""};
152163
searchSourceBuilder.fetchSource(includeFields,excludeFields);
153164
searchSourceBuilder.fetchSource(true);

src/main/java/io/academic/service/OaiDataProviderService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public boolean submitUrl(String url) throws InterruptedException {
7777
public String addRule(String url)
7878
{
7979
// String rule = "?metadataPrefix=oai_dc&verb=ListRecords";
80-
String rule = "?from=2018-01-07&until=2018-01-08&metadataPrefix=oai_dc&verb=ListRecords";
80+
String rule = "?from=2017-01-01&until=2017-01-02&metadataPrefix=oai_dc&verb=ListRecords";
8181
return url+rule;
8282
}
8383

src/main/java/io/academic/service/OaiService.java

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@
99
import com.google.gson.GsonBuilder;
1010
import com.google.gson.JsonElement;
1111
import com.google.gson.JsonParser;
12-
import eu.luminis.elastic.document.DocumentService;
13-
import eu.luminis.elastic.document.IndexRequest;
14-
import eu.luminis.elastic.document.UpdateRequest;
15-
import eu.luminis.elastic.index.IndexService;
16-
import eu.luminis.elastic.search.SearchService;
1712
import io.academic.dao.DcDao;
1813
import io.academic.entity.*;
14+
import org.apache.commons.io.IOUtils;
1915
import org.apache.http.HttpHost;
16+
import org.elasticsearch.ElasticsearchException;
17+
import org.elasticsearch.action.DocWriteResponse;
2018
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
19+
import org.elasticsearch.action.index.IndexRequest;
20+
import org.elasticsearch.action.index.IndexResponse;
2121
import org.elasticsearch.action.search.*;
22+
import org.elasticsearch.action.support.replication.ReplicationResponse;
2223
import org.elasticsearch.client.RestClient;
2324
import org.elasticsearch.client.RestHighLevelClient;
2425
import org.elasticsearch.common.unit.TimeValue;
26+
import org.elasticsearch.common.xcontent.XContentType;
2527
import org.elasticsearch.index.query.QueryBuilders;
28+
import org.elasticsearch.rest.RestStatus;
2629
import org.elasticsearch.search.Scroll;
2730
import org.elasticsearch.search.SearchHit;
2831
import org.elasticsearch.search.builder.SearchSourceBuilder;
@@ -39,7 +42,13 @@
3942
import org.springframework.stereotype.Service;
4043

4144
import javax.transaction.Transactional;
45+
import java.io.BufferedInputStream;
46+
import java.io.BufferedReader;
4247
import java.io.IOException;
48+
import java.io.InputStreamReader;
49+
import java.net.MalformedURLException;
50+
import java.net.URL;
51+
import java.net.URLConnection;
4352
import java.time.LocalDate;
4453
import java.time.LocalDateTime;
4554
import java.time.format.DateTimeFormatter;
@@ -81,28 +90,30 @@ public RestHighLevelClient getRestClient() {
8190
public static final String INDEX = "harvester";
8291
private static final String TYPE = "oai";
8392

84-
private DocumentService documentService = null;
85-
private IndexService indexService = null;
86-
private SearchService searchService = null;
87-
private IndexRequest request;
8893

8994
@Autowired
90-
public OaiService(DocumentService documentService, IndexService indexService, SearchService searchService) {
91-
this.documentService = documentService;
92-
this.indexService = indexService;
93-
// indexService.createIndex();
94-
this.searchService = searchService;
95-
}
96-
97-
public String elasticSave(Article article) {
98-
IndexRequest request = new IndexRequest(INDEX, TYPE).setEntity(article);
95+
public OaiService()
96+
{
9997

98+
}
10099

101-
if (article.getId() != null) {
102-
request.setId(String.valueOf(article.getId()));
103-
}
100+
public void elasticSave(Article article) throws IOException {
101+
IndexRequest request = new IndexRequest(INDEX,TYPE);
102+
request.setPipeline("academic-pdf");
103+
// before using this pipeline we have to add pipeline to the elasticsearch by following command
104+
// PUT _ingest/pipeline/academic-pdf
105+
// {
106+
// "description": "parse pdfs and index into ES",
107+
// "processors" :
108+
// [
109+
// { "attachment" : { "field": "pdf" } },
110+
// { "remove" : { "field": "pdf" } }
111+
// ]
112+
// }
113+
114+
request.source(new Gson().toJson(article), XContentType.JSON);
115+
IndexResponse indexResponse = restClient.index(request);
104116

105-
return documentService.index(request);
106117
}
107118

108119

@@ -131,6 +142,7 @@ public void saveRecords(List<RecordType> recordTypes) {
131142
oaiRecord.setState(0);
132143
oaiRecords.add(oaiRecord);
133144

145+
//TODO: we ave to check all the parts name and assigned according to related name not order
134146
String[] parts = parsedDc.getDc().split(";;");
135147
Article article = new Article();
136148
article.setTitle(parts[0].split("::")[1]);
@@ -140,10 +152,27 @@ public void saveRecords(List<RecordType> recordTypes) {
140152
article.setPublisher(parts[4].split("::")[1]);
141153
article.setDate(parts[5].split("::")[1]);
142154
article.setType(parts[6].split("::")[1]);
155+
if (parts.length>10)
156+
{
157+
String downlaodUrl = parts[10].split("::")[1];
158+
article.setRelation(downlaodUrl);
159+
article.setBase64(UrlPdftoBase64(downlaodUrl));
160+
}
161+
else
162+
{
163+
article.setRelation("not available");
164+
article.setBase64("bm90IGF2YWlsYWJsZQ=="); //it means not available in base 64
165+
}
143166
article.setDc(parsedDc.getDc());
167+
article.setArticleIdentifier(parseIdentifier(oaiRecord.getIdentifier()));
168+
144169
articles.add(article);
145170

146-
elasticSave(article);
171+
try {
172+
elasticSave(article);
173+
} catch (IOException e) {
174+
e.printStackTrace();
175+
}
147176
});
148177

149178
oaiRecordRepository.save(oaiRecords);
@@ -167,6 +196,31 @@ private String marshallDc(MetadataType metadataType) {
167196
}
168197
}
169198

199+
public String UrlPdftoBase64(String url) {
200+
URL oracle = null;
201+
String base64 = "bm90IGF2YWlsYWJsZQ=="; //means not available
202+
System.out.println(url);
203+
try {
204+
oracle = new URL(url);
205+
URLConnection yc = oracle.openConnection();
206+
207+
BufferedInputStream bis = new BufferedInputStream(yc.getInputStream());
208+
209+
byte bytes[] = IOUtils.toByteArray(bis);
210+
bis.close();
211+
base64 = Base64.getEncoder().encodeToString(bytes);
212+
System.out.println(url);
213+
System.out.println(base64);
214+
} catch (MalformedURLException e) {
215+
e.printStackTrace();
216+
} catch (IOException e) {
217+
e.printStackTrace();
218+
}
219+
220+
return base64;
221+
222+
}
223+
170224
private LocalDateTime parseDateTime(String string) {
171225
LocalDateTime ldt;
172226
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'HH:mm:ss'Z']");
@@ -182,12 +236,27 @@ private LocalDateTime parseDateTime(String string) {
182236
// return LocalDateTime.parse(string, formatter);
183237
}
184238

239+
private String parseIdentifier(String oaiId){
240+
String Id="";
241+
Id = oaiId.substring(oaiId.lastIndexOf(':') + 1); // split identifier with ":" and take last part
242+
Id = Id.substring(Id.lastIndexOf('/') + 1); // split identifier with "/" and take last part
243+
return Id;
244+
}
185245

186-
public void delete() throws IOException {
187246

188-
//TODO:check if there is any indices with that name
189-
DeleteIndexRequest request = new DeleteIndexRequest("harvester");
190-
restClient.indices().deleteIndex(request);
247+
public void delete() {
248+
249+
try {
250+
DeleteIndexRequest request = new DeleteIndexRequest("harvester");
251+
restClient.indices().deleteIndex(request);
252+
} catch (ElasticsearchException exception) {
253+
if (exception.status() == RestStatus.NOT_FOUND) {
254+
System.out.println("Index not found and not deleted");
255+
}
256+
} catch (IOException e) {
257+
e.printStackTrace();
258+
}
259+
191260

192261
}
193262

0 commit comments

Comments
 (0)