0

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 error RequestError: 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}}}}}}}
3
  • Can you please share index mapping ? is it your id field define as int or flot in index mapping ? Commented Jun 28, 2022 at 10:56
  • I changed the mappings, id to text, now 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}}}}}}} Commented Jun 28, 2022 at 11:05
  • Thanks for providing mapping. Please check below my answer. Commented Jun 28, 2022 at 11:10

1 Answer 1

1

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]\""
    }
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

will it impact any other search like if i am searching fifa in the query, will it cause any issue. Like latency issue anything like that
also can you share elastic doc saying this for the second issue?
Yes, it will impact because once you provide field as description.keyword and search for fifa it will not return any response. Please check my updated answer for how you can do extact match with description field.
Hey got it sagar, any latency issue will be there ,if we put description.keyword instead of description which i want to know
it not impact latency but you need to do testing of it with your data. jsut thing you need to note is description.keyword will do exact match and not allowed partial match.

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.