Elasticsearch filter based on field similarity












0















For reference, I'm using Elasticsearch 6.4.0



I have a Elasticsearch query that returns a certain number of hits, and I'm trying to remove hits with text field values that are too similar. My query is:



{
"size": 10,
"collapse": {
"field": "author_id"
},
"query": {
"function_score": {
"boost_mode": "replace",
"score_mode": "avg",
"functions": [
{
//my custom query function
}
],
"query": {
"bool": {
"must_not": [
{
"term": {
"author_id": MY_ID
}
}
]
}
}
}
},
"aggs": {
"book_name_sample": {
"sampler": {
"shard_size": 10
},
"aggs": {
"frequent_words": {
"significant_text": {
"field": "book_name",
"filter_duplicate_text": true
}
}
}
}
}
}


This query uses a custom function score combined with a filter to return books a person might like (that they haven't authored). Thing is, for some people, it returns books with names that are very similar (i.e. The Life of George Washington, Good Times with George Washington, Who was George Washington), and I'd like the hits to have a more diverse set of names.



I'm using a bucket_selector to aggregate the hits based on text similarity, and the query gives me something like:



...,
"aggregations": {
"book_name_sample": {
"doc_count": 10,
"frequent_words": {
"doc_count": 10,
"bg_count": 482626,
"buckets": [
{
"key": "George",
"doc_count": 3,
"score": 17.278715785140975,
"bg_count": 9718
},
{
"key": "Washington",
"doc_count": 3,
"score": 15.312204414323656,
"bg_count": 10919
}
]
}
}
}


Is it possible to filter the returned documents based on this aggregation result within Elasticsearch? IE remove hits with book_name_sample doc_count less than X? I know I can do this in PHP or whatever language uses the hits, but I'd like to keep it within ES. I've tried using a bucket_selector aggregator like so:



"book_name_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"freqWords": "frequent_words"
},
"script": "params.freqWords < 3"
}
}


But then I get an error: org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation



Also, if that filter removes enough documents so that the hit count is less than the requested size, is it possible to tell ES to go fetch the next top scoring hits so that hits count is filled out?










