elasticsearch


Elastic search(2.4) - search and aggregate over arrays


I have a index with mapping as -
GET /sampledesc/desc/_mapping
{
"sampledesc": {
"mappings": {
"desc": {
"properties": {
"labels": {
"type": "string",
"index": "not_analyzed"
},
"tags": {
"type": "string",
"index": "not_analyzed"
},
"user_id": {
"type": "long"
}
}
}
}
}
}
I added some data in it -
PUT /sampledesc/desc/1
{
"user_id": 1,
"tags": ["tag1", "tag2", "tag3"],
"labels": ["label1","label2"]
}
PUT /sampledesc/desc/2
{
"user_id": 2,
"tags": ["tag2","tag3",],
"labels": ["label2","label4"]
}
PUT /sampledesc/desc/3
{
"user_id": 3,
"tags": ["tag7"],
"labels": ["label1","label4"]
}
I want to query this data with these rules:
Find all users who have given list of tags.
For those users, group the labels by count.
For eg., I want to query users who have both tag2 AND tag3 and group the labels with their count in sorted order. In the given sample data for three users, my result would be label2 = 2; label1 = 1, label4 = 1.
Hi You can use the following query
{
"aggs": {
"label_group": {
"terms": {
"field": "labels",
"size": 10
}
}
},
"query": {
"bool": {
"must": [{
"terms": {
"tags": [
"tag2"
]
}
}, {
"terms": {
"tags": [
"tag3"
]
}
}]
}
}
}
Hope this helps
Thanks

Related Links

Retrieve docs that contains only allowed tags (exactly equals)
Get max-score of Elasticsearch in CakePHP 3
ElasticSearch multiple terms aggregation order
Copy one column in elasticsearch index into another column applying analyzers
Why is my query ignoring my filter aggregation?
ElasticSearch analyzer that prioritizes order of term appearance in a search
Default elasticsearch configuration for docker container
Add alias to index from template?
Save ElasticSearch snapshot physical files in specific location
Run elasticsearch with docker
elasticsearch: How to reinitialize a node?
Elasticsearch filter on mulitple nested paths
Need to Sort the _term in elastic search aggregation result
Why search_analyzer (keyword) does not work with edgeNgram index_analyer?
Elasticsearch startup: not available immediately
Elasticsearch Partial Fields With Inner Hits

Categories

HOME
python-2.7
lambda
generics
facebook-messenger
workflow
qc
transactions
ews
theano
vsts-build
portable-class-library
mathematical-optimization
haxe
onvif
pyresttest
stack-overflow
elastic-load-balancer
lapack
mailmerge
pm2
live-streaming
mpu6050
squid
varnish-vcl
jaspersoft-studio
boomi
codefluent
gollum-wiki
qa
amazon-sns
ava
exchange-server-2010
software-packaging
mod-wsgi
router
data-collection
unrar
uri
onchange
oracleforms
react-bootstrap-table
ibm-connections
location-services
netbios
osx-lion
des
spinner
nsuserdefaults
uipickerview
angular-datatables
robust
httphandler
apache-toree
kitura
autorelease
rm
nothing
beeline
md5-file
lightning-workbench
sigsegv
punctuation
maven-jaxb2-plugin
pen
dynamics-crm-4
msxml
ltrace
hmmlearn
controllers
tablespace
system.web.optimization
clp
conan
maximize
seadragon
sqlbase
espresso
darwin
alwayson
artisan
gflags
ssmtp
sunos
cakephp-2.2
ildasm
multivariate-testing
berkeley-db-xml
object-code
loginview
redpitaya
sql-function
cidr
hotswap
mft
pbkdf2
san
rikulo
icenium
clrprofiler
trialware
autosize
dropshadow
glx
asio
delphi-prism
mygeneration
aggregator
eventaggregator
firephp
spring-modules
3270
xap
comment-conventions
activex-exe
levels

Resources

Encrypt Message