0

Below is my dictionary

abc = [
{'id':"1",  'name': 'cristiano ronaldo', 'description': 'portugal@fifa.com'},
{'id':"2",  'name': 'lionel messi', 'description': 'argentina@fifa.com'},
{'id':"3",  'name': 'Lionel Jr', 'description': 'brazil@fifa.com'}
]

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": "brazil@fifa.com"
    }
  }})
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: "brazil@fifa.com"'

  • Issue 2: if my fields are ["description^2", "name^2"] I am expecting one document which contain brazil@fifa.com 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}}}}}}}
sim
  • 524
  • 3
  • 14
  • Can you please share index mapping ? is it your `id` field define as int or flot in index mapping ? – Sagar Patel Jun 28 '22 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}}}}}}} – sim Jun 28 '22 at 11:05
  • Thanks for providing mapping. Please check below my answer. – Sagar Patel Jun 28 '22 at 11:10

1 Answers1

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: "brazil@fifa.com"'

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 brazil@fifa.com 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 brazil@fifa.com 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": "brazil@fifa.com"
    }
  }
}

Updated:

Based on comment if you want to search fifa as well then you need to provide description as field but when you search brazil@fifa.com 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": "\"brazil@fifa.com\""
    }
  }
}
Sagar Patel
  • 4,993
  • 1
  • 8
  • 19
  • 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 – sim Jun 28 '22 at 11:37
  • also can you share elastic doc saying this for the second issue? – sim Jun 28 '22 at 11:40
  • 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. – Sagar Patel Jun 28 '22 at 11:44
  • Hey got it sagar, any latency issue will be there ,if we put description.keyword instead of description which i want to know – sim Jun 28 '22 at 12:03
  • 1
    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. – Sagar Patel Jun 28 '22 at 12:36