Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created April 3, 2017 16:26
Show Gist options
  • Save clintongormley/9361c37ac2c4d1936bef672d024fca37 to your computer and use it in GitHub Desktop.
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
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"
}
}
}
@FANHOOOO
Copy link

FANHOOOO commented Apr 3, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment