IP
# IP
提示
ES itself has a very friendly support for IP queries, so it is very suitable for storing and querying IP, it is worth noting that the index type of the field corresponding to IP must be established as ip type, not is text, keyword or default type, otherwise you will find in the later query you do not want to achieve the IP range search.
# best practices
public class Document{
// Omit other fields...
@IndexField(fieldType = FieldType.IP)
private String ipAddress;
}
1
2
3
4
5
2
3
4
5
Add, delete and other types of fields are not the same, no longer demonstrate, assuming I have entered an ip address of "192.168.1.1" to es
@Test
public void testIp(){
// Precise query
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getIpAddress, "192.168.1.1");
List<Document> documents = documentMapper.selectList(wrapper);
// Range search
LambdaEsQueryWrapper<Document> wrapper1 = new LambdaEsQueryWrapper<>();
wrapper1.eq(Document::getIpAddress, "192.168.0.0/16");
List<Document> documents1 = documentMapper.selectList(wrapper);
// ip fuzzy match es not supported
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
For the above IPv4 IP address contains 4 bytes, and each byte contains 8 digits, in the above /16 that means the front 16 digits, that is, 192.168, so as long as the IP is located in 192.168.0.0 to 192.168.255.255 can be retrieved by the above search criteria.
Help us improve this document (opens new window)
Last update: 2024/03/29