share|improve this question



























    0















    For reference, I'm using Elasticsearch 6.4.0



    I have a Elasticsearch query that returns a certain number of hits, and I'm trying to remove hits with text field values that are too similar. My query is:



    {
    "size": 10,
    "collapse": {
    "field": "author_id"
    },
    "query": {
    "function_score": {
    "boost_mode": "replace",
    "score_mode": "avg",
    "functions": [
    {
    //my custom query function
    }
    ],
    "query": {
    "bool": {
    "must_not": [
    {
    "term": {
    "author_id": MY_ID
    }
    }
    ]
    }
    }
    }
    },
    "aggs": {
    "book_name_sample": {
    "sampler": {
    "shard_size": 10
    },
    "aggs": {
    "frequent_words": {
    "significant_text": {
    "field": "book_name",
    "filter_duplicate_text": true
    }
    }
    }
    }
    }
    }


    This query uses a custom function score combined with a filter to return books a person might like (that they haven't authored). Thing is, for some people, it returns books with names that are very similar (i.e. The Life of George Washington, Good Times with George Washington, Who was George Washington), and I'd like the hits to have a more diverse set of names.



    I'm using a bucket_selector to aggregate the hits based on text similarity, and the query gives me something like:



    ...,
    "aggregations": {
    "book_name_sample": {
    "doc_count": 10,
    "frequent_words": {
    "doc_count": 10,
    "bg_count": 482626,
    "buckets": [
    {
    "key": "George",
    "doc_count": 3,
    "score": 17.278715785140975,
    "bg_count": 9718
    },
    {
    "key": "Washington",
    "doc_count": 3,
    "score": 15.312204414323656,
    "bg_count": 10919
    }
    ]
    }
    }
    }


    Is it possible to filter the returned documents based on this aggregation result within Elasticsearch? IE remove hits with book_name_sample doc_count less than X? I know I can do this in PHP or whatever language uses the hits, but I'd like to keep it within ES. I've tried using a bucket_selector aggregator like so:



    "book_name_bucket_filter": {
    "bucket_selector": {
    "buckets_path": {
    "freqWords": "frequent_words"
    },
    "script": "params.freqWords < 3"
    }
    }


    But then I get an error: org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation



    Also, if that filter removes enough documents so that the hit count is less than the requested size, is it possible to tell ES to go fetch the next top scoring hits so that hits count is filled out?










    share|improve this question

























      0












      0








      0








      For reference, I'm using Elasticsearch 6.4.0



      I have a Elasticsearch query that returns a certain number of hits, and I'm trying to remove hits with text field values that are too similar. My query is:



      {
      "size": 10,
      "collapse": {
      "field": "author_id"
      },
      "query": {
      "function_score": {
      "boost_mode": "replace",
      "score_mode": "avg",
      "functions": [
      {
      //my custom query function
      }
      ],
      "query": {
      "bool": {
      "must_not": [
      {
      "term": {
      "author_id": MY_ID
      }
      }
      ]
      }
      }
      }
      },
      "aggs": {
      "book_name_sample": {
      "sampler": {
      "shard_size": 10
      },
      "aggs": {
      "frequent_words": {
      "significant_text": {
      "field": "book_name",
      "filter_duplicate_text": true
      }
      }
      }
      }
      }
      }


      This query uses a custom function score combined with a filter to return books a person might like (that they haven't authored). Thing is, for some people, it returns books with names that are very similar (i.e. The Life of George Washington, Good Times with George Washington, Who was George Washington), and I'd like the hits to have a more diverse set of names.



      I'm using a bucket_selector to aggregate the hits based on text similarity, and the query gives me something like:



      ...,
      "aggregations": {
      "book_name_sample": {
      "doc_count": 10,
      "frequent_words": {
      "doc_count": 10,
      "bg_count": 482626,
      "buckets": [
      {
      "key": "George",
      "doc_count": 3,
      "score": 17.278715785140975,
      "bg_count": 9718
      },
      {
      "key": "Washington",
      "doc_count": 3,
      "score": 15.312204414323656,
      "bg_count": 10919
      }
      ]
      }
      }
      }


      Is it possible to filter the returned documents based on this aggregation result within Elasticsearch? IE remove hits with book_name_sample doc_count less than X? I know I can do this in PHP or whatever language uses the hits, but I'd like to keep it within ES. I've tried using a bucket_selector aggregator like so:



      "book_name_bucket_filter": {
      "bucket_selector": {
      "buckets_path": {
      "freqWords": "frequent_words"
      },
      "script": "params.freqWords < 3"
      }
      }


      But then I get an error: org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation



      Also, if that filter removes enough documents so that the hit count is less than the requested size, is it possible to tell ES to go fetch the next top scoring hits so that hits count is filled out?










      share|improve this question














      For reference, I'm using Elasticsearch 6.4.0



      I have a Elasticsearch query that returns a certain number of hits, and I'm trying to remove hits with text field values that are too similar. My query is:



      {
      "size": 10,
      "collapse": {
      "field": "author_id"
      },
      "query": {
      "function_score": {
      "boost_mode": "replace",
      "score_mode": "avg",
      "functions": [
      {
      //my custom query function
      }
      ],
      "query": {
      "bool": {
      "must_not": [
      {
      "term": {
      "author_id": MY_ID
      }
      }
      ]
      }
      }
      }
      },
      "aggs": {
      "book_name_sample": {
      "sampler": {
      "shard_size": 10
      },
      "aggs": {
      "frequent_words": {
      "significant_text": {
      "field": "book_name",
      "filter_duplicate_text": true
      }
      }
      }
      }
      }
      }


      This query uses a custom function score combined with a filter to return books a person might like (that they haven't authored). Thing is, for some people, it returns books with names that are very similar (i.e. The Life of George Washington, Good Times with George Washington, Who was George Washington), and I'd like the hits to have a more diverse set of names.



      I'm using a bucket_selector to aggregate the hits based on text similarity, and the query gives me something like:



      ...,
      "aggregations": {
      "book_name_sample": {
      "doc_count": 10,
      "frequent_words": {
      "doc_count": 10,
      "bg_count": 482626,
      "buckets": [
      {
      "key": "George",
      "doc_count": 3,
      "score": 17.278715785140975,
      "bg_count": 9718
      },
      {
      "key": "Washington",
      "doc_count": 3,
      "score": 15.312204414323656,
      "bg_count": 10919
      }
      ]
      }
      }
      }


      Is it possible to filter the returned documents based on this aggregation result within Elasticsearch? IE remove hits with book_name_sample doc_count less than X? I know I can do this in PHP or whatever language uses the hits, but I'd like to keep it within ES. I've tried using a bucket_selector aggregator like so:



      "book_name_bucket_filter": {
      "bucket_selector": {
      "buckets_path": {
      "freqWords": "frequent_words"
      },
      "script": "params.freqWords < 3"
      }
      }


      But then I get an error: org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation



      Also, if that filter removes enough documents so that the hit count is less than the requested size, is it possible to tell ES to go fetch the next top scoring hits so that hits count is filled out?







      elasticsearch elasticsearch-aggregation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 19:50









      Brian FitzsimmonsBrian Fitzsimmons

      2427




      2427
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Why not use top hits inside the aggregation to get relevant document that match the bucket? You can specify how many relevant top hits you want inside the top hits aggregation. So basically this will give you a certain number of documents for each bucket.






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028894%2felasticsearch-filter-based-on-field-similarity%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Why not use top hits inside the aggregation to get relevant document that match the bucket? You can specify how many relevant top hits you want inside the top hits aggregation. So basically this will give you a certain number of documents for each bucket.






            share|improve this answer






























              0














              Why not use top hits inside the aggregation to get relevant document that match the bucket? You can specify how many relevant top hits you want inside the top hits aggregation. So basically this will give you a certain number of documents for each bucket.






              share|improve this answer




























                0












                0








                0







                Why not use top hits inside the aggregation to get relevant document that match the bucket? You can specify how many relevant top hits you want inside the top hits aggregation. So basically this will give you a certain number of documents for each bucket.






                share|improve this answer















                Why not use top hits inside the aggregation to get relevant document that match the bucket? You can specify how many relevant top hits you want inside the top hits aggregation. So basically this will give you a certain number of documents for each bucket.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 13 at 2:55

























                answered Jan 12 at 4:40









                user10555501user10555501

                11




                11
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028894%2felasticsearch-filter-based-on-field-similarity%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas