Reputation: 553
I have an index like this:
"mappings": {
"_doc": {
"properties": {
"key_words": {"type": "text", "analyzer": "english", "search_analyzer": "english", "index": True},
"name": {"type": "text", "index": False},
}
}
this index contains the topic names and keywords which should be matched to relate this topic to some text. So I need to make a search by topics index using the long text and find all topics which have a full match. For example, if I have these topics in index:
{"name": "a", "key_words": "World cup"}
{"name": "b", "key_words": "Gaming cup"}
{"name": "c", "key_words": "Cup"}
And text:
The World Championship, some country win the Cup on tennis!
I want to make the query using the "text", which will match only "a" and "c" documents, because of all keywords from the document present in the text.
Can someone please help me with building this query? ES version: 6.8
Upvotes: 2
Views: 447
Reputation: 553
Thanks for Jaspreet Chahal for the link in the comments, I found the solution there. I changed the mapping so it starts looking like this:
{
"mappings": {
"_doc": {
"properties": {
"key_words": {"type": "text", "analyzer": "english", "search_analyzer": "english", "index": True, "fielddata": True},
"name": {"type": "text", "index": False},
}
}
}
}
and I needed to do the search in two calls, first is to analyze the text and generate the tokens
analyzed = await el.indices.analyze(body={"analyzer": "english", "text": "The World Championship, some country win the Cup on tennis!"})
and then the call with a script condition
{
"query": {
"bool": {
"must": [{
"match": {
"key_words": desc
}}],
"filter": {
"script": {
"script": {
"source": "if(params.search_tokens.containsAll(doc['key_words'].values)){return true;}",
"lang": "painless",
"params": {
"search_tokens": [an['token'] for an in analyzed['tokens']]
}
}
}
}
}
},
"_source": ["_id"]
}
Upvotes: 1