Redis is a very common in-memory data storage middleware used in development. Previously, it was basically used for memory storage. Recently found that Redis launched a lot of enhancement modules, such as through RedisJSON can support native JSON object storage, using RediSearch can be used as a search engine, and support Chinese search! Today bring you RediSearch+RedisJSON as a search engine use practice, I hope to help you!
SpringBoot e-commerce project mall (50K + STAR) address: github.com/macrozheng/…
RedisMod profile
Let’s start with RedisMod, which is a series of Redis enhancements. With RedisMod support, Redis is going to be very powerful. RedisMod currently includes the following enhancements:
- RediSearch: a full-featured search engine;
- RedisJSON: Native support for JSON types;
- RedisTimeSeries: Sequential database support;
- RedisGraph: Graph database support;
- RedisBloom: Native support for probabilistic data;
- RedisGears: programmable data processing;
- RedisAI: Real-time model management and deployment for machine learning.
The installation
First we need to install Redis with all RedisMod, using Docker to install very convenient!
- Run the following command to download the RedisMod image:
docker pull redislabs/redismod:preview
Copy the code
- Run the RedisMod service in the container.
docker run -p 6379:6379 --name redismod \
-v /mydata/redismod/data:/data \
-d redislabs/redismod:preview
Copy the code
RedisJSON
With the RedisJSON module, Redis can store native JSON data. It allows you to easily access JSON properties, similar to what you can do with MongoDB. Here we will use RedisInsight to manipulate Redis.
- First of all by
JSON.SET
Command to add jSON-type key-value pairs to Redis, several commodity object data, since JSON is a tree structure, using$
The symbol represents adding data to the root node of the JSON;
JSON.SET product:1 $ {"id":1,"productSn":"7437788","name":" mi 8","subTitle":" full screen game smart phone 6GB+64GB black full netcom 4G ","brandName":" price":2699,"count":1}'
JSON.SET product:2 $ {"id":2,"productSn":"7437789","name":" hongmi 5A","subTitle":" full netcom 3GB+32GB Champagne Gold Mobile China Unicom 4G mobile phone ","brandName":" brandName","price":649,"count":5}'
JSON.SET product:3 $ '{"id":3,"productSn":"7437799","name":"Apple iPhone 8 Plus","subTitle":"64GB Red Special edition Mobile unicom, telecom 4 g phone ", "brandName" : "apple", "price" : 5499, "count" : 10} '
Copy the code
- After the data is inserted successfully, the following information is displayed in RedisInsight. The JSON data can be formatted and highlighted.
- And then you can go through
JSON.GET
Command to obtain the value of a JSON key/value pair.
JSON.GET product:1
Copy the code
- You can also get only the specified properties of the value. In RedisJSON, you need to get the properties of the JSON object with the
.
At the beginning.
JSON.GET product:1 .name .subTitle
Copy the code
- You can also go through
JSON.TYPE
Command to get the JSON object type.
JSON.TYPE product:1 .
Copy the code
RediSearch
With the RediSearch module, Redis can become a powerful full-text search engine, and native support for Chinese search, let’s try it!
- Before using RediSearch to search for data, we need to create an index. The syntax for creating an index is a bit complicated, so let’s look at it first.
FT.CREATE {index}
[ON {data_type}]
[PREFIX {count} {prefix} [{prefix} ..]
[LANGUAGE {default_lang}]
SCHEMA {identifier} [AS {attribute}]
[TEXT | NUMERIC | GEO | TAG ] [CASESENSITIVE]
[SORTABLE] [NOINDEX]] ...
Copy the code
-
You can use the FT.CREATE command to CREATE an index.
- Index: indicates the index name.
- Data_type: indicates the data type of the index. Currently, the value can be JSON or HASH.
- PREFIX: This allows you to select data prefixes to be indexed, for example
PREFIX 1 "product:"
Is represented by alpha in the keyproduct:
Index prefixed data; - LANGUAGE: Specifies the default LANGUAGE for the TEXT attribute.
- Identifier: Specifies the attribute name;
- Attribute: Specifies the attribute alias.
- The TEXT | NUMERIC | GEO | TAG: the type of these attributes are optional;
- SORTABLE: Specifies that attributes can be sorted.
-
Look at the syntax may not be very easy to understand, directly to the previous commodity data index to try to understand;
FT.CREATE productIdx ON JSON PREFIX 1 "product:" LANGUAGE chinese SCHEMA $.id AS id NUMERIC $.name AS name TEXT $.subTitle AS subTitle TEXT $.price AS price NUMERIC SORTABLE $.brandName AS brandName TAG
Copy the code
- Once the index is built, we can use it
FT.SEARCH
The data has been reviewed, such as using*
Can query all;
FT.SEARCH productIdx *
Copy the code
- Because we set it up
price
Field isSORTABLE
, we can takeprice
Return product information in descending order;
FT.SEARCH productIdx * SORTBY price DESC
Copy the code
- You can also specify fields to return;
FT.SEARCH productIdx * RETURN 3 name subTitle price
Copy the code
- We put the
brandName
Set up in order toTAG
Type, we can use the following statement to query the brand asmillet
orapple
The commodity;
FT.SEARCH productIdx '@ brandName: {} millet | apple'
Copy the code
- Due to the
price
isNUMERIC
Type, we can use the following statement to query the price in500 ~ 1000
The commodity;
FT.SEARCH productIdx '@price:[500 1000]'
Copy the code
- You can also do fuzzy queries with prefixes, similar to those in SQL
LIKE
, the use of*
Said.
FT.SEARCH productIdx '@ name: millet *'
Copy the code
- in
FT.SEARCH
Directly specify the search keyword in theTEXT
Type of properties for global search, support Chinese search, such as we search under containblack
Field of goods;
FT.SEARCH productIdx 'black'
Copy the code
- Of course, we can also specify the search field, such as search subtitle with
red
Field of goods;
FT.SEARCH productIdx '@ the subTitle: red'
Copy the code
- through
FT.DROPINDEX
Command to delete indexes if addedDD
Option, the data will be deleted together;
FT.DROPINDEX productIdx
Copy the code
- through
FT.INFO
Command to view the index status.
FT.INFO productIdx
Copy the code
- The search syntax for RediSearch is a bit more complex, but we can use it compared to SQL, as described in the following table.
Compare the Elasticsearch
RediSearch has been tested against Elasticsearch for some time now.
The index ability
RediSearch takes 221s to index Wikipedia’s 5.6 million (5.3GB) documents, Elasticsearch takes 349s, and RediSearch is 58% faster!
Query capabilities
RediSearch is 4 times faster than Elasticsearch with a throughput of 12.5K ops/ SEC compared to 3.1K ops/ SEC for Elasticsearch. Also RediSearch delay is 8ms, Elasticsearch is 10ms, RediSearch delay is slightly lower!
conclusion
Over the years, Redis has become more and more powerful, becoming more like a database than just a caching tool. RediSearch gives us another option to implement the search feature, and the performance is also very good, you can consider it if you do search related features!
If you want to learn more about Redis, you can try the full Redis program (50K+Star) at github.com/macrozheng/…
The resources
- The official document: developer.redis.com/howtos/redi…
- Reference manual: oss.redis.com/redisearch/
- Performance testing: redis.com/blog/search…