protected virtual AbstractSolrQuery VisitWithinRadius(WithinRadiusNode radiusNode, SolrQueryMapper.SolrQueryMapperState state)
 {
     var orignialQuery = this.Visit(radiusNode.SourceNode, state);
     var spatialQuery = new SolrQuery(string.Format("{{!geofilt pt={0},{1} sfield={2} d={3} score=distance}}", radiusNode.Lat, radiusNode.Lon, radiusNode.Field, (int)radiusNode.Radius));
     var combinedQuery = orignialQuery && spatialQuery;
     return combinedQuery;
 }
        public TrackData ReadTrackByISRC(string isrc)
        {
            var query = new SolrQuery(string.Format("isrc:{0}", isrc));
            var results = this.solrForTracksCore.Query(query);

            return FirstFromResultSet(results);
        }
        public TrackData ReadTrack(IModelReference trackReference)
        {
            var trackId = SolrModelReference.GetId(trackReference);
            var query = new SolrQuery(string.Format("trackId:{0}", trackId));

            var results = this.solrForTracksCore.Query(query);
            return FirstFromResultSet(results);
        }
        // GET api/solrsearch/category/?q=turkey&lowPrice=50&highPrice=*
        public SearchResult Get(string q, string lowPrice = null, string highPrice = null, string categoryFilter = null)
        {
            if (q == "michael")
                throw new ArgumentException("billie jean is not my lover", "q");

            var query = new SolrQuery(SolrQuerySanitizer.Sanitize(q));
            var queryOptions = CreateQueryOptions(categoryFilter, lowPrice, highPrice);

            var solrResult = _solrOperations.Query(query, queryOptions);

            return CreateSearchResultFromSolrResult(solrResult);
        }
        public ActionResult Search(String id)
        {
            //var query = new SolrQueryByField("text", id);
            var query = new SolrQuery("text:" + id);

            var options = new QueryOptions()
            {
                //I don't need all the fields returned
                Fields = new[] { "id", "title", "source", "score" },
                //enable hit highlighting
                Highlight = new HighlightingParameters()
                {
                    Fields = new[] { "articleBody", "abstract" }
                    ,
                    Fragsize = 200
                    ,
                    AfterTerm = "</em></strong>"
                    ,
                    BeforeTerm = "<em><strong>"
                    ,
                    UsePhraseHighlighter = true
                    //, AlternateField = "source"
                }
            };

            //issue the query
            var results = _solr.Query(query, options);
            var highlights = results.Highlights;

            var resultCount = results.Highlights.Count;
            var searchResults = new List<QuoteHighlight>();
            for (int i = 0; i < resultCount; i++)
            {
                //get the basic document information before dealing with highlights
                var highlight = new QuoteHighlight()
                {
                    Id = results[i].Id,
                    Title = results[i].Title,
                    Source = results[i].Source,
                    Score = results[i].Score.Value
                };

                //highlights are a separate array, and can be an array of hits...
                foreach (var h in highlights[results[i].Id])
                {
                    highlight.ArticleBodySnippet += String.Join(",", h.Value.ToArray());
                }
                searchResults.Add(highlight);
            }
            ViewBag.Term = id;
            return View(searchResults);
        }
