Indexing PostgreSQL with Apache Solr
Searching and filtering large IP address datasets within PostgreSQL can be challenging. Why? Databases excel at data storage and structured queries, but often struggle with full-text search and complex analysis. Apache Solr, a high-performance search engine built on top of Lucene, is designed to handle these tasks with remarkable speed and flexibility.
What do we need?
- A running PostgreSQL database with a table containing IP address information (named "ip_loc" in our example).
- A basic installation of Apache Solr.
Setting up Apache Solr
1. Create a Solr Core:
bin/solr create -c ip_data -d _default2. Define the fields
Solr has no IP address field type. The usual trick is to store each IPv4 address as a number, so range queries work. The default configset ships the plong type. Add these fields to the managed schema (or schema.xml if you use a classic schema):
<field name="start_ip" type="plong" indexed="true" stored="true"/> <field name="end_ip" type="plong" indexed="true" stored="true"/> <field name="iso2" type="string" indexed="true" stored="true"/> <field name="state" type="text_general" indexed="true" stored="true"/> <field name="city" type="text_general" indexed="true" stored="true"/>
Integrating PostgreSQL and Solr
The examples assume ip_loc stores start_ip and end_ip as inet columns. In PostgreSQL, subtracting '0.0.0.0'::inet from an address gives its numeric value as a bigint, which is exactly what Solr needs.
Solr 8.x: DataImportHandler (DIH). DIH was deprecated in Solr 8.6 and removed in Solr 9, so use it only on older installs. Put the config into data-config.xml in the core's conf directory:
<dataConfig>
<dataSource type="JdbcDataSource"
driver="org.postgresql.Driver"
url="jdbc:postgresql://localhost/your_database"
user="your_username"
password="your_password"/>
<document>
<entity name="ip_data"
query="SELECT (start_ip - '0.0.0.0'::inet) AS id,
(start_ip - '0.0.0.0'::inet) AS start_ip,
(end_ip - '0.0.0.0'::inet) AS end_ip,
iso2, state, city
FROM ip_loc">
<field column="id" name="id"/>
<field column="start_ip" name="start_ip"/>
<field column="end_ip" name="end_ip"/>
<field column="iso2" name="iso2"/>
<field column="state" name="state"/>
<field column="city" name="city"/>
</entity>
</document>
</dataConfig>Register the handler in solrconfig.xml (the DIH jar and the PostgreSQL JDBC driver must be on the classpath):
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>Then start the import:
curl 'http://localhost:8983/solr/ip_data/dataimport?command=full-import'
Solr 9.x: export to JSON and post it. Without DIH, the simplest path is to let PostgreSQL build the JSON and send it to the update handler:
psql -d your_database -At -c "SELECT json_agg(t) FROM (
SELECT (start_ip - '0.0.0.0'::inet) AS id,
(start_ip - '0.0.0.0'::inet) AS start_ip,
(end_ip - '0.0.0.0'::inet) AS end_ip,
iso2, state, city
FROM ip_loc) t" > ip_loc.json
curl 'http://localhost:8983/solr/ip_data/update?commit=true' \
-H 'Content-Type: application/json' --data-binary @ip_loc.jsonQuerying Solr
Convert the address to its number first (192.168.0.1 is 3232235521, 10.0.0.0 is 167772160; in PostgreSQL: SELECT '192.168.0.1'::inet - '0.0.0.0'::inet;).
- Which range contains 192.168.1.20 (3232235796)?
start_ip:[* TO 3232235796] AND end_ip:[3232235796 TO *] - All ranges starting inside 192.168.0.1 to 192.168.255.255:
start_ip:[3232235521 TO 3232301055] - Geolocation Filtering:
iso2:US AND state:California - Combined Search:
city:"New York" AND start_ip:[167772160 TO 184549375]
Benefits vs. Pure PostgreSQL
- Performance: Solr's inverted indexes provide superior search speed.
- Scalability: Solr easily distributes across multiple machines.
- Flexibility: Solr's query syntax offers rich search capabilities.
My take
By combining PostgreSQL and Apache Solr, you create a robust IP address management system that scales efficiently while providing fast search functionality.
Need help with distributed systems, backend engineering or data platforms? See my consulting services.