You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Furkan KAMACI <fu...@gmail.com> on 2017/04/16 20:47:16 UTC

Filter if Field Exists

Hi,

I have a schema like:

name,
department,
type

type is an optional field. Some documents don't have that field. Let's
assume I have these:

Doc 1:
name: test
type: research

Doc 2:
name: test
type: developer

Doc 3:
name: test

I want to search name: test and type:research if type field exists (result
will be Doc 1 and Doc 3).

How can I do that?

Kind Regards,
Furkan KAMACI

Re: Filter if Field Exists

Posted by Erik Hatcher <er...@gmail.com>.
Too many ‘+’’s in there, I think.  

I think the query you want is this, and let’s be precise about the query parser here too in case that’s getting in the way and split this stuff up into separate reusable clauses:

    ?qq=test
    &q_no_type=({!field f=name v=$qq} -type:*)
    &q_research=({!field f=name v=$qq} type:research)
    &q={!lucene}${q_no_type) OR ${q_research}

Sorry, that’s just how my brain thinks, above - in splitting this stuff out.   But in one line it’s really:

   q=(name:test -type:*) OR (name:test AND type:research)

	Erik

    

> On Apr 17, 2017, at 7:22 AM, Furkan KAMACI <fu...@gmail.com> wrote:
> 
> On the other hand, that query does not do what I want.
> 
> On Mon, Apr 17, 2017 at 2:18 PM, Furkan KAMACI <fu...@gmail.com>
> wrote:
> 
>> Btw, what is the difference between
>> 
>> +name:test +(type:research (*:* -type:[* TO *]))
>> 
>> and
>> 
>> +name:test +(type:research -type:[* TO *])
>> 
>> On Mon, Apr 17, 2017 at 1:33 PM, Furkan KAMACI <fu...@gmail.com>
>> wrote:
>> 
>>> Actually, amount of documents which have 'type' field is relatively too
>>> small across all documents at index.
>>> 
>>> On Mon, Apr 17, 2017 at 7:08 AM, Alexandre Rafalovitch <
>>> arafalov@gmail.com> wrote:
>>> 
>>>> What about setting a default value for the field? That is probably
>>>> faster than negative search clauses?
>>>> 
>>>> Regards,
>>>>   Alex.
>>>> ----
>>>> http://www.solr-start.com/ - Resources for Solr users, new and
>>>> experienced
>>>> 
>>>> 
>>>> On 16 April 2017 at 23:58, Mikhail Khludnev <mk...@apache.org> wrote:
>>>>> +name:test +(type:research (*:* -type:[* TO *]))
>>>>> 
>>>>> On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <
>>>> furkankamaci@gmail.com>
>>>>> wrote:
>>>>> 
>>>>>> Hi,
>>>>>> 
>>>>>> I have a schema like:
>>>>>> 
>>>>>> name,
>>>>>> department,
>>>>>> type
>>>>>> 
>>>>>> type is an optional field. Some documents don't have that field. Let's
>>>>>> assume I have these:
>>>>>> 
>>>>>> Doc 1:
>>>>>> name: test
>>>>>> type: research
>>>>>> 
>>>>>> Doc 2:
>>>>>> name: test
>>>>>> type: developer
>>>>>> 
>>>>>> Doc 3:
>>>>>> name: test
>>>>>> 
>>>>>> I want to search name: test and type:research if type field exists
>>>> (result
>>>>>> will be Doc 1 and Doc 3).
>>>>>> 
>>>>>> How can I do that?
>>>>>> 
>>>>>> Kind Regards,
>>>>>> Furkan KAMACI
>>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> --
>>>>> Sincerely yours
>>>>> Mikhail Khludnev
>>>> 
>>> 
>>> 
>> 


Re: Filter if Field Exists

Posted by Furkan KAMACI <fu...@gmail.com>.
@Alexandre Rafalovitch,

I could define empty string => "" as default value but than I do facet on
that field too. I will need to filter empty strings from facet generation
logic. By the way, which one is faster:

either defining empty string as default value and appending (OR type:"") to
queries
or negative search clauses?

On Mon, Apr 17, 2017 at 2:22 PM, Furkan KAMACI <fu...@gmail.com>
wrote:

