Exemple #1
0
        public async Task <ApiResponse <string> > ReindexVanDyke()
        {
            var directoryName = @"..\App_Data\Bibles\VanDyke\";// @"C:\Suzette\TheArabicBible\The Arabic Bible\BibleCopies\VanDyke\";
            var books         = System.IO.Directory.GetFiles(directoryName);

            foreach (var book in books)
            {
                var bookText = string.Empty;
                using (var reader = new System.IO.StreamReader(book))
                {
                    bookText = reader.ReadToEnd();
                }
                if (!string.IsNullOrEmpty(bookText))
                {
                    var bookId  = Convert.ToInt16(book.Replace(directoryName, "").Substring(0, 2));
                    var groupId = GetGroupId(bookId);

                    bookText = bookText.Replace("الأصحَاحُ", "@");
                    bookText = System.Text.RegularExpressions.Regex.Replace(bookText, "[0-9]", "$"); //replace verse number/digit with $
                    bookText = bookText.Replace("$$$", "$").Replace("$$", "$");                      //if verse number has more than digit replace them all with one $

                    var chapters       = bookText.Split('@');
                    var chapterCounter = 0;
                    foreach (var chapter in chapters)
                    {
                        var verses       = chapter.Split('$');
                        var verseCounter = 0;
                        for (var i = 1; i < verses.Count(); i++)
                        {
                            var verse = verses[i];

                            var trimedVerse = verse.Replace("\n", "").Replace("\r", "").Trim();
                            var doc         = new BibleVerse()
                            {
                                BibleId = 1
                                ,
                                TestmentId = (bookId <= 39 ? 1 : 2)
                                ,
                                GroupId = groupId
                                ,
                                BookId = bookId
                                ,
                                ChapterId = chapterCounter
                                ,
                                VerseNo = i
                                ,
                                VerseText = trimedVerse
                                ,
                                SearchText = ArabicHelper.MapArabicToEnglishLetters(trimedVerse)
                            };
                            if (!string.IsNullOrEmpty(trimedVerse))
                            {
                                chapterCounter = chapterCounter == 0 ? 1 : chapterCounter;
                                verseCounter   = verseCounter == 0 ? 1 : verseCounter;
                                // return new ApiResponse<string>() { Data = JsonConvert.SerializeObject(doc) };
                                var response = await _bibleRepository.AddDocument(doc);
                            }
                            verseCounter++;
                        }
                        chapterCounter++;
                    }
                }
            }
            return(new ApiResponse <string>());//todo set status to false
        }
Exemple #2
0
        public async Task <ApiResponse <List <BibleVerse> > > Query(int pageIndex, List <int> bibleIds, Bible bibleFilter, SearchCriteria searchCriteria)
        {
            var response    = new ApiResponse <List <BibleVerse> >();
            var currentPage = pageIndex;
            var recPerPage  = 20;
            var startIndex  = recPerPage * (currentPage - 1);
            var searchTerm  = searchCriteria.SearchItems[0].SearchTerm;

            if (bibleFilter.Language.Equals("arabic", StringComparison.OrdinalIgnoreCase))
            {
                searchTerm = ArabicHelper.MapArabicToEnglishLetters(searchCriteria.SearchItems[0].SearchTerm);
            }

            var filterItems = GetFilter(bibleIds, bibleFilter);

            var searchStart = $" ( ";

            var boolQueryStart  = $" 'query' : ( 'bool' : (";
            var queryMustClause = $"   'must'     : [ ('wildcard' : ('SearchText' :  '*{searchTerm}*' )) ]";       // must works like and
            var boolQueryEnd    = $" )) ";

            var boolFilterStart     = $" , 'filter'   : ( 'bool' : (";
            var filterShouldClause  = $"   'should'   : [] ";                      //should works like or
            var filterMustClause    = $" , 'must'     : [ {filterItems.Item1} ] "; //must works like and
            var filterMustNotClause = $" , 'must_not' : [ {filterItems.Item2} ]";  //must_not works like and not
            var boolFilterEnd       = $" )) ";

            var sort = $" , 'sort' : ['BookId' , 'ChapterId' , 'VerseNo' ]";
            var from = $" , 'from' : {startIndex} ";
            var size = $" , 'size' : {recPerPage} ";

            var searchEnd = $" ) ";

            var boolQuery  = $" {boolQueryStart}  {queryMustClause} {boolQueryEnd} ";
            var boolFilter = $" {boolFilterStart} {filterShouldClause} {filterMustClause} {filterMustNotClause} {boolFilterEnd} ";

            var query = $"{searchStart} {boolQuery} {boolFilter} {sort} {from} {size} {searchEnd} ";

            query = query.Replace("(", "{").Replace(")", "}").Replace("'", "\"");

            var endpoint = new EndPoint();

            endpoint.ApiMethod  = _readIndex + "_search";
            endpoint.HttpMethod = "POST";

            var esResponse = await _requestManager.ExecuteRequest <ElasticSearchResponse <ElasticSearchBibleDocument>, string>(endpoint, query);

            if (esResponse.Status.Ok)
            {
                response.Pagination = new ApiPagination
                {
                    RecPerPage     = recPerPage,
                    CurrentPage    = currentPage,
                    TotalRecCount  = esResponse.Data.hits.total,
                    TotalPageCount = Convert.ToInt16(Math.Ceiling(Convert.ToDecimal(esResponse.Data.hits.total) / recPerPage))
                };

                response.Data = new List <BibleVerse>();
                foreach (var hit in esResponse.Data.hits.hits)
                {
                    var verse = new BibleVerse()
                    {
                        BibleId = Convert.ToInt16(hit._source.BibleId)
                        ,
                        BookId = Convert.ToInt16(hit._source.BookId)
                        ,
                        ChapterId = Convert.ToInt16(hit._source.ChapterId)
                        ,
                        VerseNo = Convert.ToInt16(hit._source.VerseNo)
                        ,
                        VerseText = hit._source.VerseText
                    };
                    response.Data.Add(verse);
                }
            }
            else
            {
                response.Status = esResponse.Status;
            }
            return(response);
        }