How do I make an OR query with keywords using multiple fields?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I make an OR query with keywords using multiple fields?

Pawel Krakowiak
I posted this on Stack Overflow as well, so feel free to answer there
if you like: http://stackoverflow.com/questions/6764553/how-should-i-query-elastic-search-given-my-mapping-and-using-keywords

I have a fairly simple mapping, which looks like this (streamlined):

{
    "location" : {
        "properties": {
            "name": { "type": "string", "boost": 2.0, "analyzer":
"snowball" },
            "description": { "type": "string", "analyzer":
"snowball" }
        }
    }
}

I want to find all documents that have a certain keyword in EITHER the
name or the description field (or both). Basically I'm looking for an
equivalent of a SQL OR query.

Initially I tried this:

{
    "fields" : ["name", "description"],
    "query" : {
        "terms" : {
            "name" : ["savage"],
            "description" : ["savage"]
        },
        "from" : 0,
        "size" : 500
    }
}

But it was brought to my attention that such query was not analyzed
and that is why I was getting no results, so I should instead use the
text query, but when I do this:

{
        "query" : {
                "bool" : {
                  "should" : [
                        {"text" : {"name" : "savage"}},
                        {"text" : {"description" : "savage"}}
                  ]
                },
                "from" : 0,
                "size" : 500
        }
}

I am getting an exception of "No query registered for [text]". What is
the proper way to pull off this query?
Reply | Threaded
Open this post in threaded view
|

Re: How do I make an OR query with keywords using multiple fields?

Clinton Gormley
Hi Pawel


> But it was brought to my attention that such query was not analyzed
> and that is why I was getting no results, so I should instead use the
> text query, but when I do this:
>
> {
> "query" : {
> "bool" : {
>  "should" : [
> {"text" : {"name" : "savage"}},
> {"text" : {"description" : "savage"}}
>  ]
> },
> "from" : 0,
> "size" : 500
> }
> }
>

Your nesting is incorrect:

{
        "query" : {
                "bool" : {
                  "should" : [
                        {"text" : {"name" : "savage"}},
                        {"text" : {"description" : "savage"}}
                  ]
                }
       },
       "from" : 0,
       "size" : 500
}

clint


Reply | Threaded
Open this post in threaded view
|

Re: How do I make an OR query with keywords using multiple fields?

Pawel Krakowiak
It throws the same exception still. I made it even simpler:

{
        "query" : {
                "bool" : {
                  "should" : [
                        {"text" : {"name" : "savage"}},
                        {"text" : {"description" : "savage"}}
                  ]
                }
       }
}

but it still does not work. I'm querying with curl (query.json is a
text file with the above contents):

curl -XGET -d @query.json http://localhost:9200/myindex/locations/_search

On 21 Lip, 11:28, Clinton Gormley <[hidden email]> wrote:

> Your nesting is incorrect:
>
> {
>         "query" : {
>                 "bool" : {
>                   "should" : [
>                         {"text" : {"name" : "savage"}},
>                         {"text" : {"description" : "savage"}}
>                   ]
>                 }
>        },
>        "from" : 0,
>        "size" : 500
>
> }
Reply | Threaded
Open this post in threaded view
|

Re: How do I make an OR query with keywords using multiple fields?

Clinton Gormley
On Thu, 2011-07-21 at 06:40 -0700, Pawel Krakowiak wrote:

> It throws the same exception still. I made it even simpler:
>
> {
>         "query" : {
>                 "bool" : {
>                   "should" : [
>                         {"text" : {"name" : "savage"}},
>                         {"text" : {"description" : "savage"}}
>                   ]
>                 }
>        }
> }
>
> but it still does not work. I'm querying with curl (query.json is a
> text file with the above contents):
>
> curl -XGET -d @query.json http://localhost:9200/myindex/locations/_search

This works for me.  Are you sure it is the same error? Are you sure
you're doing what you think you're doing?

Just try copying and pasting this:

curl http://localhost:9200/myindex/locations/_search?pretty=true -d '
{
        "query" : {
                "bool" : {
                  "should" : [
                        {"text" : {"name" : "savage"}},
                        {"text" : {"description" : "savage"}}
                  ]
                }
       }
}
'

clint


Reply | Threaded
Open this post in threaded view
|

Re: How do I make an OR query with keywords using multiple fields?

Pawel Krakowiak
Well, for once - I am having problems running multiline queries on
Windows (the command line sucks). That is why I was trying to run them
from files instead (using the -d @filename option).

Anyway, it seems as if something was awry with my installation. I
wiped out the whole directory including data and put up a fresh 0.17.1
and recreated the index with data, then incorporated a text query into
my application and it started working.

On a side note - how one is supposed to upgrade ES? I thought it was
simply a matter of overwriting files (of course while ES is down) from
a new archive?

On 21 Lip, 16:00, Clinton Gormley <[hidden email]> wrote:

> On Thu, 2011-07-21 at 06:40 -0700, Pawel Krakowiak wrote:
> > It throws the same exception still. I made it even simpler:
>
> > {
> >         "query" : {
> >                 "bool" : {
> >                   "should" : [
> >                         {"text" : {"name" : "savage"}},
> >                         {"text" : {"description" : "savage"}}
> >                   ]
> >                 }
> >        }
> > }
>
> > but it still does not work. I'm querying with curl (query.json is a
> > text file with the above contents):
>
> > curl -XGET -d @query.jsonhttp://localhost:9200/myindex/locations/_search
>
> This works for me.  Are you sure it is the same error? Are you sure
> you're doing what you think you're doing?
>
> Just try copying and pasting this:
>
> curlhttp://localhost:9200/myindex/locations/_search?pretty=true-d '
> {
>         "query" : {
>                 "bool" : {
>                   "should" : [
>                         {"text" : {"name" : "savage"}},
>                         {"text" : {"description" : "savage"}}
>                   ]
>                 }
>        }}
>
> '
>
> clint
Reply | Threaded
Open this post in threaded view
|

Re: How do I make an OR query with keywords using multiple fields?

Clinton Gormley
On Thu, 2011-07-21 at 08:19 -0700, Pawel Krakowiak wrote:

> Well, for once - I am having problems running multiline queries on
> Windows (the command line sucks). That is why I was trying to run them
> from files instead (using the -d @filename option).
>
> Anyway, it seems as if something was awry with my installation. I
> wiped out the whole directory including data and put up a fresh 0.17.1
> and recreated the index with data, then incorporated a text query into
> my application and it started working.
>
> On a side note - how one is supposed to upgrade ES? I thought it was
> simply a matter of overwriting files (of course while ES is down) from
> a new archive?

It is, but you don't want to leave old libs in there - they can cause
conflicts

clint