How to query for some key in json


(Yong Wang) #1

Hello all,

I am thinking how to index json doc so that I can query by a key
value. Any good idea?

Like this:
the docs look like
{"foo":"bar baz", "foo":"bar"}

how to query all docs have "bar" for key "foo"?

Thanks!

--


(David Pilato) #2

Use a MatchQuery http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html

or TermQuery
http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html

HTH

David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 3 nov. 2012 à 08:18, Wang cnwangyong@gmail.com a écrit :

Hello all,

I am thinking how to index json doc so that I can query by a key
value. Any good idea?

Like this:
the docs look like
{"foo":"bar baz", "foo":"bar"}

how to query all docs have "bar" for key "foo"?

Thanks!

--

--


(Yong Wang) #3

Sorry for my express is not clear.

I mean how to query the key if the json is one field of the doc.
like:
{
foo:bar,
jfoo:{k1:abc, k2:v2},
}

how to match docs with k1=abc?

On 11/3/12, David Pilato david@pilato.fr wrote:

Use a MatchQuery
http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html

or TermQuery
http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html

HTH

David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 3 nov. 2012 à 08:18, Wang cnwangyong@gmail.com a écrit :

Hello all,

I am thinking how to index json doc so that I can query by a key
value. Any good idea?

Like this:
the docs look like
{"foo":"bar baz", "foo":"bar"}

how to query all docs have "bar" for key "foo"?

Thanks!

--

--

--


(Radu Gheorghe) #4

Hi,

Assuming that you've indexed the document as a dynamic object, which
is the default of you do:

curl -XPUT localhost:9200/test/test/1 -d '{
"foo": "bar",
"jfoo": { "k1":"abc", "k2": "v2"}
}'

By the way, you can check the mapping afterwards to confirm and in
this case you should get something like this:

curl -XGET localhost:9200/test/test/_mapping?pretty=true
{
"test" : {
"properties" : {
"foo" : {
"type" : "string"
},
"jfoo" : {
"dynamic" : "true",
"properties" : {
"k1" : {
"type" : "string"
},
"k2" : {
"type" : "string"
}
}
}
}
}
}

Then, if you want to search for the string "abc" at the field "k1"
under "jfoo:, you could still use a term or match query, like David
suggested:

curl -XPOST localhost:9200/test/test/_search?pretty=true -d '{
"query": {
"term": {
"jfoo.k1": "abc"
}
}
}'

If you just want to get all the docs that have the "k1" field under
"jfoo", then you can use an Exists filter:
http://www.elasticsearch.org/guide/reference/query-dsl/exists-filter.html

Like this:
curl -XPOST localhost:9200/test/test/_search?pretty=true -d '{
"filter": {
"exists": {
"field": "jfoo.k1"
}
}
}'

Best regards,
Radu

http://sematext.com/ -- ElasticSearch -- Solr -- Lucene

On Sat, Nov 3, 2012 at 10:12 AM, Wang cnwangyong@gmail.com wrote:

Sorry for my express is not clear.

I mean how to query the key if the json is one field of the doc.
like:
{
foo:bar,
jfoo:{k1:abc, k2:v2},
}

how to match docs with k1=abc?

On 11/3/12, David Pilato david@pilato.fr wrote:

Use a MatchQuery
http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html

or TermQuery
http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html

HTH

David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 3 nov. 2012 à 08:18, Wang cnwangyong@gmail.com a écrit :

Hello all,

I am thinking how to index json doc so that I can query by a key
value. Any good idea?

Like this:
the docs look like
{"foo":"bar baz", "foo":"bar"}

how to query all docs have "bar" for key "foo"?

Thanks!

--

--

--

--


(system) #5