示例#1
0
        public void TestReturnWithFieldNames()
        {
            if (getModuleSearchVersion() <= 20200)
            {
                return;
            }

            Client cl = GetClient();
            Schema sc = new Schema().AddTextField("a").AddTextField("b").AddTextField("c");

            Assert.True(cl.CreateIndex(sc, new ConfiguredIndexOptions()));

            var doc = new Dictionary <string, RedisValue>
            {
                { "a", "value1" },
                { "b", "value2" },
                { "c", "value3" }
            };

            Assert.True(cl.AddDocument("doc", doc));

            // Query
            SearchResult res = cl.Search(new Query("*").ReturnFields(FieldName.Of("b").As("d"), FieldName.Of("a")));

            Assert.Equal(1, res.TotalResults);
            Assert.Equal("doc", res.Documents[0].Id);
            Assert.Equal("value1", res.Documents[0]["a"]);
            Assert.Equal("value2", res.Documents[0]["d"]);
        }
        public void TestWithFieldNames()
        {
            Client          cl         = GetClient();
            IndexDefinition defenition = new IndexDefinition(prefixes: new string[] { "student:", "pupil:" });
            Schema          sc         = new Schema().AddTextField(FieldName.Of("first").As("given")).AddTextField(FieldName.Of("last"));

            Assert.True(cl.CreateIndex(sc, new ConfiguredIndexOptions(defenition)));

            var docsIds  = new string[] { "student:111", "pupil:222", "student:333", "teacher:333" };
            var docsData = new Dictionary <string, RedisValue>[] {
                new Dictionary <string, RedisValue> {
                    { "first", "Joen" },
                    { "last", "Ko" },
                    { "age", "20" }
                },
                new Dictionary <string, RedisValue> {
                    { "first", "Joe" },
                    { "last", "Dod" },
                    { "age", "18" }
                },
                new Dictionary <string, RedisValue> {
                    { "first", "El" },
                    { "last", "Mark" },
                    { "age", "17" }
                },
                new Dictionary <string, RedisValue> {
                    { "first", "Pat" },
                    { "last", "Rod" },
                    { "age", "20" }
                }
            };

            for (int i = 0; i < docsIds.Length; i++)
            {
                Assert.True(cl.AddDocument(docsIds[i], docsData[i]));
            }

            // Query
            SearchResult noFilters = cl.Search(new Query("*"));

            Assert.Equal(3, noFilters.TotalResults);
            Assert.Equal("student:111", noFilters.Documents[0].Id);
            Assert.Equal("pupil:222", noFilters.Documents[1].Id);
            Assert.Equal("student:333", noFilters.Documents[2].Id);

            SearchResult asOriginal = cl.Search(new Query("@first:Jo*"));

            Assert.Equal(0, asOriginal.TotalResults);

            SearchResult asAttribute = cl.Search(new Query("@given:Jo*"));

            Assert.Equal(2, asAttribute.TotalResults);
            Assert.Equal("student:111", noFilters.Documents[0].Id);
            Assert.Equal("pupil:222", noFilters.Documents[1].Id);

            SearchResult nonAttribute = cl.Search(new Query("@last:Rod"));

            Assert.Equal(0, nonAttribute.TotalResults);
        }
        public void ReturnFieldNames()
        {
            var query = GetQuery();

            Assert.Null(query._returnFieldsNames);
            Assert.Same(query, query.ReturnFields(FieldName.Of("foo").As("bar"), FieldName.Of("foofoo")));
            Assert.Equal(2, query._returnFieldsNames.Length);
        }