Search index supports the IP data type. This type provides efficient storage and querying of IPv4 and IPv6 addresses for scenarios such as log analysis, network security, and geo-location.
To use the IP data type for a search index, and contact Tablestore technical support to enable the feature.
How it works
To use the IP data type, you can map a String field from your data table to the IP type in the search index. The IP data type supports three query methods: term query, range query, and composite query. A specialized IP address index schema provides unified storage and efficient retrieval for IPv4, IPv6, and their variant formats.
Supported IP formats
The IP data type supports multiple IP address formats. The following table describes the supported formats.
IP address format | Description | |
IPv4 | An IPv4 address format. It consists of four groups of decimal numbers separated by periods (.). |
|
IPv6 | An IPv6 address format. It consists of eight groups of four hexadecimal digits separated by colons (:). |
|
Abbreviated IPv6 format | Supports the standard abbreviated notation for IPv6 addresses. |
|
IPv4-mapped IPv6 address | The IPv4-mapped IPv6 address format. It supports queries using the IPv4 format. Note For example, if the value of the `column_ip` column in a row is |
|
Query scenarios
IP type queries support three methods: term query (exact match), range query, and composite query. Each method is suitable for different business scenarios.
Query type | Description | Implementation | Application scenario |
Term query | Queries for data that matches a specific IP address. | Use a term query with a specific IP address. |
|
Range query | Queries for data within a specified IP address range. |
|
|
Composite query | Queries for data based on a combination of an IP address and other dimensions. | Use the composite query feature. |
|
Use an IP type field
You can use the IP type field with the Tablestore Java software development kit (SDK) and Go SDK. This topic uses the Java SDK as an example. You must use version 5.17.6 or later of the Tablestore Java SDK.
Step 1: Create a search index that contains an IP type
When you create a search index, map the String field that you want to use for IP queries to the IP type.
private static void createSearchIndex(SyncClient client) {
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
request.setTableName(TABLE_NAME);
request.setIndexName(INDEX_NAME);
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("Col_Keyword", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
// Set the index for the Col_Ip column to the IP type.
new FieldSchema("Col_Ip", FieldType.IP).setIndex(true).setEnableSortAndAgg(true)
));
request.setIndexSchema(indexSchema);
client.createSearchIndex(request);
}Step 2: Write IP data to the data table
Write IP address data to the field that you configured as the IP type.
private static void putRow(SyncClient client) {
String[] keywords = { "Router", "Phone", "PC1", "PC2", "Home Bot" };
for (int i = 0; i < 5; i++) {
// Build the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_1, PrimaryKeyValue.fromString("pk1" + i));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowPutChange rowPutChange = new RowPutChange(TABLE_NAME, primaryKey);
// Add an attribute column.
rowPutChange.addColumn("Col_Keyword", ColumnValue.fromString(keywords[i]));
// Add an attribute column and write the IP address.
rowPutChange.addColumn("Col_Ip", ColumnValue.fromString("192.168.1." + i));
client.putRow(new PutRowRequest(rowPutChange));
}
}Step 3: Verify the configuration
Verify a term IP query
/** * To query for an exact IP address, use TermQuery. */ private static void searchExactIp(SyncClient client) { SearchQuery searchQuery = new SearchQuery(); TermQuery query = QueryBuilders.term("Col_Ip", "192.168.1.1").build(); searchQuery.setQuery(query); searchQuery.setLimit(100); SearchRequest searchRequest = new SearchRequest(TABLE_NAME, INDEX_NAME, searchQuery); SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet(); columnsToGet.setReturnAll(true); searchRequest.setColumnsToGet(columnsToGet); SearchResponse resp = client.search(searchRequest); System.out.println("TotalCount: " + resp.getTotalCount()); System.out.println("Row: " + resp.getRows()); }Verify an IP range query
/** * To query for an IP address range, you can use CIDR notation in a TermQuery or use a RangeQuery. */ private static void searchIpSegment(SyncClient client) { SearchQuery searchQuery = new SearchQuery(); // Query for an IP range using CIDR notation. TermQuery query = QueryBuilders.term("Col_Ip", "192.168.1.1/24").build(); // Alternatively, use a RangeQuery to query for an IP range. // RangeQuery query = QueryBuilders.range("Col_Ip").greaterThanOrEqual("192.168.1.0").lessThanOrEqual("192.168.1.255").build(); searchQuery.setQuery(query); searchQuery.setLimit(100); SearchRequest searchRequest = new SearchRequest(TABLE_NAME, INDEX_NAME, searchQuery); SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet(); columnsToGet.setReturnAll(true); searchRequest.setColumnsToGet(columnsToGet); SearchResponse resp = client.search(searchRequest); System.out.println("TotalCount: " + resp.getTotalCount()); System.out.println("Row: " + resp.getRows()); }
Production use
Range query recommendations
When you perform a range query, we recommend that you specify both the upper and lower borders of the IP address range. If your data table contains both IPv4 and IPv6 addresses, a query with only a single border condition for an IPv4 address might return unwanted IPv6 addresses.
CIDR notation recommendation
For IP range queries, we recommend using CIDR notation, such as
192.168.1.1/24. This method is more concise and accurate than traditional range queries and helps prevent errors when you set border conditions.