Beispiel #1
0
        protected List <Book> Search(Query query, int take, int skip, out int count)
        {
            Query = query.ToString();
            watch = Stopwatch.StartNew();
            var fields = new SortField[]
            {
                new SortField("title_sort", SortField.STRING, false),
                new SortField("pubdate", SortField.STRING, true),
                SortField.FIELD_SCORE
            };
            var sort = new Sort(fields);

            using (var directory = new SimpleFSDirectory(new DirectoryInfo(Settings.Instance.DatabaseIndex)))
                using (var searcher = new IndexSearcher(directory))
                {
                    var docs = searcher.Search(query, null, skip + take, sort);
                    count = docs.TotalHits;

                    var books = new List <Book>();
                    for (int i = skip; i < docs.TotalHits; i++)
                    {
                        if (i > (skip + take) - 1)
                        {
                            break;
                        }

                        var doc     = searcher.Doc(docs.ScoreDocs[i].Doc);
                        var authors = doc.GetFields("author_fullname")
                                      .Select(x => x.StringValue.Split(','))
                                      .Select(x => new Author {
                            FirstName = x[0], MiddleName = x[1], LastName = x[2]
                        });
                        var genres = doc.GetFields("genre")
                                     .Select(x => x.StringValue)
                                     .Select(x => GenreExtensions.Construct(x));

                        Cover cover            = null;
                        var   coverContentType = doc.Get("_cover_type");
                        if (coverContentType != null)
                        {
                            cover = new Cover
                            {
                                Data        = doc.GetBinaryValue("_cover_data"),
                                ContentType = coverContentType
                            };
                        }

                        var meta      = new List <MetaField>();
                        var docFields = doc.GetFields();
                        foreach (var f in docFields)
                        {
                            if (!KNOWN_FIELDS.Contains(f.Name, StringComparer.OrdinalIgnoreCase))
                            {
                                meta.Add(new MetaField {
                                    Name = f.Name, Value = f.StringValue
                                });
                            }
                        }

                        var book = new Book
                        {
                            Id              = Guid.Parse(doc.Get("guid")),
                            LibraryId       = Guid.Parse(doc.Get("_library_id")),
                            UpdatedFromFile = bool.Parse(doc.Get("_updated_from_file")),
                            UpdatedAt       = DateTools.StringToDate(doc.Get("_updated_at")),
                            Title           = doc.Get("title"),
                            Series          = doc.Get("series"),
                            SeriesNo        = int.Parse(doc.Get("seriesno")),
                            File            = doc.Get("file"),
                            Ext             = doc.Get("ext"),
                            Date            = DateTime.Parse(doc.Get("pubdate")),
                            Archive         = doc.Get("archive"),
                            Authors         = authors,
                            Genres          = genres,
                            Annotation      = doc.Get("annotation"),
                            Language        = doc.Get("language"),
                            Cover           = cover,
                            Meta            = meta
                        };
                        books.Add(book);
                    }

                    watch.Stop();

                    return(books);
                }
        }