Created
April 3, 2017 16:26
-
-
Save clintongormley/9361c37ac2c4d1936bef672d024fca37 to your computer and use it in GitHub Desktop.
Only return docs where all of the words in the docs are in the query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PUT t | |
{ | |
"settings": { | |
"analysis": { | |
"analyzer": { | |
"unique_terms": { | |
"tokenizer": "standard", | |
"filter": [ | |
"lowercase", | |
"unique" | |
] | |
} | |
} | |
} | |
}, | |
"mappings": { | |
"t": { | |
"properties": { | |
"words": { | |
"type": "text", | |
"index_options": "docs", | |
"analyzer": "unique_terms", | |
"fields": { | |
"count": { | |
"type": "token_count", | |
"analyzer": "unique_terms" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
PUT t/t/1 | |
{ | |
"words": "the snow" | |
} | |
PUT t/t/2 | |
{ | |
"words": "in the lake in tahoe" | |
} | |
PUT t/t/3 | |
{ | |
"words": "the snow is cold" | |
} | |
POST t/_analyze | |
{ | |
"text": "The snow in the lake in Tahoe is beautiful", | |
"analyzer": "unique_terms" | |
} | |
GET t/_search | |
{ | |
"query": { | |
"function_score": { | |
"query": { | |
"bool": { | |
"should": [ | |
{ | |
"constant_score": { | |
"filter": { | |
"term": { | |
"words": "the" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"term": { | |
"words": "snow" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"term": { | |
"words": "in" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"term": { | |
"words": "lake" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"term": { | |
"words": "tahoe" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"term": { | |
"words": "is" | |
} | |
} | |
} | |
}, | |
{ | |
"constant_score": { | |
"filter": { | |
"term": { | |
"words": "beautiful" | |
} | |
} | |
} | |
} | |
] | |
} | |
}, | |
"script_score": { | |
"script": "_score - doc['words.count'].value" | |
}, | |
"min_score": 0, | |
"boost_mode": "replace" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, Thanks for your update about the solution.
I tried the solution, however I find some problems. I'm currently using ElasticSearch 2.3.4
There is no text for type, so I change it to string instead.
When I query for "The snow in the lake in Tahoe is beautiful", I want to get "in the lake in tahoe" and " the snow", I don't want to get "the snow is cold". However when I run the above script, I still get "the snow is cold". Could you help further solve it? Thanks so much