private static BooleanQuery GetCombinedTermAndSplitQuery(string prefix, IEnumerable<Query> upperSplitQuery, Range currentPart)
 {
     var upperRangeQuery = new BooleanQuery();
     upperRangeQuery.AddTermQuery(currentPart.Name(prefix), currentPart.Value(currentPart.End));
     upperRangeQuery.AddClause(upperSplitQuery.First());
     return upperRangeQuery;
 }
        private static IEnumerable<Query> GetQuery(IEnumerable<Range> parts, string prefix)
        {
            var currentPart = parts.First();
            var remainingParts = parts.Skip(1);

            if (currentPart.IsRange)
            {
                var splitQueries = Split(parts, prefix);
                if (splitQueries.Any())
                {
                    var query = new BooleanQuery();
                    query.AddClause(splitQueries.First());
                    yield return query;
                }
            }
            else
            {
                var query = new BooleanQuery();
                query.AddTermQuery(currentPart.Name(prefix), currentPart.Value(currentPart.Start));

                if (remainingParts.Any())
                {
                    foreach (var childQuery in GetQuery(remainingParts, prefix))
                    {
                        query.AddClause(childQuery);
                    }
                }

                yield return query;
            }
        }
        private static IEnumerable<Query> Split(IEnumerable<Range> parts, string prefix)
        {
            var currentPart = parts.First();
            var remainingParts = parts.Skip(1);

            if(remainingParts.Any())
            {
                var lowerEdgeQuery = GetLowerEdgeQuery(prefix, remainingParts);
                var upperEdgeQuery = GetUpperEdgeQuery(prefix, remainingParts);

                if (lowerEdgeQuery.Any() || upperEdgeQuery.Any() || !currentPart.IsFloorValue || !currentPart.IsCeilingValue)
                {
                    var query = new BooleanQuery();

                    var midRangeStart = currentPart.Value(currentPart.Start);
                    var midRangeEnd = currentPart.Value(currentPart.End);

                    if (lowerEdgeQuery.Any())
                    {
                        midRangeStart = currentPart.Value(currentPart.Start + 1);
                        query.AddClause(GetCombinedTermAndSplitQuery(prefix, lowerEdgeQuery, currentPart), BooleanClause.Occur.SHOULD);
                    }

                    if (upperEdgeQuery.Any())
                    {
                        midRangeEnd = currentPart.Value(currentPart.End - 1);
                        query.AddClause(GetCombinedTermAndSplitQuery(prefix, upperEdgeQuery, currentPart), BooleanClause.Occur.SHOULD);
                    }

                    if (currentPart.End - currentPart.Start > 1)
                    {
                        query.AddRangeQuery(currentPart.Name(prefix), midRangeStart, midRangeEnd, BooleanClause.Occur.SHOULD);
                    }

                    yield return query;
                }
            }
            else
            {
                if (!currentPart.IsFloorValue || !currentPart.IsCeilingValue)
                {
                    yield return
                        new RangeQuery(
                            new Term(currentPart.Name(prefix), currentPart.Value(currentPart.Start)),
                            new Term(currentPart.Name(prefix), currentPart.Value(currentPart.End)),
                            true);
                }
            }
        }