Expressing has-any-value in the Query DSL

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Expressing has-any-value in the Query DSL

Clinton Gormley
Hiya

If I have a mapping that looks like this:

  {
    "name": { "type": "string", "index": "analyzed"},
    .... more fields ...
  }

Is there any way of saying "give me only documents which have a value in
the name field", ie name is not blank.

Would it be:

 {
   "bool": {
       "mustNot": [ { "term": { "name": ""}}]
   }
 }

That doesn't seem to work.

Would I have to add a new field to the mapping? eg

   { "has_name": { "type":  "boolean" }}

thanks

Clint

--
Web Announcements Limited is a company registered in England and Wales,
with company number 05608868, with registered address at 10 Arvon Road,
London, N5 1PR.

Reply | Threaded
Open this post in threaded view
|

Re: Expressing has-any-value in the Query DSL

kimchy
Administrator
You can pass a JSON null in the name field when it has no value, and add a nullValue to the name mapping, for example:
 
{
   "name": { "type": "string", "index": "analyzed", "nullValue":"na"},
   .... more fields ...
 }

Then, you can search on all the names that have "na".

-shay.banon


On Thu, Mar 11, 2010 at 12:46 PM, Clinton Gormley <[hidden email]> wrote:
Hiya

If I have a mapping that looks like this:

 {
   "name": { "type": "string", "index": "analyzed"},
   .... more fields ...
 }

Is there any way of saying "give me only documents which have a value in
the name field", ie name is not blank.

Would it be:

 {
  "bool": {
      "mustNot": [ { "term": { "name": ""}}]
  }
 }

That doesn't seem to work.

Would I have to add a new field to the mapping? eg

  { "has_name": { "type":  "boolean" }}

thanks

Clint

--
Web Announcements Limited is a company registered in England and Wales,
with company number 05608868, with registered address at 10 Arvon Road,
London, N5 1PR.