0

I have a simple python app that uses Elasticsearch to store documents for Pokemon using this mapping:

{


"mappings": {
    "properties": {
      "id": {
        "type": "integer"
      },
      "name": {
        "type": "object"
      },
      "type": {
        "type": "keyword"
      },
      "base": {
        "type": "object"
      },
      "species": {
        "type": "text"
      },
      "description": {
        "type": "text"
      },
      "evolution": {
        "type": "object"
      },
      "profile": {
        "type": "nested",
        "properties": {
          "ability": {
            "type": "keyword"  
          }
        }
      },
      "image": {
        "type": "object"
      },
      "embedding": {  
        "type": "dense_vector",
        "dims": 384
      }
    }
  }
}

When I query by a property for example name in english from my python code:

term_query = {
    "size": 5,
    "query": {
        "term": {
            "name.english": "Pikachu"
        }
    }
}

response = es_client.search(index="pokemon", body=term_query)
print(response)

I do get back the document for Pikachu. This is the response:

{'took': 16, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 1, 'relation': 'eq'}, 'max_score': 2.5902672, 'hits': [{'_index': 'pokemon', '_type': '_doc', '_id': 'uTSUE5EB_ikN9OuVrCYz', '_score': 2.5902672, '_source': {'id': 25, 'name': {'english': 'Pikachu', 'japanese': 'ピカチュウ', 'chinese': '皮卡丘', 'french': 'Pikachu'}, 'type': ['Electric'], 'base': {'HP': 35, 'Attack': 55, 'Defense': 40, 'Sp. Attack': 50, 'Sp. Defense': 50, 'Speed': 90}, 'species': 'Mouse Pokémon', 'description': 'While sleeping, it generates electricity in the sacs in its cheeks. If it’s not getting enough sleep, it will be able to use only weak electricity.', 'evolution': {'prev': ['172', 'high Friendship'], 'next': [['26', 'use Thunder Stone']]}, 'profile': {'height': '0.4 m', 'weight': '6 kg', 'egg': ['Field', 'Fairy'], 'ability': [['Static', 'false'], ['Lightning Rod', 'true']], 'gender': '50:50'}, 'image': {'sprite': 'https://raw.githubusercontent.com/Purukitto/pokemon-data.json/master/images/pokedex/sprites/025.png', 'thumbnail': 'https://raw.githubusercontent.com/Purukitto/pokemon-data.json/master/images/pokedex/thumbnails/025.png', 'hires': 'https://raw.githubusercontent.com/Purukitto/pokemon-data.json/master/images/pokedex/hires/025.png'}, 'embedding': [-0.08073285967111588, 0.06939519941806793, 0.12187427282333374, -0.004082795698195696, -0.12248878180980682, -0.20900841057300568, 0.24315859377384186, 0.17423027753829956, 0.16088330745697021, -0.11973902583122253, 0.219094380736351, -0.1787237524986267, 0.14656412601470947, 0.26914048194885254, -0.0900934562087059, -0.02184007130563259, 0.08033201843500137, 0.22495900094509125, -0.2534410059452057, 0.041292887181043625, 0.06425879895687103, -0.23064647614955902, 0.13032366335391998, -0.18455955386161804, -0.27389195561408997, -0.039019424468278885, 0.35004937648773193, -0.015063440427184105, -0.046075183898210526, 0.06129240244626999, -0.2043248414993286, 0.06172535941004753, 0.04312220215797424, 0.08780308812856674, 0.13448253273963928, -0.05551649630069733, -0.1422523856163025, -0.027074327692389488, 0.04502112418413162, -0.1769474595785141, -0.008200257085263729, -0.25519609451293945, 0.13449157774448395, 0.2700392007827759, 0.006515479180961847, 0.5337464809417725, 0.1946619153022766, -0.3672884404659271, -0.12406620383262634, -0.09569848328828812, -0.007241903804242611, 0.3416319191455841, -0.05228937789797783, 0.036399926990270615, -0.22220291197299957, -0.061945606023073196, 0.11898031830787659, -0.18378591537475586, -0.041961733251810074, -0.047705043107271194, -0.17708761990070343, 0.16963569819927216, 0.3308679163455963, 0.04163277521729469, 0.04719669744372368, 0.05068834125995636, -0.26456910371780396, 0.026870373636484146, -0.10195782780647278, 0.23298844695091248, 0.031212573871016502, -0.3020530343055725, 0.1332738697528839, -0.29328441619873047, -0.09723328799009323, 0.12734854221343994, -0.06871715933084488, -0.09589243680238724, -0.0217132605612278, -0.04689288139343262, 0.11232715100049973, -0.1212148666381836, -0.502180814743042, 0.3030500113964081, 0.23431822657585144, 0.20775146782398224, 0.03362984582781792, -0.3450615406036377, -0.38474512100219727, -0.015262553468346596, -0.012009349651634693, 0.5406750440597534, -0.11293786019086838, 0.19005414843559265, -0.15757060050964355, -0.1948317289352417, 0.09620492905378342, -0.2629433870315552, -0.8698116540908813, -0.1905592530965805, 0.007942819967865944, 0.5021940469741821, -0.22358925640583038, 0.28493547439575195, 0.2063635140657425, -0.09802521765232086, -0.12083346396684647, -0.2472897320985794, 0.11121658980846405, 0.2569717764854431, 0.14332978427410126, -0.4158305823802948, -0.21560056507587433, 0.47279030084609985, 0.05417272448539734, 0.15705130994319916, 0.11930802464485168, 0.2766338884830475, -0.16857114434242249, 0.38867419958114624, 0.07035291939973831, -0.19579669833183289, -0.20194637775421143, -0.045491937547922134, 0.22118116915225983, -0.4455321729183197, -0.14013874530792236, 0.08595887571573257, 0.07313554733991623, 0.21260005235671997, 0.011720014736056328, -0.11162177473306656, -0.31165409088134766, -0.18956924974918365, -0.08518164604902267, 0.3265906274318695, 0.3938029110431671, 0.16317987442016602, -0.24403564631938934, 0.2091466188430786, 0.15612895786762238, 0.5397796034812927, -0.021354854106903076, -0.2237994521856308, 0.15890972316265106, -0.1837628185749054, 0.07010602951049805, -0.05256254971027374, -0.06645826995372772, 0.05712897330522537, 0.0011353784939274192, -0.26107025146484375, 0.2231748253107071, 0.03408034145832062, -0.019110316410660744, -0.09659949690103531, -0.20144665241241455, 0.22397348284721375, 0.1376374363899231, 0.09118841588497162, 0.05390686169266701, 0.2291051596403122, -0.22862714529037476, -0.011429731734097004, 0.26337283849716187, 0.008011268451809883, -0.2091754674911499, -0.018558669835329056, -0.3221794664859772, -0.03949500247836113, 0.06667587906122208, 0.1057417243719101, 0.09627758711576462, 0.02740870602428913, 0.13777735829353333, -0.31403109431266785, -0.35416749119758606, -0.06899789720773697, -0.21653032302856445, -0.027547530829906464, 0.10295339673757553, 0.1908693164587021, 0.06642354279756546, -0.2547055780887604, -0.27135589718818665, -0.10589489340782166, 0.07966331392526627, 0.10850854963064194, -0.1262790560722351, -0.2978314459323883, -0.23875080049037933, -0.3113715648651123, 0.20441405475139618, 0.047840893268585205, -0.12133669853210449, -0.025360196828842163, -0.18699155747890472, -0.3434793949127197, -0.011686017736792564, -0.1433863788843155, -0.02858828380703926, -0.2638932764530182, -0.20476993918418884, -0.12419438362121582, 0.028579195961356163, -0.1174812987446785, -0.33529555797576904, -0.20364123582839966, 0.04101632535457611, -0.09168056398630142, -0.05435829237103462, -0.30858591198921204, 0.25615713000297546, 0.1913250833749771, 0.6707220673561096, 0.4516240358352661, -0.10038889944553375, 0.09332328289747238, -0.08849727362394333, -0.04820533096790314, 0.3817048966884613, -0.2124391496181488, -0.20861664414405823, -0.3969747722148895, -0.26697418093681335, -0.09186507016420364, -0.17242462933063507, 0.163199320435524, -0.18881991505622864, 0.08426131308078766, -0.2372647523880005, -0.004029334522783756, 0.06960441172122955, -0.047179535031318665, -0.20344287157058716, 0.183263897895813, -0.06168253347277641, -0.04381486400961876, 0.21352356672286987, -0.29498425126075745, 0.046090275049209595, 0.016421712934970856, -0.03849317133426666, 0.2436819225549698, -0.24784734845161438, 0.06414017081260681, -0.01664029061794281, 0.18358607590198517, 0.025173719972372055, 0.6090837717056274, 0.050406910479068756, 0.1362634152173996, -0.22938694059848785, 0.3377200961112976, 0.13915757834911346, 0.23770390450954437, 0.1720094382762909, 0.030198220163583755, 0.024356193840503693, -0.28200817108154297, -0.19686788320541382, -0.1351262480020523, -0.007643698249012232, -0.22928962111473083, 0.11154638230800629, -0.014717038720846176, 0.1324407309293747, 0.46048006415367126, -0.017119301483035088, -0.4727839231491089, -0.4402349591255188, -0.01458784844726324, -0.04428641498088837, 0.04039650410413742, 0.48811277747154236, -0.3889062702655792, -0.2668595612049103, 0.05276121944189072, -0.1911555528640747, -0.11344823241233826, 0.0762748047709465, -0.19064147770404816, 0.2186267226934433, -0.23358970880508423, 0.15427519381046295, -0.13358311355113983, 0.03089122287929058, -0.26767098903656006, 0.08962049335241318, -0.13496246933937073, 0.10376256704330444, 0.26555293798446655, 0.7292829155921936, 0.12933622300624847, 0.1885850727558136, 0.33418792486190796, -0.0045865499414503574, -0.08271858841180801, -0.19287362694740295, 0.39168789982795715, 0.07085824757814407, 0.16441291570663452, 0.026745975017547607, -0.014314485713839531, -0.10071783512830734, -0.08725757896900177, 0.04012788459658623, -0.22500525414943695, 0.1916060894727707, -0.44129520654678345, -0.34983548521995544, 0.3279268145561218, 0.35589221119880676, -0.014993308112025261, -0.2724052369594574, 0.1550331711769104, -0.16982153058052063, 0.28001534938812256, -0.08957020193338394, 0.26859310269355774, -0.06395307928323746, -0.18223333358764648, -0.03468851372599602, -0.091072678565979, -0.012290200218558311, -0.28910940885543823, 0.08019937574863434, -0.2714097201824188, 0.23566178977489471, -0.15085045993328094, 0.31374698877334595, 0.030088581144809723, 0.1816730797290802, -0.13970644772052765, -0.0039431205950677395, 0.6152960658073425, 0.4371360242366791, 0.03539286553859711, -0.10140117257833481, 0.03148588910698891, -0.16396838426589966, -0.3729839324951172, -0.08252952247858047, -0.10826507955789566, -0.13000987470149994, -0.0005456415819935501, -0.11005621403455734, 0.17918984591960907, -0.3737146556377411, -0.3396584987640381, -0.08200288563966751, 0.14435864984989166, 0.5620496273040771, 0.27496692538261414, 0.004530945792794228, -0.15538428723812103, 0.380673348903656, -0.22902937233448029, 0.22183893620967865, -0.06294988840818405, 0.08632779121398926, -0.5587152242660522, -0.22411471605300903, 0.1320917010307312, -0.1736510843038559, 0.09566263109445572, 0.4774761497974396, -0.009492949582636356, -0.2615973651409149, 0.31702664494514465, -0.11396189033985138, 0.4716211259365082, 0.057476241141557693, -0.05458242818713188, -0.05082737281918526, -0.1389731615781784, 0.22226481139659882, 0.5554996728897095, -0.19391866028308868, 0.24401207268238068, 0.3081281781196594, 0.26846665143966675, 0.13681122660636902, 0.04111223667860031]}}]}}

