Azure cognitive search escape special characters

140 views Asked by At

I am trying to query a azure search index which has special chars. I am not getting the intended results with the following query,

{
  "search": "*",
  "filter": "search.ismatch('*\\ca-\\*', 'Text')",
  "count": true,
  "top": 10
}

Results set,

[{
    "Id": "11110",    
    "Author": null,
    "Text": "ca-1000"
},
{
    "Id": "11123",    
    "Author": null,
    "Text": "da-20000"
},{
    "Id": "11136",    
    "Author": null,
    "Text": "ca 900"
}]

Expected,

[{
    "Id": "11109",    
    "Author": null,
    "Text": "ca-1000"
}]

Basically, I want to query the "Text" field of any record that has value "ca-". What am I missing here?

UPDATE

enter image description here

Its also returning with records with just "ca",

1

There are 1 answers

3
Rishabh Meshram On BEST ANSWER

Based on the provided information, the issue might be because of using same id value for both document and the later value is being update to same id.

To solve this try giving different id for each entry.

You can check the below code snippet:



# Define the index schema
index_schema = {
    "name": index_name,
    "fields": [
        {"name": "id", "type": "Edm.String", "key": True},
        {"name": "Author", "type": "Edm.String", "searchable": True, "filterable": True},
        {"name": "Text", "type": "Edm.String", "searchable": True, "filterable": True},
    ],
}

# Create the index
headers = {
    "Content-Type": "application/json",
    "api-key": api_key,
}
url = f"https://{search_service_name}.search.windows.net/indexes?api-version={api_version}"
response = requests.post(url, headers=headers, data=json.dumps(index_schema))
response.raise_for_status()

# Upload sample data
documents =[{
    "Id": "11108",    
    "Author": None,
    "Text": "ca-1000"
},
{
    "Id": "11109",    
    "Author": None,
    "Text": "da-20000"
}]

upload_data = {"value": documents}
url = f"https://{search_service_name}.search.windows.net/indexes/{index_name}/docs/index?api-version={api_version}"
response = requests.post(url, headers=headers, data=json.dumps(upload_data))
response.raise_for_status()

With above setup I was able to get required results: enter image description here