> On the other hand, that query does not do what I want.
>
> On Mon, Apr 17, 2017 at 2:18 PM, Furkan KAMACI <fu...@gmail.com>
> wrote:
>
>> Btw, what is the difference between
>>
>> +name:test +(type:research (*:* -type:[* TO *]))
>>
>> and
>>
>> +name:test +(type:research -type:[* TO *])
>>
>> On Mon, Apr 17, 2017 at 1:33 PM, Furkan KAMACI <fu...@gmail.com>
>> wrote:
>>
>>> Actually, amount of documents which have 'type' field is relatively too
>>> small across all documents at index.
>>>
>>> On Mon, Apr 17, 2017 at 7:08 AM, Alexandre Rafalovitch <
>>> arafalov@gmail.com> wrote:
>>>
>>>> What about setting a default value for the field? That is probably
>>>> faster than negative search clauses?
>>>>
>>>> Regards,
>>>>    Alex.
>>>> ----
>>>> http://www.solr-start.com/ - Resources for Solr users, new and
>>>> experienced
>>>>
>>>>
>>>> On 16 April 2017 at 23:58, Mikhail Khludnev <mk...@apache.org> wrote:
>>>> > +name:test +(type:research (*:* -type:[* TO *]))
>>>> >
>>>> > On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <
>>>> furkankamaci@gmail.com>
>>>> > wrote:
>>>> >
>>>> >> Hi,
>>>> >>
>>>> >> I have a schema like:
>>>> >>
>>>> >> name,
>>>> >> department,
>>>> >> type
>>>> >>
>>>> >> type is an optional field. Some documents don't have that field.
>>>> Let's
>>>> >> assume I have these:
>>>> >>
>>>> >> Doc 1:
>>>> >> name: test
>>>> >> type: research
>>>> >>
>>>> >> Doc 2:
>>>> >> name: test
>>>> >> type: developer
>>>> >>
>>>> >> Doc 3:
>>>> >> name: test
>>>> >>
>>>> >> I want to search name: test and type:research if type field exists
>>>> (result
>>>> >> will be Doc 1 and Doc 3).
>>>> >>
>>>> >> How can I do that?
>>>> >>
>>>> >> Kind Regards,
>>>> >> Furkan KAMACI
>>>> >>
>>>> >
>>>> >
>>>> >
>>>> > --
>>>> > Sincerely yours
>>>> > Mikhail Khludnev
>>>>
>>>
>>>
>>
>

Re: Filter if Field Exists

Posted by Furkan KAMACI <fu...@gmail.com>.
On the other hand, that query does not do what I want.

On Mon, Apr 17, 2017 at 2:18 PM, Furkan KAMACI <fu...@gmail.com>
wrote:

> Btw, what is the difference between
>
> +name:test +(type:research (*:* -type:[* TO *]))
>
> and
>
> +name:test +(type:research -type:[* TO *])
>
> On Mon, Apr 17, 2017 at 1:33 PM, Furkan KAMACI <fu...@gmail.com>
> wrote:
>
>> Actually, amount of documents which have 'type' field is relatively too
>> small across all documents at index.
>>
>> On Mon, Apr 17, 2017 at 7:08 AM, Alexandre Rafalovitch <
>> arafalov@gmail.com> wrote:
>>
>>> What about setting a default value for the field? That is probably
>>> faster than negative search clauses?
>>>
>>> Regards,
>>>    Alex.
>>> ----
>>> http://www.solr-start.com/ - Resources for Solr users, new and
>>> experienced
>>>
>>>
>>> On 16 April 2017 at 23:58, Mikhail Khludnev <mk...@apache.org> wrote:
>>> > +name:test +(type:research (*:* -type:[* TO *]))
>>> >
>>> > On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <
>>> furkankamaci@gmail.com>
>>> > wrote:
>>> >
>>> >> Hi,
>>> >>
>>> >> I have a schema like:
>>> >>
>>> >> name,
>>> >> department,
>>> >> type
>>> >>
>>> >> type is an optional field. Some documents don't have that field. Let's
>>> >> assume I have these:
>>> >>
>>> >> Doc 1:
>>> >> name: test
>>> >> type: research
>>> >>
>>> >> Doc 2:
>>> >> name: test
>>> >> type: developer
>>> >>
>>> >> Doc 3:
>>> >> name: test
>>> >>
>>> >> I want to search name: test and type:research if type field exists
>>> (result
>>> >> will be Doc 1 and Doc 3).
>>> >>
>>> >> How can I do that?
>>> >>
>>> >> Kind Regards,
>>> >> Furkan KAMACI
>>> >>
>>> >
>>> >
>>> >
>>> > --
>>> > Sincerely yours
>>> > Mikhail Khludnev
>>>
>>
>>
>