예제 #6
0
        public SearchResults DoSearch(string sField, string sQueryTerm, string sExcludeTerm, Dictionary<string, string> FacetFilterCollection, int ResultsPerPage, int PageNumber, string sStartDate, string sEndDate, string Boost, string sAcctId)
        {
            int nAcctId;
            string sIncludeQuery = "", sExcludeQuery = "";

            new SolrBaseRepository.Instance<Contact>().Start();
            var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Contact>>();

            sStartDate = sStartDate == "" ? "*" : DateTime.Parse(sStartDate).ToString("yyyy-MM-ddTHH:mm:ssZ");
            sEndDate = sEndDate == "" ? "*" : DateTime.Parse(sEndDate).ToString("yyyy-MM-ddTHH:mm:ssZ");
            sQueryTerm = sQueryTerm == "" ? "*" : sQueryTerm;
            int nBoostfactor = 1;
            Int32.TryParse(Boost == "" ? "1" : Boost, out nBoostfactor);

            #region Query Parameters
            HighlightingParameters HighlightParams = new HighlightingParameters
            {
                Fields = new[] {    "cont_first_name_phonetic",
                                    "cont_first_name",
                                    "cont_last_name",
                                    "email_addr",
                                    "cont_source_id",
                                    "cont_note_text",
                                    "cont_attachment_text"
                                },
                BeforeTerm = "<span style='background-color:Yellow'>",
                AfterTerm = "</span>"
            };

            SpellCheckingParameters SpellCheckParams = new SpellCheckingParameters
            {
                OnlyMorePopular = true,
                Count = 5,
                Collate = true,
                Query = sQueryTerm
            };

            FacetParameters FacetParams = new FacetParameters
            {
                MinCount = 1,
                Sort = true,
                Queries = new List<ISolrFacetQuery>
                {                   new SolrFacetFieldQuery("gender_name"),
                                    new SolrFacetFieldQuery("home_state_name"),
                                    new SolrFacetFieldQuery("home_city"),
                                    new SolrFacetFieldQuery("cont_type_name_facet")
                                    //new SolrFacetQuery(new SolrQueryByRange<string>("cont_first_name", "A", "G")),
                                    //new SolrFacetQuery(new SolrQueryByRange<string>("cont_first_name", "H", "P")),
                                    //new SolrFacetQuery(new SolrQueryByRange<string>("cont_first_name", "Q", "Z"))
                }
            };

            List<ISolrQuery> FilterQuery = new List<ISolrQuery>() { };
            if (FacetFilterCollection != null && FacetFilterCollection.Count > 0)
            {
                foreach (KeyValuePair<string, string> kp in FacetFilterCollection)
                    FilterQuery.Add(new SolrQueryByField(kp.Key, kp.Value));
            }

            #endregion

            var QueryOptions = new QueryOptions
            {
                Rows = ResultsPerPage,
                Start = (PageNumber - 1) * ResultsPerPage,
                Fields = new[] { "*", "score" },
                OrderBy = new[] { new SortOrder("score", Order.DESC), new SortOrder("cont_first_name", Order.DESC) },
                FilterQueries = FilterQuery,
                Highlight = HighlightParams,
                SpellCheck = SpellCheckParams,
                Facet = FacetParams,
            };

            switch (sField)
            {
                case "cont_first_name":
                    sIncludeQuery = sField + ":" + sQueryTerm + "^2 OR " +
                            "cont_first_name_phonetic" + ":" + sQueryTerm;
                    if (sExcludeTerm != "")
                        sExcludeQuery = sField + ":" + sExcludeTerm;

                    break;

                case "*":

                    sIncludeQuery = "cont_first_name_phonetic:" + sQueryTerm + " OR " +
                                    "cont_first_name:" + sQueryTerm + "^2 OR " +
                             "cont_last_name:" + sQueryTerm + " OR " +
                             "email_addr:" + sQueryTerm + " OR " +
                             "cont_source_id:" + sQueryTerm + " OR " +
                             "cont_type_name:" + sQueryTerm + " OR " +
                             "cont_attachment_text:" + sQueryTerm + " OR " +
                             "cont_note_text:" + sQueryTerm;

                    if (sExcludeTerm != "")
                        sExcludeQuery = "cont_first_name_phonetic:" + sExcludeTerm + " OR " +
                               "cont_last_name:" + sExcludeTerm + " OR " +
                               "email_addr:" + sExcludeTerm + " OR " +
                               "cont_source_id:" + sExcludeTerm + " OR " +
                               "cont_type_name:" + sExcludeTerm + " OR " +
                               "cont_note_text:" + sExcludeTerm;

                    break;

                case "registration_date":

                    sIncludeQuery = sField + ":" + sQueryTerm + " AND registration_date:[" + sStartDate + " TO " + sEndDate + "]";

                    if (sExcludeTerm != "")
                        sExcludeQuery = "cont_first_name_phonetic:" + sExcludeTerm + " OR " +
                               "cont_last_name:" + sExcludeTerm + " OR " +
                               "email_addr:" + sExcludeTerm + " OR " +
                               "cont_source_id:" + sExcludeTerm + " OR " +
                               "cont_type_name:" + sExcludeTerm + " OR " +
                               "cont_note_text:" + sExcludeTerm;

                    break;

                case "~": // Raw Query Syntax - Don't process anything
                    sIncludeQuery = sQueryTerm;
                    break;

                default:
                    sIncludeQuery = sField + ":" + sQueryTerm;
                    if (sExcludeTerm != "")
                        sExcludeQuery = sField + ":" + sExcludeTerm;
                    break;
            }

            //// Enforce AccountId for Multitenancy
            //if (Int32.TryParse(sAcctId, out nAcctId))
            //    sIncludeQuery += " AND acct_id:" + nAcctId.ToString();

            SolrQuery ContactQuery = new SolrQuery(sIncludeQuery + (sExcludeQuery != "" ? " -(" + sExcludeQuery + ")" : ""));

            AbstractSolrQuery SolrQueryAb = ContactQuery;
            if (Int32.TryParse(sAcctId, out nAcctId))
                SolrQueryAb = ContactQuery && new SolrQuery("acct_id:" + nAcctId.ToString());

            var results = solr.Query(SolrQueryAb, QueryOptions);

            int idx = (QueryOptions.Start + 1) ?? 1;
            // Encapuslate the result and send object to caller
            var SearchResultSet = new SearchResults
            {
                ResultSet = results.Select((p) =>
                {
                    return new Contact
                    {
                        SerialNo = idx++,
                        FirstName = (sField.Contains("cont_first_name") && sQueryTerm != "*" ? results.Highlights[p.ContStub].ContainsKey("cont_first_name_phonetic") ? results.Highlights[p.ContStub]["cont_first_name_phonetic"].ToList()[0] : results.Highlights[p.ContStub]["cont_first_name"].ToList()[0] : p.FirstName) ?? "",
                        LastName = (sField == "cont_last_name" && sQueryTerm != "*" ? results.Highlights[p.ContStub][sField].ToList()[0] : p.LastName) ?? "",
                        EmailAddr = (sField == "email_addr" && sQueryTerm != "*" ? results.Highlights[p.ContStub][sField].ToList()[0] : p.EmailAddr) ?? "",
                        SourceId = (sField == "cont_source_id" && sQueryTerm != "*" ? results.Highlights[p.ContStub][sField].ToList()[0] : p.SourceId) ?? "",
                        NoteText = ((sField == "cont_note_text" || sField == "cont_attachment_text") && sQueryTerm != "*" ? results.Highlights[p.ContStub][sField].ToList()[0] : "") ?? "",
                        ResultScore = p.ResultScore,
                        AccountId = p.AccountId
                    };
                }).ToList(),
                QueryTime = results.Header.QTime,
                TotalHits = results.NumFound,
                QuerySuggestions = results.SpellChecking.SelectMany(p => p.Suggestions.ToList()),
                RawSolrQuery = ContactQuery.Query
            };

            if (results.FacetFields.Count > 0)
            {
                foreach (var facet in results.FacetFields["gender_name"])
                    SearchResultSet.GenderFacets.Add(facet.Key == "" ? "Blank" : facet.Key, facet.Value);

                foreach (var facet in results.FacetFields["home_state_name"])
                    SearchResultSet.StateFacets.Add(facet.Key == "" ? "Blank" : facet.Key, facet.Value);

                foreach (var facet in results.FacetFields["home_city"])
                    SearchResultSet.CityFacets.Add(facet.Key == "" ? "Blank" : facet.Key, facet.Value);

                foreach (var facet in results.FacetFields["cont_type_name_facet"])
                    SearchResultSet.ContTypeFacets.Add(facet.Key == "" ? "Blank" : facet.Key, facet.Value);
            }

            //// Arbitrary Facet Queries like Name Ranges and so on ..
            //if (results.FacetQueries.Count > 0)
            //{
            //    foreach (var arbitraryFacet in results.FacetQueries)
            //        SearchResultSet.ArbitraryFacets.Add(arbitraryFacet.Key, arbitraryFacet.Value);
            //}

            return SearchResultSet;
        }
 public IList<TrackData> ReadAll()
 {
     var query = new SolrQuery("*:*");
     var results = this.solrForTracksCore.Query(query);
     return AllFromResultSet(results);
 }
 public IList<TrackData> ReadTrackByArtistAndTitleName(string artist, string title)
 {
     var query = new SolrQuery(string.Format("title:{0} AND artist:{1}", title, artist));
     var results = this.solrForTracksCore.Query(query);
     return AllFromResultSet(results);
 }
        public ISolrQueryResults<BaseballGame> ExecuteQuery()
        {
            List<ISolrQuery> fieldQueries = new List<ISolrQuery>();
            QueryOptions options = new QueryOptions();
            options.Facet = BuildBaseFacetQuery();
            options.Rows = 200;

            if (this.AppliedFacets.Count > 0)
            {
                var facetGroups = this.AppliedFacets.Select(t => t.Item1).Distinct().ToList();

                foreach(var group in facetGroups)
                {
                    List<ISolrQuery> queries = this.AppliedFacets.Where(fg => fg.Item1 == group).Select(q => new SolrQueryByField(q.Item1, q.Item2) as ISolrQuery).ToList();
                    SolrMultipleCriteriaQuery smcq =
                        new SolrMultipleCriteriaQuery(queries,"OR");
                    fieldQueries.Add(smcq);
                }

                ISolrQuery multipleCriteriaQuery = new SolrMultipleCriteriaQuery(fieldQueries, "AND");
                options.AddFilterQueries(multipleCriteriaQuery);
            }
            options.OrderBy.Add(new SortOrder(this.CurrentSortTerm, this.SortDirection == "D" ? Order.DESC : Order.ASC));
            options.AddFilterQueries(new SolrQueryByField("docType", DocType.BaseballGame.ToString()));
            ISolrQuery finalQuery = SolrQuery.All;

            if (!string.IsNullOrEmpty(this.SearchTerm))
            {
                finalQuery = new SolrQuery(this.SearchTerm);
            }

            return _solrOperations.Query(finalQuery, options);
        }
예제 #10
0
파일: SolrQuery.cs 프로젝트: vblain/SolrNet
 static SolrQuery()
 {
     All = new SolrQuery("*:*");
 }