Question

I am trying to write SolrNet query for following block:

q={!boost b=sum(contentscore,1) defType=edismax}te

How we write SolrNet based Linq or regular C# code for above Solr query ?

Solr server version is 3.6.0

Full Solr query is: http://192.192.168.12:8080/solr/select/?rows=10&q={!boost b=sum(contentscore,1) defType=edismax}testingsearchterm&hl=true&group=true&group.ngroups=true&group.truncate=true&group.field=collapse&fq=-type:(file) roles:(1)&facet=on&facet.field=category&f.category.facet.sort=count&f.category.facet.limit=12&f.category.facet.offset=0&f.category.facet.mincount=0&facet.field=user&f.user.facet.sort=count&f.user.facet.limit=12&f.user.facet.offset=0&f.user.facet.mincount=1&facet.field=group&f.group.facet.sort=count&f.group.facet.limit=12&f.group.facet.offset=0&f.group.facet.mincount=1&facet.field=tag&f.tag.facet.sort=count&f.tag.facet.limit=12&f.tag.facet.offset=0&f.tag.facet.mincount=1&facet.query=date:[2013-11-06T02:33:00Z TO 2013-11-13T02:33:00Z]&facet.query=date:[2013-10-13T01:33:00Z TO 2013-11-13T02:33:00Z]&facet.query=date:[2012-11-13T02:33:00Z TO 2013-11-13T02:33:00Z]

I have following code written for basic querying which is going to change as per above full query:

var matchingRecords = solr.Query(BuildQuery(parameters), new QueryOptions
{
    FilterQueries = BuildFilterQueries(parameters),
    Rows = parameters.PageSize,
    Start = start,
    OrderBy = GetSelectedSort(parameters),
    SpellCheck = new SpellCheckingParameters(),
    Facet = new FacetParameters
    {
        Queries = AllFacetFields.Except(SelectedFacetFields(parameters))
                                .Select(f => new SolrFacetFieldQuery(f) { MinCount = 1 })
                                .Cast<ISolrFacetQuery>()
                                .ToList(),
    },
});

Reference question: Solrnet - Boost query with function

Was it helpful?

Solution

I have written this Solr query as below:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SolrNet;
using SolrNet.Commands.Parameters;
using SolrNet.DSL;
using SolrNet.Exceptions;
using SolrNet.Impl;

public class Class1
{
    public void Method1()
    {
        var serviceProvider = ((IServiceProvider)Startup.Container);
        if (serviceProvider == null)
            return;

        var solrOperation = serviceProvider.GetService(typeof(ISolrReadOnlyOperations<MySampleItem>));
        if (solrOperation == null)
            return;

        ISolrReadOnlyOperations<MySampleItem> solr = (ISolrReadOnlyOperations<MySampleItem>)solrOperation;
        SearchParameters parameters = new SearchParameters();
        parameters.FreeSearch = txtSearch.Text;
        DateTime currentdate = DateTime.Now;
        var facetweek = new SolrQueryByRange<DateTime?>("date", new DateTime(currentdate.Year, currentdate.Month, currentdate.Day).AddDays(-7), new DateTime(currentdate.Year, currentdate.Month, currentdate.Day));
        var facetyear = new SolrQueryByRange<DateTime?>("date", new DateTime(currentdate.Year, currentdate.Month, currentdate.Day).AddYears(-1), new DateTime(currentdate.Year, currentdate.Month, currentdate.Day));
        var facetmonth = new SolrQueryByRange<DateTime?>("date", new DateTime(currentdate.Year, currentdate.Month, currentdate.Day).AddMonths(-1), new DateTime(currentdate.Year, currentdate.Month, currentdate.Day));
        var start = (parameters.PageIndex - 1) * parameters.PageSize;
        var matchingRecords = solr.Query(BuildQuery(parameters), new QueryOptions
        {

            ExtraParams = new Dictionary<string, string>
            {
                {"fq", "-type file)  roles 1)"},
                {"group.truncate","true"},
            },
            Highlight = new HighlightingParameters { },
            Grouping = new GroupingParameters()
            {
                Fields = new[] { "collapse" },
                Ngroups = true,
            },
            FilterQueries = BuildFilterQueries(parameters),
            Rows = parameters.PageSize,
            Facet = new FacetParameters
            {
                Queries = new ISolrFacetQuery[]
                {   
                    new SolrFacetFieldQuery("category") { MinCount = 0 ,Limit=12,Offset=0,Sort=true},
                    new SolrFacetFieldQuery("user") { MinCount = 1 ,Limit=12,Offset=0,Sort=true},
                    new SolrFacetFieldQuery("group") { MinCount = 1 ,Limit=12,Offset=0,Sort=true},
                    new SolrFacetFieldQuery("tag") { MinCount = 1 ,Limit=12,Offset=0,Sort=true},
                    new SolrFacetQuery(facetweek),
                    new SolrFacetQuery(facetyear),
                    new SolrFacetQuery(facetmonth),
                }
            },
        });
    }

    public ISolrQuery BuildQuery(SearchParameters parameters)
    {
        if (!string.IsNullOrEmpty(parameters.FreeSearch))
        {
            return new LocalParams { { "boost b", "sum(contentscore,1)" }, { "defType", "edismax" } } + new SolrQuery(parameters.FreeSearch);
            //return new SolrQuery(parameters.FreeSearch);
        }
        return SolrQuery.All;
    }

    public ICollection<ISolrQuery> BuildFilterQueries(SearchParameters parameters)
    {
        var queriesFromFacets = from p in parameters.Facets
                                select (ISolrQuery)Query.Field(p.Key).Is(p.Value);
        return queriesFromFacets.ToList();
    }
}

Class for mapping the Solr search result into object

public class MySampleItem
{
    [SolrUniqueKey("id")]
    public string Id { get; set; }

    [SolrField("title")]
    public string Title { get; set; }

    [SolrField("content")]
    public string Content { get; set; }

    [SolrField("url")]
    public string FriendlyUrl { get; set; }

    [SolrField("score")]
    public float Score { get; set; }

    [SolrField("date")]
    public DateTime PublishDate { get; set; }
}

I hope this will benefit others in future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top