Re: Filter if Field Exists

Posted by Erik Hatcher <er...@gmail.com>.
If you need to do an inner purely negative clause, it must be OR’d with *:* - queries about matching not excluding.  There’s a shortcut in Solr to allow a top-level purely negative clause as convenience but when it gets nested it needs pairing explicitly.

Those queries below don’t quite do what you’re asking for though.

	Erik



> On Apr 17, 2017, at 7:18 AM, Furkan KAMACI <fu...@gmail.com> wrote:
> 
> Btw, what is the difference between
> 
> +name:test +(type:research (*:* -type:[* TO *]))
> 
> and
> 
> +name:test +(type:research -type:[* TO *])
> 
> On Mon, Apr 17, 2017 at 1:33 PM, Furkan KAMACI <fu...@gmail.com>
> wrote:
> 
>> Actually, amount of documents which have 'type' field is relatively too
>> small across all documents at index.
>> 
>> On Mon, Apr 17, 2017 at 7:08 AM, Alexandre Rafalovitch <arafalov@gmail.com
>>> wrote:
>> 
>>> What about setting a default value for the field? That is probably
>>> faster than negative search clauses?
>>> 
>>> Regards,
>>>   Alex.
>>> ----
>>> http://www.solr-start.com/ - Resources for Solr users, new and
>>> experienced
>>> 
>>> 
>>> On 16 April 2017 at 23:58, Mikhail Khludnev <mk...@apache.org> wrote:
>>>> +name:test +(type:research (*:* -type:[* TO *]))
>>>> 
>>>> On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <furkankamaci@gmail.com
>>>> 
>>>> wrote:
>>>> 
>>>>> Hi,
>>>>> 
>>>>> I have a schema like:
>>>>> 
>>>>> name,
>>>>> department,
>>>>> type
>>>>> 
>>>>> type is an optional field. Some documents don't have that field. Let's
>>>>> assume I have these:
>>>>> 
>>>>> Doc 1:
>>>>> name: test
>>>>> type: research
>>>>> 
>>>>> Doc 2:
>>>>> name: test
>>>>> type: developer
>>>>> 
>>>>> Doc 3:
>>>>> name: test
>>>>> 
>>>>> I want to search name: test and type:research if type field exists
>>> (result
>>>>> will be Doc 1 and Doc 3).
>>>>> 
>>>>> How can I do that?
>>>>> 
>>>>> Kind Regards,
>>>>> Furkan KAMACI
>>>>> 
>>>> 
>>>> 
>>>> 
>>>> --
>>>> Sincerely yours
>>>> Mikhail Khludnev
>>> 
>> 
>> 


Re: Filter if Field Exists

Posted by Furkan KAMACI <fu...@gmail.com>.
Btw, what is the difference between

+name:test +(type:research (*:* -type:[* TO *]))

and

+name:test +(type:research -type:[* TO *])

On Mon, Apr 17, 2017 at 1:33 PM, Furkan KAMACI <fu...@gmail.com>
wrote:

> Actually, amount of documents which have 'type' field is relatively too
> small across all documents at index.
>
> On Mon, Apr 17, 2017 at 7:08 AM, Alexandre Rafalovitch <arafalov@gmail.com
> > wrote:
>
>> What about setting a default value for the field? That is probably
>> faster than negative search clauses?
>>
>> Regards,
>>    Alex.
>> ----
>> http://www.solr-start.com/ - Resources for Solr users, new and
>> experienced
>>
>>
>> On 16 April 2017 at 23:58, Mikhail Khludnev <mk...@apache.org> wrote:
>> > +name:test +(type:research (*:* -type:[* TO *]))
>> >
>> > On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <furkankamaci@gmail.com
>> >
>> > wrote:
>> >
>> >> Hi,
>> >>
>> >> I have a schema like:
>> >>
>> >> name,
>> >> department,
>> >> type
>> >>
>> >> type is an optional field. Some documents don't have that field. Let's
>> >> assume I have these:
>> >>
>> >> Doc 1:
>> >> name: test
>> >> type: research
>> >>
>> >> Doc 2:
>> >> name: test
>> >> type: developer
>> >>
>> >> Doc 3:
>> >> name: test
>> >>
>> >> I want to search name: test and type:research if type field exists
>> (result
>> >> will be Doc 1 and Doc 3).
>> >>
>> >> How can I do that?
>> >>
>> >> Kind Regards,
>> >> Furkan KAMACI
>> >>
>> >
>> >
>> >
>> > --
>> > Sincerely yours
>> > Mikhail Khludnev
>>
>
>

Re: Filter if Field Exists

Posted by Furkan KAMACI <fu...@gmail.com>.
Actually, amount of documents which have 'type' field is relatively too
small across all documents at index.

On Mon, Apr 17, 2017 at 7:08 AM, Alexandre Rafalovitch <ar...@gmail.com>
wrote:

> What about setting a default value for the field? That is probably
> faster than negative search clauses?
>
> Regards,
>    Alex.
> ----
> http://www.solr-start.com/ - Resources for Solr users, new and experienced
>
>
> On 16 April 2017 at 23:58, Mikhail Khludnev <mk...@apache.org> wrote:
> > +name:test +(type:research (*:* -type:[* TO *]))
> >
> > On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <fu...@gmail.com>
> > wrote:
> >
> >> Hi,
> >>
> >> I have a schema like:
> >>
> >> name,
> >> department,
> >> type
> >>
> >> type is an optional field. Some documents don't have that field. Let's
> >> assume I have these:
> >>
> >> Doc 1:
> >> name: test
> >> type: research
> >>
> >> Doc 2:
> >> name: test
> >> type: developer
> >>
> >> Doc 3:
> >> name: test
> >>
> >> I want to search name: test and type:research if type field exists
> (result
> >> will be Doc 1 and Doc 3).
> >>
> >> How can I do that?
> >>
> >> Kind Regards,
> >> Furkan KAMACI
> >>
> >
> >
> >
> > --
> > Sincerely yours
> > Mikhail Khludnev
>

Re: Filter if Field Exists

Posted by Alexandre Rafalovitch <ar...@gmail.com>.
What about setting a default value for the field? That is probably
faster than negative search clauses?

Regards,
   Alex.
----
http://www.solr-start.com/ - Resources for Solr users, new and experienced


On 16 April 2017 at 23:58, Mikhail Khludnev <mk...@apache.org> wrote:
> +name:test +(type:research (*:* -type:[* TO *]))
>
> On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <fu...@gmail.com>
> wrote:
>
>> Hi,
>>
>> I have a schema like:
>>
>> name,
>> department,
>> type
>>
>> type is an optional field. Some documents don't have that field. Let's
>> assume I have these:
>>
>> Doc 1:
>> name: test
>> type: research
>>
>> Doc 2:
>> name: test
>> type: developer
>>
>> Doc 3:
>> name: test
>>
>> I want to search name: test and type:research if type field exists (result
>> will be Doc 1 and Doc 3).
>>
>> How can I do that?
>>
>> Kind Regards,
>> Furkan KAMACI
>>
>
>
>
> --
> Sincerely yours
> Mikhail Khludnev

Re: Filter if Field Exists

Posted by Mikhail Khludnev <mk...@apache.org>.
+name:test +(type:research (*:* -type:[* TO *]))

On Sun, Apr 16, 2017 at 11:47 PM, Furkan KAMACI <fu...@gmail.com>
wrote:

> Hi,
>
> I have a schema like:
>
> name,
> department,
> type
>
> type is an optional field. Some documents don't have that field. Let's
> assume I have these:
>
> Doc 1:
> name: test
> type: research
>
> Doc 2:
> name: test
> type: developer
>
> Doc 3:
> name: test
>
> I want to search name: test and type:research if type field exists (result
> will be Doc 1 and Doc 3).
>
> How can I do that?
>
> Kind Regards,
> Furkan KAMACI
>



-- 
Sincerely yours
Mikhail Khludnev