Function_score retrieve stranges results

Hello !

I'm using function_score alongside a knn query and the results are kind of strange.

I get results with very large distance from the function I ask. In this example you will get a game at 14 age where I ask 7 with the function_score.

Maybe the filter in knn has too much weight ?

Here is how to create the case :

PUT /bug_filters
{
  "mappings": {
    "properties": {
      "embedding": {
        "type": "dense_vector",
        "dims": 384
      }
    }
  }
}

The data:

Here is the query: (embedding is "Science Fiction")

GET bug_filters/_search
{
  "from": 0,
  "size": 3,
  "knn": {
    "field": "embedding",
    "query_vector": [
      -0.13229592144489288, -0.0065690018236637115, 0.052609775215387344,
      0.011947154998779297, -0.0924176499247551, 0.07560735195875168,
      0.09373809397220612, 0.09520447254180908, 0.04087772220373154,
      0.049590498208999634, -0.059368912130594254, -0.09487146884202957,
      -0.05184142291545868, -0.014641658402979374, -0.06280389428138733,
      -0.08162933588027954, -0.09049706161022186, 0.01350866723805666,
      0.03699958696961403, -0.04865887388586998, 0.00460370909422636,
      -0.0369873046875, -0.048276085406541824, 0.04666801169514656,
      -0.008158382028341293, -0.035100217908620834, 0.021922746673226357,
      0.06788501888513565, 0.019677065312862396, -0.10269355028867722,
      -0.006135032046586275, 0.07144246995449066, 0.030754247680306435,
      -0.05938609316945076, 0.016195081174373627, -0.063160240650177,
      0.05020435154438019, -0.08911071717739105, -0.0360778346657753,
      0.08933073282241821, -0.042047806084156036, 0.008641647174954414,
      -0.023813029751181602, -0.022228769958019257, -0.04921939596533775,
      0.04552343860268593, 0.008920441381633282, 0.04290918633341789,
      0.05932876840233803, -0.05435686558485031, -0.0045435321517288685,
      0.01660062186419964, -0.013323817402124405, -0.03461325168609619,
      -0.025404473766684532, -0.029513755813241005, 0.06597469002008438,
      -0.08015668392181396, -0.007282895501703024, 0.028122996911406517,
      -0.018217401579022408, -0.02129586972296238, -0.011560419574379921,
      0.02619851380586624, -0.10898888111114502, -0.07047593593597412,
      -0.022241052240133286, -0.036473944783210754, -0.09531374275684357,
      0.0478849783539772, 0.07617548108100891, -0.028686601668596268,
      -0.0024819502141326666, -0.01790846697986126, 0.034003034234046936,
      0.043085791170597076, -0.06745168566703796, -0.10342840105295181,
      -0.07076259702444077, -0.07228819280862808, 0.04006248340010643,
      -0.05549609288573265, 0.01924184337258339, -0.0033514252863824368,
      0.03328580781817436, -0.06836498528718948, 0.02515893615782261,
      0.02861216478049755, 0.08036566525697708, -0.022462690249085426,
      -0.03946955129504204, -0.000014212898349796887, -0.036756012588739395,
      0.005333820357918739, 0.014941742643713951, 0.008085436187684536,
      0.008893577381968498, -0.02136872708797455, -0.06040314957499504,
      0.11936021596193314, -0.0016558635979890823, 0.08411382883787155,
      0.009601516649127007, 0.0466848723590374, 0.03670354187488556,
      0.0653553307056427, -0.02665410377085209, 0.02768581733107567,
      0.05256827920675278, -0.03231083229184151, 0.025371620431542397,
      -0.06739424169063568, 0.0883825272321701, -0.0371636226773262,
      -0.005393417552113533, -0.026564395055174828, 0.013747521676123142,
      -0.06911376863718033, -0.0035423936787992716, -0.018930772319436073,
      0.02270578034222126, -0.007022292353212833, -0.011867387220263481,
      0.032382939010858536, 0.026112224906682968, -0.055299311876297,
      0.03937559574842453, -2.5111608272036506e-34, -0.046142950654029846,
      -0.035221654921770096, 0.03465105965733528, 0.1108323410153389,
      -0.06487131118774414, -0.02427162043750286, -0.04451324790716171,
      -0.010538245551288128, 0.0013418763410300016, -0.05640224367380142,
      -0.14363372325897217, -0.01079012081027031, -0.013239201158285141,
      0.01400691270828247, 0.03432545065879822, -0.008773082867264748,
      0.11340450495481491, 0.019983535632491112, 0.05217170715332031,
      -0.05458667874336243, -0.036350514739751816, -0.0481361486017704,
      0.05677593871951103, 0.012922336347401142, 0.09875178337097168,
      0.05543891340494156, 0.007284597959369421, 0.031988997012376785,
      -0.07462868839502335, -0.008691494353115559, 0.006450437009334564,
      0.009811115451157093, 0.006037815939635038, 0.0634508952498436,
      0.0028930408880114555, -0.03327440097928047, 0.05330231785774231,
      0.08207979798316956, -0.021156076341867447, 0.03785509616136551,
      0.05654258280992508, -0.03731849417090416, 0.03765076398849487,
      -0.03226969763636589, 0.04397575929760933, 0.07555153220891953,
      -0.005738781299442053, 0.0027951388619840145, 0.043355006724596024,
      0.03985532745718956, 0.04038757458329201, 0.009683217853307724,
      -0.06747804582118988, 0.004267401061952114, 0.018284639343619347,
      0.02030099369585514, -0.05066990479826927, 0.03132984787225723,
      0.0427887849509716, -0.023781223222613335, 0.10067564249038696,
      -0.04981401935219765, -0.0889657512307167, 0.04600849375128746,
      -0.04466231167316437, 0.029431177303195, 0.1028582826256752,
      -0.012406988069415092, 0.04326559230685234, -0.029034513980150223,
      -0.1065615713596344, 0.029465781524777412, 0.11463406682014465,
      -0.008348616771399975, -0.0021328646689653397, 0.053880028426647186,
      -0.00840633362531662, 0.005618729628622532, -0.011295761913061142,
      -0.01239859964698553, -0.028533978387713432, -0.08993663638830185,
      -0.050319939851760864, 0.015528512187302113, 0.013197534717619419,
      0.03695276379585266, -0.023846428841352463, 0.0078745037317276,
      0.013994036242365837, -0.042573049664497375, 0.007962027564644814,
      -0.03260445222258568, 0.006110901013016701, -0.04736575856804848,
      -0.008489806205034256, -1.8009027121948465e-33, 0.0005241218023002148,
      0.031001895666122437, 0.003117321291938424, 0.048458606004714966,
      0.0011553062358871102, 0.05289909616112709, 0.061047423630952835,
      0.07754753530025482, -0.02349337562918663, -0.04693884775042534,
      -0.04184290021657944, -0.023733898997306824, 0.1662731021642685,
      -0.07040738314390182, -0.055149104446172714, 0.12542134523391724,
      0.01136072352528572, -0.026592005044221878, -0.04794321954250336,
      -0.01675126701593399, -0.05666305869817734, -0.04471036046743393,
      -0.014588123187422752, 0.0495055615901947, 0.0037230446469038725,
      -0.003748263232409954, -0.036459147930145264, -0.03372372314333916,
      0.003899262985214591, 0.07892560958862305, 0.025210734456777573,
      -0.07346852868795395, -0.014810786582529545, 0.03316308930516243,
      -0.03445086255669594, 0.03883692994713783, 0.06518980860710144,
      0.07148293405771255, -0.06547800451517105, 0.06029300019145012,
      -0.0682276263833046, 0.007866458967328072, 0.0473271980881691,
      -0.0371914841234684, 0.041192200034856796, -0.027226723730564117,
      -0.006927833892405033, -0.04419878125190735, -0.04080499708652496,
      -0.009277191944420338, 0.003237704746425152, -0.012261500582098961,
      -0.02336547151207924, -0.027674537152051926, -0.024533648043870926,
      0.019142474979162216, -0.04710166156291962, -0.04537873715162277,
      -0.07497280836105347, 0.03627295792102814, 0.1194736510515213,
      0.028310969471931458, 0.016241401433944702, -0.04752490296959877,
      -0.0007934432360343635, 0.0012713174801319838, -0.10169019550085068,
      0.03823398798704147, -0.002405524021014571, -0.04413221776485443,
      0.07095804810523987, 0.01599239744246006, -0.05878834426403046,
      0.04687952995300293, 0.00995248556137085, -0.10720215737819672,
      0.028082815930247307, 0.04311851039528847, -0.022672580555081367,
      0.08251463621854782, -0.1260281205177307, -0.03457404300570488,
      -0.07475540786981583, 0.02195870876312256, 0.039959780871868134,
      0.01715986244380474, 0.08292578905820847, -0.0557662658393383,
      0.13137486577033997, -0.02402450144290924, 0.054205749183893204,
      0.04931407794356346, 0.0005548998597078025, -0.03140421584248543,
      -0.006482397206127644, -1.6309840589201485e-8, 0.02370624803006649,
      -0.08939305692911148, -0.024051029235124588, 0.014772891066968441,
      0.0761319026350975, -0.10253581404685974, -0.048810794949531555,
      0.022731540724635124, 0.01599271409213543, 0.031096819788217545,
      0.0478823259472847, 0.034923817962408066, -0.0527569018304348,
      0.10006961971521378, -0.011942214332520962, 0.09834464639425278,
      -0.017507921904325485, 0.029601914808154106, 0.00978305097669363,
      -0.04386706277728081, 0.06764359772205353, -0.049470409750938416,
      -0.05933290719985962, -0.10492463409900665, 0.030586611479520798,
      -0.08023244887590408, -0.12515272200107574, -0.02154586836695671,
      -0.06042991578578949, -0.06057354062795639, 0.02608226239681244,
      0.05097983404994011, -0.022452935576438904, -0.06492028385400772,
      0.027270305901765823, 0.012212682515382767, 0.09862051904201508,
      0.002632246119901538, 0.020161841064691544, 0.059381015598773956,
      0.05268202722072601, 0.031565479934215546, 0.030315261334180832,
      -0.016248362138867378, -0.014096487313508987, -0.005251845344901085,
      -0.036969032138586044, 0.05684174224734306, 0.031004952266812325,
      0.053219497203826904, 0.03524639829993248, 0.0355432890355587,
      0.023320913314819336, 0.024405010044574738, 0.06320729851722717,
      -0.03566635772585869, 0.00811781920492649, 0.00481315515935421,
      0.01529162097722292, -0.015245482325553894, 0.01669478788971901,
      0.05580828711390495, 0.01844032295048237, -0.019049780443310738
    ],
    "k": 5,
    "num_candidates": 50,
    "filter": {
      "bool": {
        "filter": [],
        "should": [],
        "must": [{ "match": { "tags.theme": { "query": "Science Fiction" } } }],
        "must_not": []
      }
    }
  },
  "query": {
    "function_score": {
      "query": {"bool": {
        "must": [
          {"exists": {
              "field": "tags.age.minAge"
            }
          },
          {"exists": {
              "field": "tags.age.maxTime"
            }
          },
          {"exists": {
              "field": "tags.player.max"
            }
          }
      ]
    }}, 
      "functions": [
        { "gauss": { "tags.age.minAge": { "origin": 7, "scale": 1 } } },
        { "gauss": { "tags.time.maxTime": { "origin": 30, "scale": 3 } } },
        { "gauss": { "tags.player.max": { "origin": 8, "scale": 1 } } }
      ]
    }
  }
}

Thank you!

I could find a solution, this was coming from the must in the knn filter.

Using a should in the function_score makes it more precise