我正在做一个小项目,通过使用 Apache Solr 实例来检索一些搜索结果。在我的客户端应用程序中,我使用 solr-node 与 Node.js 中的 Solr 进行交互。代码如下:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
// add Apache Solr and integrate it with node.js
const SolrNode = require('solr-node');
const client = new SolrNode({
host:'localhost',
port:'8983',
core:'mycollection',
protocol:'http'
});
app.get('/search', (req, res) => {
const strNewQuery = client.query()
.q('text: searchkeyword')
.addParams({
wt: 'json',
indent: true
});
client.search(strNewQuery, (err, result) => {
if (err) {
console.error('Error querying Solr:', err);
res.status(500).send('Error querying Solr');
res.redirect('/');
} else {
// res.json(result.response.docs);
console.log('Response: ', result.response);
console.log('node.js fetching is successful!');
const formattedResults = result.response.docs.map((doc) => ( {
id: doc.id,
titile: doc.filename
}));
res.render("list", {listTitle: "Probe Search Result", newListItems: formattedResults});
}
});
});
app.get("/about", function(req, res) {
res.render("about");
});
app.use(express.static(__dirname + '/public'));
// Start the Server
const port = 3030;
app.listen(port, ()=> {
console.log('Solr-node server listening on port: ' + port);
});
在这里,我将搜索关键字硬编码为变量 strNewQuery,这样如果我运行 url“http”//localhost:3030/search”,来自 Apache Solr 实例的响应结果将显示在网页中。我使用list.ejs 来显示我的结果。solr-node 与我自己的 solr 集合配合得很好,我将我的 solr-node 结果与 Solr Admin 结果进行了比较。它们看起来不错。 然而,硬编码的搜索词只是一个模型测试,不是我的目标。如何从客户端程序将查询搜索关键字从 HTML 输入表单传递到 solr-node 服务器?为了解决这个问题,我执行了以下代码:
//filename: clientSolr.js, given in the <script> part in HTML
const solr = require('solr-client');
// create a Solr client
const client = solr.createClient({
host: 'localhost',
port: 8983,
core: 'mycollection',
protocol: 'http'
});
//Handle form submission
document.getElementById('searchForm').addEventListener('submit', async (e) => {
e.preventDefault();
const query = document.getElementById('queryInput').value;
try {
// Execute Solr query
const response = await client.search(query, { wt: 'json' }); // Specify JSON response format
const docs = response.response.docs; // Extract search results
const formattedResults = result.response.docs.map((doc) => ( {
id: doc.id,
titile: doc.filename
}));
formattedResults.forEach((formatResult) => {
console.log('result id: ', formatResult.id);
console.log('result filename: ', formatResult.filename);
});
// Display results
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = ''; // Clear previous results
docs.forEach((doc) => {
resultsDiv.innerHTML += `<p>${doc.id}</p>`; // Display relevant data (adjust as needed)
});
}
catch (error)
{
console.error('Error fetching Solr data:', error);
}
});
// }
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Solr client Page</title>
</head>
<body>
<form id="searchForm">
<input type="text" id="queryInput" placeholder="Enter your search query">
<button type="submit">Search</button>
</form>
<div id="results"></div>
<script type="text/javascript" src="clientSolr.js">
</script>
</body>
</html>
在这里,我在客户端 JavaScript 中使用 solr-client 库,并通过从 HTML 中识别元素 id 来检索查询关键字。但这些代码不起作用。我知道我无法使用全局变量 document 来检索 node.js 代码中的 elementId,因为后端无法识别该文档。我的问题是,即使 Node.js 无法识别全局变量 document,如何在 solr-node 程序中传递查询搜索关键字。我知道 solr-client 和 solr-node 之间的区别,但似乎两者都无法处理来自 HTML 输入表单的查询搜索关键字。我该如何解决这个问题?
您有一个 HTML 页面,其中有一些 JS 在您的浏览器中运行(PAGE)。
然后你的服务器上运行了一些 NodeJS 代码,监听端口 3030 (NODE)。
最后你可以运行 Solr (SOLR)。
现在,您已经有了一个有效的 NODE->SOLR 连接。你做错的地方是把 Solr 客户端代码放在你的 PAGE 中。它应该只在您的 NODE 服务器上运行。
因此,您的 PAGE 代码必须传递查询框中的文本并将其传递给 NODE,例如请求参数:
http://localhost:3030/search?myQuery=hello
。接下来,您必须从 NODE 服务器上的 req
中提取该请求参数,并将其进一步传递给 Solr。
祝你好运!