Solrnet: problem when search field value is query operator (eq. or, and)

六月ゝ 毕业季﹏ 提交于 2019-12-12 15:28:14

问题


To be more precise I will be working with example...

Clean query is: (type:77 AND (zipCode:12345 OR name:OR))

When querying on Solr Admin page this throws exception:

org.apache.lucene.queryParser.ParseException: Cannot parse...

So on Solr Admin page I changed query to:

(type:"77" AND (zipCode:"12345" OR name:"OR"))

which worked as a charm

Now I have a problem to do the same stuff with solrnet. I am using SolrQueryByField class for querying. When I am working with

new SolrQueryByField("name", "OR")

I get Solrnet.Exceptions.InvalidFieldException which is in accordance with Solr Admin page, but when I am working with

new SolrQueryByField("name", "\"OR\"")

I get wrong results. By inspecting network traffic I found out that http get request is different (for brevity only name field name and value are given):

name%3A%22OR%22 => from Solr Admin page

name%3a%5c%22OR%5c%22 => from solrnet

My question is: what sholud I do to prevent solrnet from adding %5C (backslash) to query string?

Thanks in advance


回答1:


SolrQueryByField produces quoted/escaped values. If you have some special case where this is not desirable (such as this case) you can use SolrQuery, e.g. :

Query.Field("type").Is(77) && (Query.Field("zipCode").Is("12345") || Query.Simple("name:\"OR\""))



回答2:


Please try to pass the string array that contains multiple field names and search text in the below method. It will return the solrnet query for search with multiple filed name with OR condition.

public ISolrQuery BuildQuery(string[] SearchFields, string SearchText)
    {
        try
        {
            AbstractSolrQuery firstQuery = new SolrQueryByField(SearchFields[0], SearchText) { Quoted = false };

            for (var i = 1; i < parameters.SearchFields.Length; i++)
            {
                firstQuery = firstQuery || new SolrQueryByField(SearchFields[i], SearchText) { Quoted = false };
            }

            return firstQuery;
        }


来源:https://stackoverflow.com/questions/6042259/solrnet-problem-when-search-field-value-is-query-operator-eq-or-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!