elasticsearch


Elastic Search query return terms within array of a specific type


I've a mapping of an index as following:
{"tagged_index":{"mappings":{"tagged":{"properties":{"tags":{"properties":{"resources":{"properties":{"tagName":{"type":"string"},"type":{"type":"string"}}}}},"content":{"type":"string"}}}}}}
Where Resources is an array which can have multiple tags. For example
{"_id":"82906194","_source":{"tags":{"resources":[{"type":"Person","tagName":"Kim_Kardashian",},{"type":"Person","tagName":"Kanye_West",},{"type":"City","tagName":"New_York",},...},"content":" Popular NEWS ..."}}
,
{"_id":"82906195","_source":{"tags":{"resources":[{"type":"City","tagName":"London",},{"type":"Country","tagName":"USA",},{"type":"Music","tagName":"Hello",},...},"content":" Adele's Hello..."}},
...
I do know how to extract important terms[tagName] with the below query, but I do not want terms[tagName] of all types.
How can I extract only the terms which are for example Cities only [type:City]? (I would like to get a list of tagName where the type is City i.e. London, New_York, Berlin,...)
{"size":0,"query":{"filtered":{"query":{"query_string":{"query":"*","analyze_wildcard":true}}}},"aggs":{"Cities":{"terms":{"field":"tags.resources.tagName","size":10,"order":{"_count":"desc"}}}}}
Following is how the required output should look like:
{"took":1200,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":5179261,"max_score":0.0,"hits":[]},"aggregations":{"Cities":{"doc_count_error_upper_bound":46737,"sum_other_doc_count":36037440,"buckets":[{"key":"London","doc_count":332820},{"key":"New_York","doc_count":211274},{"key":"Berlin","doc_count":156954},{"key":"Amsterdam","doc_count":132173},...
Can you try this:
{
"_source" : ["tags.resources.tagName"]
"query": {
"term": {
"tags.resources.type": {
"value": "City"
}
}
}
}
Above query will fetch those resources which are of type city provided resources is of object type.
After Edit
Problem Group By Tag name which are Of city Type. That would not be achieved with the current mapping you have. You will have to change resources field to nested type.
Mapping would look like.
"mappings": {
"resource": {
"properties": {
"tags": {
"properties": {
"content": {
"type": "string"
},
"resources": {
"type": "nested",
"properties": {
"tagName": {
"type": "string"
},
"type": {
"type": "string"
}
}
}
}
}
}
}
}
Final query would be :
{
"size": 0,
"query": {
"nested": {
"path": "tags.resources",
"query": {
"match": {
"tags.resources.type": "city"
}
}
}
},
"aggs": {
"resources Nested path": {
"nested": {
"path": "tags.resources"
},
"aggs": {
"city type": {
"filter": {
"term": {
"tags.resources.type": "city"
}
},
"aggs": {
"group By tagName": {
"terms": {
"field": "tags.resources.tagName"
}
}
}
}
}
}
}
}
Output would be:
"aggregations": {
"resources Nested path": {
"doc_count": 6,
"city type": {
"doc_count": 2,
"group By tagName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "london",
"doc_count": 1
},
{
"key": "new_york",
"doc_count": 1
}
]
}
}
}
}

Related Links

Why search_analyzer (keyword) does not work with edgeNgram index_analyer?
Elasticsearch startup: not available immediately
Elasticsearch Partial Fields With Inner Hits
ElasticSearch issue when adding new path.data
To copy an index from one machine to another in elasticsearch
Elasticsearch 2.0: how to delete by query in Java
Erroneour match using snowball analyzer
ElasticSearch 2.0 upgrade now can't connect to server
Elasticsearch layered ordering
Elasticsearch significant terms on nested objects
System Configuration for 3 node elasticsearch cluster
Nest - how to write a span near query with multiple clauses?
low disk watermark [??%] exceeded on
Why are shards getting initialized and relocated during bulk insert
Terms Aggregation for nested field in Elastic Search
Why does kibana 3.x not work with elastic search 2.x?

Categories

HOME
knockout.js
shinyapps
clearcase
code-formatting
slider
semantic-web
jersey-2.0
gallery
mosquitto
visualforce
bittorrent
cdi
pyresttest
twitter-oauth
createjs
pygobject
android-5.1.1-lollipop
nullreferenceexception
multiple-tables
altium-designer
gollum-wiki
fluent-migrator
introspection
cellular-network
ruby-daemons
tortoisehg
react-bootstrap-table
django-import-export
unmarshalling
monitor
tilemill
intune
xenapp
commonsware-cwac
appcode
payeezy
buildforge
dreamfactory
twilio-php
forms-authentication
django-crispy-forms
cheat-engine
sspi
task-parallel-library
stat
flowchart
grunt-contrib-watch
jacoco-maven-plugin
raytracing
dagger
spooler
wysihtml5
.net-micro-framework
django-static-precompiler
skip-lists
google-news
algebra
alertify
front-camera
fitbit
deviare
thread-exceptions
s-function
qpixmap
yii2-model
psd
jsr363
cortana
in-app-billing
http.client
supersocket.net
modern.ie
swagger-maven-plugin
com0com
mutators
sql-processor
famo.us
aho-corasick
network-printers
mfmessagecomposeview
code-complexity
.htpasswd
miglayout
cidr
accountmanager
cloo
mft
acitree
jquery-transit
ofstream
server-name
autosize
prototypal-inheritance
algol68
background-repeat
mmc3
litespeed
datacontracts
qcar-sdk
gaelyk
tacit-programming

Resources

Encrypt Message