예제 #1
0
 public AddressPartReduction GetReduction(ParsedAddressPart part)
 {
     if (_reduction == null)
     {
         _reduction = new ReductionRamStorage(_indexDirectory);
     }
     return _reduction.Get(part.Level, part.Reduction);
 }
예제 #2
0
        public ParsedAddress GetByCode(string code)
        {
            int? level = _Base.GetLevel(code);
            if (!level.HasValue)
            {
                _log.Warn(ClassName + String.Format("Для кода: {0} не найден уровень в КЛАДР", code));
                return null;
            }

            var queryString = new StringBuilder();
            queryString.Append(code);
            for (; level > 0; level--)
            {
                string parentCode = AddressParser.GetParentCode(level.Value, code);
                if (!code.Equals(parentCode, StringComparison.CurrentCultureIgnoreCase)) queryString.Append(" " + parentCode);
                code = parentCode;
            }

            var parser = new QueryParser(Version.LUCENE_30, "id", _analyzer);
            Query query = parser.Parse(queryString.ToString());

            var searcher = GetSearcher();
            if (searcher == null) return null;
            TopDocs hits = searcher.Search(query, int.MaxValue);

            ParsedAddress d = new ParsedAddress();

            var totalHits = hits.TotalHits;
            var list = new List<ParsedAddressPart>();
            int minLvl = _Base.NoLevel;
            for (int i = 0; i < totalHits; i++)
            {
                Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);

                ParsedAddressPart part = new ParsedAddressPart();
                part.Id = doc.Get("id");
                string postalCode = doc.Get("postalCode");

                part.Level = int.Parse(doc.Get("lvl"));
                part.Reduction = doc.Get("reduction");
                part.Name = doc.Get("name");
                part.NameWithShortReduction = doc.Get("name_short_reduction");
                part.NameWithReduction = doc.Get("name_reduction");

                list.Add(part);

                if (!string.IsNullOrEmpty(postalCode) && minLvl < part.Level)
                {
                    d.PostalCode = postalCode;
                    minLvl = part.Level;
                }
            }

            d.AddressPart = list.OrderBy(p => p.Level).ToList();
            return d;
        }
예제 #3
0
        public IEnumerable<ParsedAddressPart> GetChildren(string code)
        {
            int? level = _Base.GetLevel(code);
            if (!level.HasValue)
            {
                _log.Warn(ClassName + String.Format("Для кода: {0} не найден уровень в КЛАДР", code));
                yield break;
            }

            while (true)
            {
                string queryString = GetQueryChildren(level.Value, code);
                if (string.IsNullOrEmpty(queryString)) yield break;
                level++; // необходимый уровень

                var parser = new QueryParser(Version.LUCENE_30, "id", _analyzer);
                Query query = parser.Parse(queryString);

                var searcher = GetSearcher();
                if (searcher == null) yield break;
                TopDocs hits = searcher.Search(query, int.MaxValue);

                int no = 0;
                var totalHits = hits.TotalHits;

                if (totalHits < 2) continue;

                var lastInfo = new ParsedAddressPartInfo();
                while (no < totalHits)
                {
                    lastInfo.No = no;
                    lastInfo.TotalHits = totalHits;

                    Document doc = searcher.Doc(hits.ScoreDocs[no].Doc);
                    int lvl = int.Parse(doc.Get("lvl"));
                    if (lvl == level)
                    {
                        ParsedAddressPart part = new ParsedAddressPart();
                        part.LastInfo = lastInfo;

                        part.Id = doc.Get("id");

                        part.Level = lvl;
                        part.Reduction = doc.Get("reduction");
                        part.Name = doc.Get("name");
                        part.NameWithShortReduction = doc.Get("name_short_reduction");
                        part.NameWithReduction = doc.Get("name_reduction");

                        yield return part;
                    }
                    no++;
                }

                yield break;
            }
        }
예제 #4
0
 public AddressPartReduction GetReduction(ParsedAddressPart part)
 {
     return _source.GetReduction(part);
 }