i am trying to figure out how to query on specific fields in an object with elasticsearch. my indexed document looks like this:
{
name : 'thename'
meta : {
title : {value: 'foo is the title'}
headline: {value : 'bar is the headline'}
}
}
how would i for example create a query against meta.title.value
?
is this actually supported?
i can can query for those values without specifying a key like:
{
query: 'foo'
}
and i get the correct results back but i cant figure out how to do it if i just want to search for that specific key inside the meta object.
more specifically i am using mongoose
with mongoosastic
if that makes any difference.
here my document mapping on elasticsearch:
"mappings": {
"asset": {
"properties": {
"kind": {
"type": "string"
},
"meta": {
"properties": {
"description": {
"properties": {
"value": {
"type": "string"
}
}
},
"headline": {
"properties": {
"value": {
"type": "string"
}
}
},
"visible": {
"type": "boolean"
}
}
}
}
}
}
Example of query by a specific field:
{
"query": {
"match" : {
"name" : "thename"
}
}
}
In the example above, "name" is the name of the field you want to query for.
For the case of the nested data (meta.title, for example), you can take a look in the section "How to query nested fields" I posted in this topic https://stackoverflow.com/a/25203970/3917476 or check the "nested query" and "nested type" documentation: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query.
However, I find you should read the ElasticSearch documentation, because querying by a specific field is one of the most basic things (IMHO). http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html
There are many types of queries you can create. Try to go through most of them (for start, I suggest you "match", "term", "bool", "nested", "fuzzy" and "wildcard") creating small examples.
Edit 1
I have a suggestion for you: do not create fields with the name "value", otherwise we will have to create useless nested queries.
This is a suggestion for your mapping:
"mappings": {
"asset": {
"properties": {
"kind": {
"type": "string"
},
"meta": {
"properties": {
"description": {
"type": "string"
},
"headline": {
"type": "string"
},
"visible": {
"type": "boolean"
}
}
}
}
}
}
With this mapping, you use the following query:
{
"query": {
"nested": {
"path": "meta",
"query": {
"term": {
"description": "foo"
}
}
}
}
}
Otherwise you may use this query to your existing mapping:
{
"query": {
"nested": {
"path": "meta",
"query": {
"nested": {
"path": "description",
"query": {
"term": {
"value": "foo"
}
}
}
}
}
}
}