[英]Posting a pdf to Solr using Ajax
我正在嘗試將pdf文件推送(發布)到Solr / Tika,以使用Ajax / js進行文本提取和索引編制。 我已經使用以下curl命令來工作:
curl 'http://localhost:8983/solr/techproducts/update/extract?literal.id=doc1&commit=true' -F "myfile=@/PathToFile/SomeDoc.pdf"
此命令將所需的pdf放入Solr索引中,我可以正常檢索它。 但是,我需要能夠從Web瀏覽器執行此操作。 經過大量的搜索和一些實驗之后,我有了以下JS代碼ALMOST。 它返回0狀態碼和成功狀態,但沒有任何東西提交給索引:
$("#solrPost").click(function(event) {
event.stopPropagation();
event.preventDefault();
/* Read a local pdf file as a blob */
let fileAsBlob = null;
let file = $('#upload_file')[0].files[0];
let myReader = new FileReader();
myReader.onloadend = function() {
fileAsBlob = myReader.result;
sendToSolr(fileAsBlob);
};
fileAsBlob = myReader.readAsArrayBuffer(file);
function sendToSolr(fileAsBlob) {
$.ajax({
url:"http://localhost:8983/solr/techproducts/update/extract?literal.id=doc2&commit=true",
type: 'POST',
data: fileAsBlob,
cache: false,
crossOrigin: true,
dataType: 'jsonp',
jsonp: 'json.wrf',
processData: false,
contentType: false,
success: function(data, status) {
console.log("Ajax.post successful, status: " + data.responseHeader.status + "\t status text: " + status);
console.log("debug");
},
error: function(data, status) {
console.log("Ajax.post error, status: " + data.status + "\t status text:" + data.statusText);
},
done: function(data, status) {
console.log("Ajax.post Done");
}
});
}
這已經很接近工作了,但是我無法弄清楚出了什么問題。 所有指示(從客戶端)都是好的,但是沒有任何內容添加到索引中。 注意:
我知道也有類似的帖子,但是都沒有解決在提供的帖子腳本之外使用Solr / Tika提取pdf的問題。 提前致謝。
好吧,它花了一些時間搜索,但是由於“ tonejac”的帖子,我找到了解決方案。 如果您查看:[ JQuery Ajax正在發送GET而不是POST 。最后一個注釋指出,如果您使用dataType:jsonp,則“ POST”將轉換為“ GET”。 我刪除了jsonp,安裝了一個插件來處理我試圖通過使用jsonp避免的CORS問題,而中提琴則奏效了。 對於那些感興趣的人,工作代碼發布在下面。 它不花哨或堅固,但允許我從Web應用程序發布或獲取文檔(.pdf,.docx ...)到Solr。 我只發布了js代碼,但html很簡單,並提供了“文件”類型的輸入,以及設置ID以便發布文檔或按ID搜索的輸入。 有兩個按鈕,solrPost和solrGet,它們調用js中的偵聽器。 從html onLoad調用connectSolr()函數。
function connectSolr() {
$("#solrPost").click(function(event) {
event.stopPropagation();
event.preventDefault();
/* Read a local pdf file as a blob */
let fileAsBlob = null;
let file = $('#upload_file')[0].files[0];
let myReader = new FileReader();
myReader.onloadend = function() {
fileAsBlob = myReader.result;
sendToSolr(fileAsBlob);
};
fileAsBlob = myReader.readAsArrayBuffer(file);
/* Get the unique Id for the doc and append to the extract url*/
let docId = $("#userSetId").val();
let extractUrl = "http://localhost:8983/solr/techproducts/update/extract/?commit=true&literal.id=" + docId;
/* Ajax call to Solr/Tika to extract text from pdf and index it */
function sendToSolr(fileAsBlob) {
$.ajax({
url: extractUrl,
type: 'POST',
data: fileAsBlob,
cache: false,
jsonp: 'json.wrf',
processData: false,
contentType: false,
echoParams: "all",
success: function(data, status) {
console.log("Ajax.post successful, status: " + data.responseHeader.status + "\t status text: " + status);
console.log("debug");
},
error: function(data, status) {
console.log("Ajax.post error, status: " + data.status + "\t status text:" + data.statusText);
},
done: function(data, status) {
console.log("Ajax.post Done");
},
});
}
});
$("#solrGet").click(function(event) {
event.stopPropagation();
event.preventDefault();
let docId = "id:" + $("#docId").val();
$.ajax({
url:"http://localhost:8983/solr/techproducts/select/",
type: "get",
dataType: "jsonp",
data: {
q: docId
//wt: "json",
//indent: "true"
},
jsonp: "json.wrf",
//"json.wrf": "?",
success: function(data, status) {
renderDoc(data, status);
},
error: function(data, status) {
console.log("Ajax.get error, Error: " + status);
},
done: function(data, status) {
console.log("Ajax.get Done");
}
});
console.log("Debug");
});
let renderDoc = function(theText, statusCode) {
let extractedText = theText.response.docs[0].content[0];
let extractedLinks = theText.response.docs[0].links;
let $textArea = $("#textArea");
$textArea.empty();
let sents = extractedText.split('\n')
sents.map(function(element, i) {
let newSpan = $("<span />");
$textArea.append(newSpan.html(element).append("<br/>"));
});
console.log("debug");
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.