Beispiel #1
0
        public static List <SolrDoc> search(Guid applicationId, string phrase, List <SearchDocType> docTypes, List <Guid> typeIds,
                                            List <string> types, bool additionalId, bool title, bool description, bool tags, bool content, bool fileContent,
                                            bool forceHasContent, int count, int lowerBoundary, bool highlight, ref int totalCount)
        {
            ISolrOperations <SolrDoc> solr = get_solr_operator();

            QueryTerms searchTerms = new QueryTerms(phrase);

            List <KeyValuePair <string, double> > fieldBoosts = new List <KeyValuePair <string, double> >();

            docTypes = (docTypes == null ? new List <SearchDocType>() : docTypes.Where(d => d != SearchDocType.All)).Distinct().ToList();

            if (title)
            {
                fieldBoosts.Add(new KeyValuePair <string, double>("title", 5));
            }
            if (tags)
            {
                fieldBoosts.Add(new KeyValuePair <string, double>("tags", 4));
            }
            if (description)
            {
                fieldBoosts.Add(new KeyValuePair <string, double>("description", 3));
            }
            if (content)
            {
                fieldBoosts.Add(new KeyValuePair <string, double>("content", 2));
            }
            if (fileContent)
            {
                fieldBoosts.Add(new KeyValuePair <string, double>("file_content", 1));
            }
            if (additionalId)
            {
                fieldBoosts.Add(new KeyValuePair <string, double>("additional_id", 0));
            }

            string query = searchTerms.get_query(fieldBoosts, docTypes, typeIds, types, forceHasContent);

            QueryOptions queryOptions = new QueryOptions()
            {
                Rows          = count + (count / 2),
                StartOrCursor = new StartOrCursor.Start(Math.Max(0, lowerBoundary)),
                ExtraParams   = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("_route_", applicationId.ToString() + "!")
                },
                Fields = new[] { "id", "search_doc_type", "type_id", "type", "additional_id", "title", "no_content", "deleted" }
            };

            if (highlight)
            {
                queryOptions.Highlight = new HighlightingParameters()
                {
                    Fields = fieldBoosts.Where(b => b.Key != "additional_id").Select(b => b.Key).ToArray()
                }
            }
            ;

            SolrQueryResults <SolrDoc> results = solr.Query(query, queryOptions);

            totalCount = results.NumFound;

            if (highlight)
            {
                results.Where(d => results.Highlights.ContainsKey(d.ID)).ToList().ForEach(doc => {
                    HighlightedSnippets snippets = results.Highlights[doc.ID];
                    doc.Description = string.Join(" ", snippets.Values.Select(v => string.Join(" ", v)))
                                      .Replace("<em>", "<b>").Replace("</em>", "</b>");
                });
            }

            return(results.ToList());
        }
Beispiel #2
0
 public QueryTerms(string phrase)
 {
     Terms = QueryTerms.parse(phrase);
 }
Beispiel #3
0
        private static List <string> parse(string phrase)
        {
            List <string> terms = new List <string>();

            if (string.IsNullOrEmpty(phrase))
            {
                return(terms);
            }

            phrase = " " + PublicMethods.verify_string(phrase) + " ";

            string pattern = "\\s[\\+\\-]?\"[^\"]*\"\\s";

            Dictionary <string, string> quotesDic = new Dictionary <string, string>();

            MatchCollection matches = null;

            while (true)
            {
                matches = string.IsNullOrEmpty(phrase) ? (MatchCollection)null : (new Regex(pattern)).Matches(phrase);

                if (matches == null || matches.Count == 0)
                {
                    break;
                }

                for (int i = 0; i < matches.Count; i++)
                {
                    string key = PublicMethods.random_string(10);
                    quotesDic[key] = matches[i].Value;
                    phrase         = phrase.Replace(matches[i].Value.Trim(), " " + key + " ");
                }
            }

            phrase.Split(' ')
            .Select(x => x.Trim())
            .Where(x => !string.IsNullOrEmpty(x))
            .Select(x => quotesDic.ContainsKey(x) ? quotesDic[x].Trim() : x)
            .ToList()
            .ForEach(x =>
            {
                string prefix = new[] { "+", "-" }.Where(c => x.StartsWith(c)).FirstOrDefault();

                if (!string.IsNullOrEmpty(prefix))
                {
                    x = x.Length > 1 ? x.Substring(1) : string.Empty;
                }

                if (string.IsNullOrEmpty(x))
                {
                    return;
                }

                bool isQuote = x[0] == '"' && x[x.Length - 1] == '"';

                if (isQuote)
                {
                    if (x.Length <= 2)
                    {
                        return;
                    }
                    x = x.Substring(1, x.Length - 2).Trim();
                }

                new[] {
                    "+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", ":", "/",
                    "\\", ".", ",", "،", "؛", "؟"
                }.ToList().ForEach(c => x = x.Replace(c, " "));

                x = x.Trim();
                if (string.IsNullOrEmpty(x))
                {
                    return;
                }

                isQuote = isQuote && x.IndexOf(" ") > 0;
                if (isQuote)
                {
                    x = "\"" + x + "\"";
                }

                if (!string.IsNullOrEmpty(prefix))
                {
                    x = prefix + x;
                }

                if (!isQuote && x.IndexOf(" ") > 0)
                {
                    terms.AddRange(QueryTerms.parse(x));
                }
                else
                {
                    if (!isQuote && !(new[] { "?", "*" }).Any(c => x.IndexOf(c) >= 0))
                    {
                        x += "~1";
                    }
                    else if (isQuote)
                    {
                        x += "^";
                    }

                    terms.Add(x);
                }
            });

            return(terms.Distinct().Where(t => !string.IsNullOrEmpty(t)).ToList());
        }