Building a search engine using PHP and Apache Solr

王林
Release: 2023-06-25 08:30:02
Original
1274 people have browsed it

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:

  1. Efficiency: Solr can process large amounts of data and can quickly locate the required information when querying.
  2. Scalability: Solr can be expanded to hundreds of servers and supports horizontal expansion.
  3. Ease of use: Solr supports multiple query languages ​​and data formats, and configuration and deployment are relatively simple.

3. How to use Apache Solr?

  1. Installing 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.

  1. Create index

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:

  1. field: Defines the index field and data type.
  2. copyField: Define rules for copying from one field to another.
  3. uniqueKey: Defines the unique identifier of the indexed document.

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> 
Copy after login

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":["高效性", "可扩展性", "易用性"] }
]’
Copy after login

The above command means to import the data with IDs 1, 2, and 3 Import into the mycore index library.

  1. Query data

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
Copy after login

The above query means:

  1. Query conditions: title is Solr.
  2. Filter condition: content contains "open source".
  3. Sort: Sort by id in descending order.
  4. Paging: Starting from the 0th record, take 10 records.
  5. Return fields: Only title and id fields are returned.

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;
?>
Copy after login

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?

  1. Build index

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);
Copy after login

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.

  1. Query data

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);
Copy after login

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.

  1. Display data

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/>';
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!