Quantcast

matching a phrase in URI mode

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

matching a phrase in URI mode

eranid
I have an index that has "user_name" with the following mapping:

'user' :
            {
            '_id' :
                {
                 'path' : 'user_id'
                },
           'properties':
                {
                    'user_name': { 'type': 'string'},
                    'user_id: { 'type': 'string'},
                 }
            }

1. I wish to get the user corresponding to a specific user_name (i.e. "richard baker", but not anyone else who has "baker" in his name.
2. I wish to find anyone who has both words in his name ("richard Jr. baker" is also acceptable)

In both cases I am interested on how to do this through the API, but also as a simple query string if possible.
Also: what do I do with a space in the URI? should it be URL encoded to "%20"?

for the first case, I tried:
Furl -X GET "http://localhost:9200/test_db/google_user/_match?q=user_name:richard%20baker&fields=user_name&type:phrase"

but it did not work (obviously...)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: matching a phrase in URI mode

Clinton Gormley-2
Hiya

On Sun, 2013-02-24 at 05:31 -0800, eranid wrote:

> I have an index that has "user_name" with the following mapping:
>
> 'user' :
>             {
>             '_id' :
>                 {
>                  'path' : 'user_id'
>                 },
>            'properties':
>                 {
>                     'user_name': { 'type': 'string'},
>                     'user_id: { 'type': 'string'},
>                  }
>             }
>
> 1. I wish to get the user corresponding to a specific user_name (i.e.
> "richard baker", but not anyone else who has "baker" in his name.

So "Richard BAKER" should match as well? In other words, you're looking
for a full text match, rather than an exact value match, correct?

> 2. I wish to find anyone who has both words in his name ("richard Jr. baker"
> is also acceptable)
>
> In both cases I am interested on how to do this through the API, but also as
> a simple query string if possible.

You can use a simple 'match' query, with the "operator" changed from
"or" to "and":

curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1'  -d '
{
   "user_name" : "Richard Wilfred Baker"
}
'

curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1'  -d '
{
   "user_name" : "John Baker"
}
'

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "query" : {
      "match" : {
         "user_name" : {
            "operator" : "and",
            "query" : "richard baker"
         }
      }
   }
}
'
# {
#    "hits" : {
#       "hits" : [
#          {
#             "_source" : {
#                "user_name" : "Richard Wilfred Baker"
#             },
#             "_score" : 0.2169777,
#             "_index" : "test",
#             "_id" : "7gN3AK3LSFuPqst85VSF4A",
#             "_type" : "test"
#          }
#       ],
#       "max_score" : 0.2169777,
#       "total" : 1
#    },
#    "timed_out" : false,
#    "_shards" : {
#       "failed" : 0,
#       "successful" : 5,
#       "total" : 5
#    },
#    "took" : 66
# }

Don't use the query string approach.  First, you don't want to expose ES
directly to users (unless you are talking about just internal users).
Second, the query string query allows users to run extremely heavy
queries on your cluster.  I much prefer the approach of parsing any
search keywords in my application first, so that I'm sure about what I'm
sending to ES.

clint

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.


Loading...