Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
52bf5d6c
Commit
52bf5d6c
authored
Apr 30, 2017
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Async API>
parent
79c023b4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
122 additions
and
6 deletions
+122
-6
Client.cs
NRediSearch/Client.cs
+122
-6
No files found.
NRediSearch/Client.cs
View file @
52bf5d6c
...
...
@@ -3,6 +3,7 @@
using
StackExchange.Redis
;
using
System
;
using
System.Collections.Generic
;
using
System.Threading.Tasks
;
namespace
NRediSearch
{
...
...
@@ -81,6 +82,28 @@ public bool CreateIndex(Schema schema, IndexOptions options)
return
(
string
)
_db
.
Execute
(
"FT.CREATE"
,
args
)
==
"OK"
;
}
/// <summary>
/// Create the index definition in redis
/// </summary>
/// <param name="schema">a schema definition <seealso cref="Schema"/></param>
/// <param name="options">index option flags <seealso cref="IndexOptions"/></param>
/// <returns>true if successful</returns>
public
async
Task
<
bool
>
CreateIndexAsync
(
Schema
schema
,
IndexOptions
options
)
{
var
args
=
new
List
<
object
>();
args
.
Add
(
_boxedIndexName
);
SerializeRedisArgs
(
options
,
args
);
args
.
Add
(
"SCHEMA"
.
Literal
());
foreach
(
var
f
in
schema
.
Fields
)
{
f
.
SerializeRedisArgs
(
args
);
}
return
(
string
)
await
_db
.
ExecuteAsync
(
"FT.CREATE"
,
args
)
==
"OK"
;
}
/// <summary>
/// Search the index
/// </summary>
...
...
@@ -96,6 +119,21 @@ public SearchResult Search(Query q)
return
new
SearchResult
(
resp
,
!
q
.
NoContent
,
q
.
WithScores
,
q
.
WithPayloads
);
}
/// <summary>
/// Search the index
/// </summary>
/// <param name="q">a <see cref="Query"/> object with the query string and optional parameters</param>
/// <returns>a <see cref="SearchResult"/> object with the results</returns>
public
async
Task
<
SearchResult
>
SearchAsync
(
Query
q
)
{
var
args
=
new
List
<
object
>();
args
.
Add
(
_boxedIndexName
);
q
.
SerializeRedisArgs
(
args
);
var
resp
=
(
RedisResult
[])
await
_db
.
ExecuteAsync
(
"FT.SEARCH"
,
args
);
return
new
SearchResult
(
resp
,
!
q
.
NoContent
,
q
.
WithScores
,
q
.
WithPayloads
);
}
/// <summary>
/// Add a single document to the query
/// </summary>
...
...
@@ -106,6 +144,27 @@ public SearchResult Search(Query q)
/// <param name="replace">if set, and the document already exists, we reindex and update it</param>
/// <param name="payload">if set, we can save a payload in the index to be retrieved or evaluated by scoring functions on the server</param>
public
bool
AddDocument
(
string
docId
,
Dictionary
<
string
,
RedisValue
>
fields
,
double
score
=
1.0
,
bool
noSave
=
false
,
bool
replace
=
false
,
byte
[]
payload
=
null
)
{
var
args
=
BuildAddDocumentArgs
(
docId
,
fields
,
score
,
noSave
,
replace
,
payload
);
return
(
string
)
_db
.
Execute
(
"FT.ADD"
,
args
)
==
"OK"
;
}
/// <summary>
/// Add a single document to the query
/// </summary>
/// <param name="docId">the id of the document. It cannot belong to a document already in the index unless replace is set</param>
/// <param name="score">the document's score, floating point number between 0 and 1</param>
/// <param name="fields">a map of the document's fields</param>
/// <param name="noSave">if set, we only index the document and do not save its contents. This allows fetching just doc ids</param>
/// <param name="replace">if set, and the document already exists, we reindex and update it</param>
/// <param name="payload">if set, we can save a payload in the index to be retrieved or evaluated by scoring functions on the server</param>
public
async
Task
<
bool
>
AddDocumentAsync
(
string
docId
,
Dictionary
<
string
,
RedisValue
>
fields
,
double
score
=
1.0
,
bool
noSave
=
false
,
bool
replace
=
false
,
byte
[]
payload
=
null
)
{
var
args
=
BuildAddDocumentArgs
(
docId
,
fields
,
score
,
noSave
,
replace
,
payload
);
return
(
string
)
await
_db
.
ExecuteAsync
(
"FT.ADD"
,
args
)
==
"OK"
;
}
private
List
<
object
>
BuildAddDocumentArgs
(
string
docId
,
Dictionary
<
string
,
RedisValue
>
fields
,
double
score
,
bool
noSave
,
bool
replace
,
byte
[]
payload
)
{
var
args
=
new
List
<
object
>
{
_boxedIndexName
,
docId
,
score
};
if
(
noSave
)
...
...
@@ -129,8 +188,7 @@ public bool AddDocument(string docId, Dictionary<string, RedisValue> fields, dou
args
.
Add
(
ent
.
Key
);
args
.
Add
(
ent
.
Value
);
}
return
(
string
)
_db
.
Execute
(
"FT.ADD"
,
args
)
==
"OK"
;
return
args
;
}
/// <summary>
...
...
@@ -139,6 +197,12 @@ public bool AddDocument(string docId, Dictionary<string, RedisValue> fields, dou
public
bool
ReplaceDocument
(
string
docId
,
Dictionary
<
string
,
RedisValue
>
fields
,
double
score
=
1.0
,
byte
[]
payload
=
null
)
=>
AddDocument
(
docId
,
fields
,
score
,
false
,
true
,
payload
);
/// <summary>
/// replaceDocument is a convenience for calling addDocument with replace=true
/// </summary>
public
Task
<
bool
>
ReplaceDocumentAsync
(
string
docId
,
Dictionary
<
string
,
RedisValue
>
fields
,
double
score
=
1.0
,
byte
[]
payload
=
null
)
=>
AddDocumentAsync
(
docId
,
fields
,
score
,
false
,
true
,
payload
);
/// <summary>
/// Index a document already in redis as a HASH key.
/// </summary>
...
...
@@ -149,14 +213,28 @@ public bool ReplaceDocument(string docId, Dictionary<string, RedisValue> fields,
public
bool
AddHash
(
string
docId
,
double
score
,
bool
replace
)
{
var
args
=
new
List
<
object
>
{
_boxedIndexName
,
docId
,
score
};
if
(
replace
)
{
args
.
Add
(
"REPLACE"
.
Literal
());
}
return
(
string
)
_db
.
Execute
(
"FT.ADDHASH"
,
args
)
==
"OK"
;
}
/// <summary>
/// Index a document already in redis as a HASH key.
/// </summary>
/// <param name="docId">the id of the document in redis. This must match an existing, unindexed HASH key</param>
/// <param name="score">the document's index score, between 0 and 1</param>
/// <param name="replace">if set, and the document already exists, we reindex and update it</param>
/// <returns>true on success</returns>
public
async
Task
<
bool
>
AddHashAsync
(
string
docId
,
double
score
,
bool
replace
)
{
var
args
=
new
List
<
object
>
{
_boxedIndexName
,
docId
,
score
};
if
(
replace
)
{
args
.
Add
(
"REPLACE"
.
Literal
());
}
return
(
string
)
await
_db
.
ExecuteAsync
(
"FT.ADDHASH"
,
args
)
==
"OK"
;
}
/// <summary>
/// Get the index info, including memory consumption and other statistics.
...
...
@@ -165,8 +243,20 @@ public bool AddHash(string docId, double score, bool replace)
/// <returns>a map of key/value pairs</returns>
public
Dictionary
<
string
,
RedisValue
>
GetInfo
()
{
var
res
=
(
RedisValue
[])
_db
.
Execute
(
"FT.INFO"
,
_boxedIndexName
);
return
ParseGetInfo
(
_db
.
Execute
(
"FT.INFO"
,
_boxedIndexName
));
}
/// <summary>
/// Get the index info, including memory consumption and other statistics.
/// </summary>
/// <remarks>TODO: Make a class for easier access to the index properties</remarks>
/// <returns>a map of key/value pairs</returns>
public
async
Task
<
Dictionary
<
string
,
RedisValue
>>
GetInfoAsync
()
{
return
ParseGetInfo
(
await
_db
.
ExecuteAsync
(
"FT.INFO"
,
_boxedIndexName
));
}
static
Dictionary
<
string
,
RedisValue
>
ParseGetInfo
(
RedisResult
value
)
{
var
res
=
(
RedisValue
[])
value
;
var
info
=
new
Dictionary
<
string
,
RedisValue
>();
for
(
int
i
=
0
;
i
<
res
.
Length
;
i
+=
2
)
{
...
...
@@ -187,6 +277,16 @@ public bool DeleteDocument(string docId)
return
(
long
)
_db
.
Execute
(
"FT.DEL"
,
_boxedIndexName
,
docId
)
==
1
;
}
/// <summary>
/// Delete a document from the index.
/// </summary>
/// <param name="docId">the document's id</param>
/// <returns>true if it has been deleted, false if it did not exist</returns>
public
async
Task
<
bool
>
DeleteDocumentAsync
(
string
docId
)
{
return
(
long
)
await
_db
.
ExecuteAsync
(
"FT.DEL"
,
_boxedIndexName
,
docId
)
==
1
;
}
/// <summary>
/// Drop the index and all associated keys, including documents
/// </summary>
...
...
@@ -195,6 +295,14 @@ public bool DropIndex()
{
return
(
string
)
_db
.
Execute
(
"FT.DROP"
,
_boxedIndexName
)
==
"OK"
;
}
/// <summary>
/// Drop the index and all associated keys, including documents
/// </summary>
/// <returns>true on success</returns>
public
async
Task
<
bool
>
DropIndexAsync
()
{
return
(
string
)
await
_db
.
ExecuteAsync
(
"FT.DROP"
,
_boxedIndexName
)
==
"OK"
;
}
/// <summary>
/// Optimize memory consumption of the index by removing extra saved capacity. This does not affect speed
...
...
@@ -203,5 +311,13 @@ public long OptimizeIndex()
{
return
(
long
)
_db
.
Execute
(
"FT.OPTIMIZE"
,
_boxedIndexName
);
}
/// <summary>
/// Optimize memory consumption of the index by removing extra saved capacity. This does not affect speed
/// </summary>
public
async
Task
<
long
>
OptimizeIndexAsync
()
{
return
(
long
)
await
_db
.
ExecuteAsync
(
"FT.OPTIMIZE"
,
_boxedIndexName
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment