This is a tough one. I think there are two possible strategies here - you
can either store all tags in a single field and include all permutations in
your query, or you can find a complimentary set of tags and use it to
exclude all undesired records from your results.
In the first case, you will need to create an additional field (taglist,
for example) that would contain a sorted merged list of all tags:
doc1 will have taglist: 1-2
doc1 will have taglist: 1-2-3
doc1 will have taglist: 1-2-4
And then your will be able to use terms query for terms 1, 2, 3, 1-2, 1-3,
2-3, and 1-2-3. Needless to say this solution is not going to scale very
well if your query will contain more then a few tags.
In the second case, assuming that you have 10 different tags, the
complimentary tags will be 4,5,6,7,8,9, and 10 and your query will look
like this:
-tags:4 -tags:5 -tags:6 -taglist:7 -taglist:8 -taglist:9 -taglist:10
You can retrieve list of all available tags using faceted search
and implement query as a terms filter wrapped into a not filter.
On Friday, April 13, 2012 1:58:21 PM UTC-4, Alex Chen wrote:
Thanks Marcin for your help. Both queries you suggested would match doc2
(tags: [1,2,3]), but will miss doc1 (tags: [1,2]).
if a doc's tags are subset of the query tags, it should be matched. the
minimum_match should be the total number of tags for each doc.
Is it possible for ES to support something like this in the terms query?
"minimum_match" : "ALL"
Thanks.
-alex
On Friday, April 13, 2012 1:39:30 AM UTC-7, Marcin Dojwa wrote:
Hi,
You can use:
{
"and":[
{
"term":{
"tags":1
}
},
{
"term":{
"tags":2
}
},
{
"term":{
"tags":3
}
}
]
}
You may want to check this too:
Elasticsearch Platform — Find real-time answers at scale | Elastic
I am not sure what is minimum_match for but I can guess that the query
below could return what you want too:
{
"terms":{
"tags":[
1,
2,
3
],
"minimym_match":3
}
}
2012/4/13 Alex Chen chen650@yahoo.com
Hi,
I have a query that is very similar to the terms query, but with more
restriction. I would like to get
the documents that have all the terms appear in the query.
for example:
doc1 has "tags": [1, 2]
doc2 has "tags": [1, 2, 3]
doc3 has "tags": [1, 2, 4]
query "tags" : [1, 2, 3] should return doc1 and doc2, but not doc3,
because tag 4 is not in the query.
how can i write a query that does this?
thanks.
-alex