1

我正在尝试创建和索引,以便可以搜索文档中的多个字段(这就是我想要做的)。我创建了这个测试类来说明我想要做什么。我的问题是,在索引中我拆分了Note字段,以便搜索中的任何单词Note。但是我注意到,如果我用第一个单词或字母进行搜索,在测试中将是“A”我没有得到任何命中,但如果我用“B”搜索得到命中,想不出为什么会这样。

public class Sandbox
{
    [Fact]
    public void Run_Forrest_Run()
    {
        var store = new EmbeddableDocumentStore
        {
            RunInMemory = true, 
            Conventions = { DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites }
        };

        store.Initialize();
        IndexCreation.CreateIndexes(typeof(SearchExpenseIndex).Assembly, store);

        using (store)
        {
            using (var session = store.OpenSession())
            {
                session.Store(new Expense
                {
                    Date = DateTime.Today,
                    Category = "Transport",
                    Amount = 1000,
                    Note = "A B C"
                });

                session.SaveChanges();
            }

            Thread.Sleep(1000);

            // var terms = new[] {"A"}; <-- THIS WILL FAIL
            var terms = new[] {"B"};

            using (var session = store.OpenSession())
            {
                var result = session.Query<SearchExpenseIndex.ReduceResult, SearchExpenseIndex>()
                                .Where(x => x.Query.In(terms))
                                .As<Expense>()
                                .Select(x => new 
                                {
                                    Id = x.Id,
                                    Category = x.Category,
                                    Date = x.Date,
                                    Amount = x.Amount,
                                    Note = x.Note
                                }).ToList();


                Assert.Equal(1, result.Count);

            }
        }
    }

    public class Expense
    {
        public string Id { get; set; }
        public DateTime Date { get; set; }
        public string Category { get; set; }
        public decimal Amount { get; set; }
        public string Note { get; set; }
    }

    public class SearchExpenseIndex : AbstractIndexCreationTask<Expense, SearchExpenseIndex.ReduceResult>
    {
        public class ReduceResult
    {
        public string Query { get; set; }
        public string UserId { get; set; }
    }

        public SearchExpenseIndex()
    {
        Map = documents => from d in documents
            select new
            {
                Query = new object[]
                {
                    d.Category,
                    d.Note,
                    d.Note.Split(' '),
                    d.Date.ToString("yyyy-MM-dd")
                }
            };
        }
    }
}

我在这里想念什么?

4

1 回答 1

0

Pelle,字符“A”是英语中的停用词。因此,您无法真正搜索它,因为几乎任何搜索词都会在那里找到它。

于 2012-06-10T11:14:46.587 回答