I checked all the properties and they look correct. The embedding also has the right lenght 384 which is what this model I am using produces: embeddings_model = SentenceTransformer("paraphrase-MiniLM-L6-v2")

However when I try to run an embedding cosine similarity query like:

query = "Pikachu"
query_embedding = embeddings_model.encode([query])[0].tolist()
script_query_cosine = {
    "size": 5,
    "query": {
        "script_score": {
            "query": {
                "match_all": {}
            },
            "script": {
                "source": "cosineSimilarity(params.query_vector, 'embedding') + 1.0",
                "params": {
                    "query_vector": query_embedding
                }
            }
        }
    }
}

response = es_client.search(index="pokemon", body=script_query_cosine)
print(response)

I get this error:

RequestError(400, 'search_phase_execution_exception', 'runtime error')

I checked the embedding size and it is correct. I checked if any documents are missing the embeddings fields and I found no documents that miss it. I have no clue what this error is.

2
  • Did you try to print script_query_cosine and execute from the Kibana ? it seems like query formation have some issue. Please print query and see if it correct or not. Commented Aug 2, 2024 at 15:37
  • Hi @SagarPatel what do you mean print the query string in my python code? Or print the query the client sends? How do I do this? Thank you! Commented Aug 3, 2024 at 17:17

1 Answer 1

0

Term is an exact/perfect matching query and Match is fuzzy matching query.

I would suggest you to use match query, instead of term for your case.

GET /pokemon/_search
{
  "size": 5,
  "query": {
    "match": {
      "name.english": "Pikachu"
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.