With the popularity of the Internet and mobile devices, search engines have become the first choice for people to obtain information and find answers. Building an efficient and accurate search engine requires the use of a variety of technologies and tools. This article will introduce how to use PHP and Apache Solr to build a search engine.
1. What is Apache Solr?
Apache Solr is an open source search platform based on Lucene, written in Java, which provides fast, scalable and efficient text search and analysis functions. Solr can store, index, and search text, XML, JSON and other data formats. It supports multiple query languages, such as Lucene query syntax, SQL, XPath, XSLT, etc.
2. Why choose Apache Solr?
Compared with other search engine software, Solr has the following advantages:
3. How to use Apache Solr?
First you need to download the latest version of Apache Solr and extract it to the appropriate directory. Then, follow the official documentation (https://lucene.apache.org/solr/guide/8_6/) to configure and start.
The core function of Solr is to index and search data. Indexing is to store data on the Solr server in a certain way. Solr supports multiple data formats, such as JSON, XML, CSV, etc.
The following uses JSON format as an example to introduce how to create an index:
First, you need to define a schema.xml file to describe the data structure and index configuration. schema.xml contains the following content:
For example:
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="title" type="text_general" indexed="true" stored="true" multiValued="false" /> <field name="content" type="text_general" indexed="true" stored="true" multiValued="true" /> <copyField source="title" dest="text" /> <copyField source="content" dest="text" /> <uniqueKey>id</uniqueKey>
Next, use the curl command to import the data into Solr:
curl http://localhost:8983/solr/mycore/update -H “Content-Type:application/json" -d '[ { "id":"1", "title":"Solr是什么", "content":"Solr是一款开源搜索引擎" }, { "id":"2", "title":"Solr如何使用", "content":"可以使用Java或HTTP协议发送请求到Solr服务器" }, { "id":"3", "title":"Solr的优势是什么", "content":["高效性", "可扩展性", "易用性"] } ]’
The above command means to import the data with IDs 1, 2, and 3 Import into the mycore index library.
Solr’s query language supports a variety of query methods, such as wildcard query, phrase query, range query, Boolean query, etc. The following takes HTTP query as an example:
http://localhost:8983/solr/mycore/select?q=title:Solr&fq=content:开源&sort=id+desc&start=0&rows=10&fl=title,id
The above query means:
4. How to use PHP to connect to Solr server?
PHP provides curl extension, which can be used to send HTTP requests. The following is a simple example of connecting to the Solr server:
<?php $url = 'http://localhost:8983/solr/mycore/select?q=title:Solr'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo $response; ?>
The above code means sending a query request to the Solr server, and the returned result will be stored in the $response variable.
5. How to use PHP and Solr to build a search engine?
First, you need to import the data to be searched into Solr. You can use the curl command or write code in PHP to perform the import operation. For example:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://localhost:8983/solr/mycore/update?commitWithin=1000'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); $response = curl_exec($ch); curl_close($ch);
The above code indicates using PHP to import the data in $json_data into an index library named mycore, and complete the submission operation within 1 second.
Use PHP to connect to the Solr server, call the query interface, and return query results. For example:
$url = 'http://localhost:8983/solr/mycore/select?q='.$query.'&start='.($page-1)*$rows.'&rows='.$rows.'&wt=json&indent=true'; $response = file_get_contents($url);
The above code means defining a query condition $query. On page $page, each page displays $rows pieces of data. Use the file_get_contents function to get query results from the Solr server.
Parse the query results into a PHP array, and then perform paging, sorting, filtering and other operations as needed, and finally display the data on the page . For example:
$data = json_decode($response, true); foreach ($data['response']['docs'] as $doc) { echo '<a href="'.$doc['url'].'">'.$doc['title'].'</a><br/>'; }
The above code indicates traversing the query results and displaying the title and link of each piece of data.
6. Summary
This article introduces how to use PHP and Apache Solr to build a search engine. Through a detailed explanation of Solr's basic functions, usage methods, and combination with PHP, readers can quickly master the use of Solr and use PHP to write search engine code. Solr provides powerful search and analysis capabilities and is ideal for building various types of search engines.
The above is the detailed content of Building a search engine using PHP and Apache Solr. For more information, please follow other related articles on the PHP Chinese website!