是的,Apache Solr可以用作用于从不同网站索引和搜索文档的第三方系统。
以下是一个简单的代码示例,可用作从不同网站和不同格式的文档(如XML、JSON和CSV)中索引和搜索数据的起点。
1)在Solr中创建一个新的核(core)并定义一个schema.xml文件。 2)使用HTTP POST请求将文档索引到Solr中(请注意,下面的示例使用Python中的Requests库)。您可以从多个网站中获取文档,并将其转换为您所需的格式(在下面的示例中,我们将JSON文档转换为Solr可接受的XML格式)。
import requests import json import xml.etree.ElementTree as ET
solr_url = 'http://localhost:8983/solr/new_core/update'
json_doc = { "id": "test_doc_1", "title": "Sample document", "content": "This is a sample document." }
root = ET.Element("add") doc = ET.SubElement(root, "doc") for key, value in json_doc.items(): ET.SubElement(doc, "field", name=key).text = str(value) xml_doc = ET.tostring(root)
headers = {'Content-type': 'text/xml'} response = requests.post(solr_url, headers=headers, data=xml_doc) print(response.text)
3)使用Solr查询文档。使用HTTP GET请求(在下面的示例中,我们使用cURL)向Solr发送查询并得到文档列表。
solr_query_url = 'http://localhost:8983/solr/new_core/select?q=:'