How to search result from azure search service from filed to filed

29 Views Asked by At

I have two fields in azure index one is Address and other field is Mobileno filed i want to see result from index address filed contains mobile no

i want to is match mobile no from address: "search": "search.ismatch('MobileNo','Address')",

1

There are 1 best solutions below

0
Rishabh Meshram On

Unfortunately, Azure Cognitive Search does not support directly using the value of one field as an input to another field's search expression. However, you can achieve a similar effect by using filters or adding specific patterns to your search query.

I have created a sample index with below schema:

fields=[  
        {"name": "id", "type": SearchFieldDataType.String, "key": True, "searchable": False},  
        {"name": "Address", "type": SearchFieldDataType.String, "searchable": True},  
        {"name": "MobileNo", "type": SearchFieldDataType.String, "searchable": True},  
       ]  

And below sample data

sample_data = [  
    {"id": "1", "Address": "123 Main St, New York, NY 10001, +11234567890", "MobileNo": "+11234567890"},  
    {"id": "2", "Address": "456 Elm St, San Francisco, CA 94107, +12223334444", "MobileNo": "+12223334444"},  
    {"id": "3", "Address": "789 Oak Ave, Los Angeles, CA 90001, +13334445555", "MobileNo": "+13334445555"}, 
    {"id": "4", "Address": "123 Main St, New York, NY 10001, +11234567890", "MobileNo": "+12223334444"}, 
] 

You can use below query to search with same or different value of MobileNo in Address and MobileNo fields.

valueInAddress = '+12223334444'  
valueInMobile = '+12223334444'  
  
# Create the filter expression string  
filter_expression = f"search.ismatch('{valueInAddress}', 'Address')"  
  
results = search_client.search(  
    search_text=valueInMobile,  
    filter=filter_expression,  
    query_type="full",  
    search_mode="all",  
    search_fields=["MobileNo"]  
)  
  
for result in results:  
    print(result)  

Below is an example output with different value of Mobile Number in Address and MobileNo field:

enter image description here

you can modify the above sample query based on your requirement and for more details you can check this documentation.