# How to query for some key in json

**URL:** <https://discuss.elastic.co/t/how-to-query-for-some-key-in-json/9540>\
**Category:** Elasticsearch\
**Created:** [November 3, 2012, 7:18am UTC](https://discuss.elastic.co/t/how-to-query-for-some-key-in-json/9540 "2012-11-03T07:18:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![limac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/limac/32/3981_2.png) [@limac](https://discuss.elastic.co/u/limac)\
**Post date:** [November 3, 2012, 7:18am UTC](https://discuss.elastic.co/t/how-to-query-for-some-key-in-json/9540/1 "2012-11-03T07:18:20Z")

</div>

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!

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 3, 2012, 7:23am UTC](https://discuss.elastic.co/t/how-to-query-for-some-key-in-json/9540/2 "2012-11-03T07:23:42Z")

</div>

Use a MatchQuery [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html)

or TermQuery

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

## HTH

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

Le 3 nov. 2012 à 08:18, Wang [cnwangyong@gmail.com](mailto: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!
> 
> --

--

---

<div class="post-metadata">

**Author:** ![limac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/limac/32/3981_2.png) [@limac](https://discuss.elastic.co/u/limac)\
**Post date:** [November 3, 2012, 8:12am UTC](https://discuss.elastic.co/t/how-to-query-for-some-key-in-json/9540/3 "2012-11-03T08:12:20Z")

</div>

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](mailto:david@pilato.fr) wrote:

> Use a MatchQuery  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html)
> 
> or TermQuery  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html)
> 
> ## HTH
> 
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 3 nov. 2012 à 08:18, Wang [cnwangyong@gmail.com](mailto: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!
> > 
> > --
> 
> --

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [November 3, 2012, 1:44pm UTC](https://discuss.elastic.co/t/how-to-query-for-some-key-in-json/9540/4 "2012-11-03T13:44:37Z")

</div>

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:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

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

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Sat, Nov 3, 2012 at 10:12 AM, Wang [cnwangyong@gmail.com](mailto: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](mailto:david@pilato.fr) wrote:
> 
> > Use a MatchQuery  
> > [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html)
> > 
> > or TermQuery  
> > [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html)
> > 
> > ## HTH
> > 
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 3 nov. 2012 à 08:18, Wang [cnwangyong@gmail.com](mailto: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!
> > > 
> > > --
> > 
> > --
> 
> --

--

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:06am UTC](https://discuss.elastic.co/t/how-to-query-for-some-key-in-json/9540/5 "2017-07-06T03:06:21Z")

</div>


