lucene-solr-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From William Bell <billnb...@gmail.com>
Subject Re: Issue with if() statement
Date Sun, 03 Jan 2016 10:52:35 GMT
OK. I was able to get it to work by looking at {!switch} code. I had a heck
of a time trying to get parse() to work as a separate class.

q.add(new TermQuery(new Term("state", "CO"), Occur.MUST);
return q;

That never did anything.

Here is some code that worked...

http://localhost:8983/solr/providersearchfull/select?wt=json&state1=state:NY&fl=ss,score&df=state&q={!orquery}$state,$state1

Notice $state was not sent... It works.

Thoughts?

package hg.parsers;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.solr.common.params.CommonParams;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.solr.parser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.search.QueryParsing;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QParserPlugin;
import org.apache.solr.search.SyntaxError;

/**
 * Created by bbell on 1/3/16.
 */
public class OrQueryParserPlugin extends QParserPlugin {
    //public static String NAME = "lucene";

    @Override
    public void init(NamedList args) {
    }

    @Override
    public QParser createParser(String qstr, SolrParams localParams,
SolrParams params, SolrQueryRequest req) {
        return new QParser(qstr, localParams, params, req) {
            QParser subParser;

            @Override
            public Query parse() throws SyntaxError {
                String v = localParams.get(QueryParsing.V);
                if (v == null || v.length() == 0) return null;
                String subQ = null;

                String[] arr = v.split(",");
                System.out.println("parse arr:" + arr.length + "," + v);
                if (arr != null && arr.length == 2) {
                    System.out.println("parse arr[0].length:" +
arr[0].length());
                    System.out.println("parse arr[1].length:" +
arr[1].length());
                    if (arr[0].trim().length() > 0) {
                        System.out.println("parse arr[0] params:" +
params.get(arr[0].trim()));
                        if (arr[0].trim().substring(0, 1).equals("$")) {
                            String val = params.get(arr[0].trim().substring(1));
                            if (val != null && val.length() > 0) {
                                subQ = val;
                                System.out.println("parse arr[0]:" +
arr[0].trim() + "," + subQ);
                            }
                        } else {
                            subQ = arr[0].trim();
                            System.out.println("parse arr[0]:" + subQ);
                        }
                    }
                    if (subQ == null && arr[1].trim().length() > 0) {
                        System.out.println("parse arr[1] params:" +
params.get(arr[1].trim()));
                        if (arr[1].trim().substring(0, 1).equals("$")) {
                            String val = params.get(arr[1].trim().substring(1));
                            if (val != null && val.length() > 0) {
                                subQ = val;
                                System.out.println("parse arr[1]:" +
arr[1].trim() + "," + subQ);
                            }
                        } else {
                            subQ = arr[1].trim();
                            System.out.println("parse arr[1]:" + subQ);
                        }
                    }
                } else {
                    subQ = null;
                }

                subParser = subQuery(subQ, null);
                return subParser.getQuery();
            }
            @Override
            public String[] getDefaultHighlightFields() {
                return subParser.getDefaultHighlightFields();
            }

            @Override
            public Query getHighlightQuery() throws SyntaxError {
                return subParser.getHighlightQuery();
            }

            @Override
            public void addDebugInfo(NamedList<Object> debugInfo) {
                subParser.addDebugInfo(debugInfo);
            }
        };
    }
}



.

On Sat, Jan 2, 2016 at 5:03 AM, Upayavira <uv@odoko.co.uk> wrote:

> Hrmph. I've got an Ant based codebase for building custom components
> against Solr. I've been asked on numerous occasions to publish this
> codebase. It is now at:
>
> https://github.com/upayavira/custom-solr-components
>
> There's no sample code in there yet. I'll see if I can stick one in
> there now, but I'm not sure how much time I have today to pull it all
> together (making it look nice and clear takes much more effort than
> making it work!).
>
> Upayavira
>
> On Sat, Jan 2, 2016, at 02:15 AM, William Bell wrote:
> > Sample code for a simple query parser?
> >
> > On Fri, Jan 1, 2016 at 3:08 PM, Upayavira <uv@odoko.co.uk> wrote:
> >
> > > You've got to think of what the queries will be that you produce under
> > > the bonnet - what you are talking about is either a (!func} or a
> > > {!frange} query - both of which are inefficient as they must scan the
> > > entire resultset.
> > >
> > > However, what you're trying to do is evaluate the length of the *input
> > > value*, not a value in the documents being searched, so this doesn't
> > > really apply - you're talking about doing some work before your query
> > > happens.
> > >
> > > Again, I'm not sure of a way to do this on a stock Solr, but with a
> > > little coding it should be achievable - you could extend the switch
> > > query parser, or you could write your own - they're not that hard to
> > > create. Or, you could create a search component that operates before
> the
> > > QueryComponent does.
> > >
> > > A simple queryparser of your own would seem like a pretty lightweight
> > > thing - probably 20 lines of code or less.
> > >
> > > Upayavira
> > >
> > > On Fri, Jan 1, 2016, at 09:40 PM, William Bell wrote:
> > > > Forgot last bit:
> > > >
> > > > fq={!switch case.gt.0=$state1
> > > >                              default=$state
> > > >                              func=len($state1)}</str>
> > > >
> > > >
> > > > On Fri, Jan 1, 2016 at 2:39 PM, William Bell <billnbell@gmail.com>
> > > wrote:
> > > >
> > > > > Maybe we can add function value on a switch?
> > > > >
> > > > > fq={!switch case.gt.0
> > > > >                              default=$state
> > > > >                              func=len($state1)}</str>
> > > > >
> > > > >
> > > > >
> > > > > We could add: gt, lt, eq, ge, le ?
> > > > >
> > > > > gt: greater than
> > > > > lt: less than
> > > > > eq: equal
> > > > > ge: greater than or equal
> > > > > le: less than or equal
> > > > >
> > > > > ??
> > > > >
> > > > >
> > > > >
> > > > > On Fri, Jan 1, 2016 at 2:25 PM, William Bell <billnbell@gmail.com>
> > > wrote:
> > > > >
> > > > >> This all started with me trying to use {!switch} to indicate
with
> CO
> > > or
> > > > >> NY to use. if we pass state1, that is supposed to take precedence,
> > > but if
> > > > >> nothing is returned, then use state.... Make sense now?
> > > > >>
> > > > >> I could not find a way to check for strlen($state1). Which is
> what I
> > > want
> > > > >> in the case statement. This just does not work...
> > > > >> Something like the following is what I am trying to do:
> > > > >>
> > > > >>
> > > > >> fq={!switch case."anything"=$state1
> > > > >>                             default=$state
> > > > >>                              v=$state1}</str>
> > > > >>
> > > > >> So I tried the following to set all 50 states:
> > > > >>
> > > > >>
> > > > >> fq={!switch case.all='*:*'
> > > > >>                              case.CO='state:CO'
> > > > >>                              case.NY='state:NY'
> > > > >>                              v=$state1}</str>
> > > > >>
> > > > >>
> > > > >> Obviously Oregon (OR!) is an issue, and it won't work at all
plus
> > > > >>
> > > > >>
> > > > >>
> > > > >> On Fri, Jan 1, 2016 at 2:15 PM, William Bell <billnbell@gmail.com
> >
> > > wrote:
> > > > >>
> > > > >>> Another weirdness:
> > > > >>>
> > > > >>>
> > > > >>>
> > >
> http://localhost:8983/solr/providersearch/select?wt=json&state=state:CO&state1=state:NY&fl=*&q=*:*&tt=$state1&fq={!lucene%20v=$tt}
> > > > >>> <
> > >
> http://localhost:8983/solr/providersearch/select?wt=json&state=state:CO&state1=state:NY&fl=*&q=*:*&tt=$state1&fq=%7B!lucene%20v=$tt%7D
> > > >
> > > > >>>
> > > > >>> That does not return anything.
> > > > >>>
> > > > >>> But if I set v=$state1 I get results.
> > > > >>>
> > > > >>> Can I not set equivalent variables?
> > > > >>>
> > > > >>>
> > > > >>>
> > > > >>> On Fri, Jan 1, 2016 at 2:07 PM, William Bell <
> billnbell@gmail.com>
> > > > >>> wrote:
> > > > >>>
> > > > >>>> Example.
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > >
> http://localhost:8983/solr/providersearch/select?wt=json&state=state:%22CO%22&state1=state:%22NY%22&fl=ss,score&q=*:*&fq={!lucene%20v=$state1}
> > > > >>>> <
> > >
> http://localhost:8983/solr/providersearch/select?wt=json&state=state:%22CO%22&state1=state:%22NY%22&fl=ss,score&q=*:*&fq=%7B!lucene%20v=$state1%7D
> > > >
> > > > >>>>
> > > > >>>> This return 236,000
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > >
> http://localhost:8983/solr/providersearch/select?wt=json&state=state:%22CO%22&state1=state:%22NY%22&fl=ss,score&q=*:*&fq={!lucene%20v=$state}
> > > > >>>> <
> > >
> http://localhost:8983/solr/providersearch/select?wt=json&state=state:%22CO%22&state1=state:%22NY%22&fl=ss,score&q=*:*&fq=%7B!lucene%20v=$state%7D
> > > >
> > > > >>>>
> > > > >>>> This returns 10,000
> > > > >>>>
> > > > >>>> I want to put an IF statement around which v to use.
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>> On Fri, Jan 1, 2016 at 1:52 PM, William Bell <
> billnbell@gmail.com>
> > > > >>>> wrote:
> > > > >>>>
> > > > >>>>> Sure.
> > > > >>>>>
> > > > >>>>> If the state:NY returns results filter by state:NY,
if it does
> not,
> > > > >>>>> then use state:CO. If we have results in NY, use
it, otherwise
> use
> > > CO.
> > > > >>>>>
> > > > >>>>> OK?
> > > > >>>>>
> > > > >>>>> On Fri, Jan 1, 2016 at 1:15 PM, Upayavira <uv@odoko.co.uk>
> wrote:
> > > > >>>>>
> > > > >>>>>>
> > > > >>>>>>
> > > > >>>>>> On Thu, Dec 31, 2015, at 11:50 PM, William Bell
wrote:
> > > > >>>>>> > We are getting weird results with if(exists(a),b,c).
We are
> > > getting
> > > > >>>>>> b+c!!
> > > > >>>>>> >
> > > > >>>>>> >
> > > > >>>>>>
> > >
> http://localhost:8983/solr/providersearch/select?q=*:*&wt=json&state=state:%22CO%22&state1=state:%22NY%22&fq=if(exists(query($state1)),{!lucene%20v=$state1},{!lucene%20v=$state})
> > > > >>>>>> <
> > >
> http://localhost:8983/solr/providersearch/select?q=*:*&wt=json&state=state:%22CO%22&state1=state:%22NY%22&fq=if(exists(query($state1)),%7B!lucene%20v=$state1%7D,%7B!lucene%20v=$state%7D)
> > > >
> > > > >>>>>> >
> > > > >>>>>> > I am getting NY and CO!
> > > > >>>>>> >
> > > > >>>>>> > I only want $state1, which is NY.
> > > > >>>>>> >
> > > > >>>>>> > Any other ways to craft this?
> > > > >>>>>>
> > > > >>>>>> Does this work at all?
> > > > >>>>>>
> > > > >>>>>> The if() function is a function query that can
be used to
> sort,
> > > boost
> > > > >>>>>> and as calculated fields. I haven't seen them
used in
> filtering.
> > > > >>>>>>
> > > > >>>>>> Also, the query() function does *not* do a query,
it just says
> > > "what
> > > > >>>>>> would this document score for this query?"
> > > > >>>>>>
> > > > >>>>>> Can you describe in English what you are trying
to do?
> > > > >>>>>>
> > > > >>>>>> Upayavira
> > > > >>>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>>
> > > > >>>>> --
> > > > >>>>> Bill Bell
> > > > >>>>> billnbell@gmail.com
> > > > >>>>> cell 720-256-8076
> > > > >>>>>
> > > > >>>>
> > > > >>>>
> > > > >>>>
> > > > >>>> --
> > > > >>>> Bill Bell
> > > > >>>> billnbell@gmail.com
> > > > >>>> cell 720-256-8076
> > > > >>>>
> > > > >>>
> > > > >>>
> > > > >>>
> > > > >>> --
> > > > >>> Bill Bell
> > > > >>> billnbell@gmail.com
> > > > >>> cell 720-256-8076
> > > > >>>
> > > > >>
> > > > >>
> > > > >>
> > > > >> --
> > > > >> Bill Bell
> > > > >> billnbell@gmail.com
> > > > >> cell 720-256-8076
> > > > >>
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Bill Bell
> > > > > billnbell@gmail.com
> > > > > cell 720-256-8076
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Bill Bell
> > > > billnbell@gmail.com
> > > > cell 720-256-8076
> > >
> >
> >
> >
> > --
> > Bill Bell
> > billnbell@gmail.com
> > cell 720-256-8076
>



-- 
Bill Bell
billnbell@gmail.com
cell 720-256-8076

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message