Below is my dictionary
abc = [
{'id':"1", 'name': 'cristiano ronaldo', 'description': '[email protected]'},
{'id':"2", 'name': 'lionel messi', 'description': '[email protected]'},
{'id':"3", 'name': 'Lionel Jr', 'description': '[email protected]'}
]
Ingested the players into elasticsearch
for i in abc:
es.index(index="players", body=i, id=i['id'])
Below is the dsl query
resp = es.search(index="players",body={
"query": {
"query_string": {
"fields": ["id^12","description^2", "name^2"],
"query": "[email protected]"
}
}})
resp
Issue 1: if
"fields": ["id^12","description^2", "name^2"]
then i am getting errorRequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: For input string: "[email protected]"'
Issue 2: if my fields are
["description^2", "name^2"]
I am expecting one document which contain[email protected]
but returning all 3 documents
Edited: From the comment of sagar my setting id was long which i changed now . mapping is below. and issue1 is resolved
{'players': {'mappings': {'properties': {'description': {'type': 'text',
'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}},
'id': {'type': 'text',
'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}},
'name': {'type': 'text',
'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}}}}}
CodePudding user response:
Issue 1: if "fields": ["id^12","description^2", "name^2"] then i am getting error RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: For input string: "[email protected]"'
Above issue you are getting because your id
field is define as integer
or flot
type of field (other then text
type of field). You need to provide "lenient": true
in your query and it will not return any exception.
Issue 2: if my fields are ["description^2", "name^2"] I am expecting one document which contain [email protected] but returning all 3 documents
Above issue is happening because you are searching on text
type of field which applied default standard
analyzer when you do search.
So when you search [email protected]
then it will create two token brazil
and fifa.com
. Here, fifa.com
is matching in your all 3 documents so it is returning in result. To resolved this issue, you can use description.keyword
field.
Below query will resolved your both issue:
{
"query": {
"query_string": {
"lenient": true,
"fields": [
"id^12",
"description.keyword^2",
"name^2"
],
"query": "[email protected]"
}
}
}
Updated:
Based on comment if you want to search fifa
as well then you need to provide description
as field but when you search [email protected]
then you need to provide it in double quotes for exact match. Please see below example:
{
"query": {
"query_string": {
"lenient": true,
"fields": [
"id^12",
"description^2",
"name^2"
],
"query": "\"[email protected]\""
}
}
}