Search code examples
pythonazureazure-cognitive-searchazure-search-.net-sdk

Azure Search on Complex Field


I have an index created with these fields:

fields = [
        SimpleField(name="key", type=SearchFieldDataType.String, key=True),

        SearchableField(name="type", type=SearchFieldDataType.String, sortable=True),
        # SearchableField(name="unitRef", type=SearchFieldDataType.String, analyzer_name="en.lucene"),
        
        ComplexField(name="period", fields=[
            SimpleField(name="startDate", type=SearchFieldDataType.DateTimeOffset, facetable=True, filterable=True, sortable=True),
            SimpleField(name="endDate", type=SearchFieldDataType.DateTimeOffset, facetable=True, filterable=True, sortable=True)
      
        ]),
        # ComplexField(name="segment", fields=[
        #     SearchableField(name="dimension", type=SearchFieldDataType.String),
        #     SearchableField(name="value", type=SearchFieldDataType.String)
      
        # ]),

        SimpleField(name="value", type=SearchFieldDataType.Double, facetable=True, filterable=True, sortable=True)

        #  ComplexField(name="segment", fields=[
        #     SearchableField(name="dimension", type=SearchFieldDataType.String),
        #     SearchableField(name="value", type=SearchFieldDataType.String, facetable=True, filterable=True, sortable=True)
      
        # ]),


    ]


I want to filter my peroid within a certain date, I am using this code but it's not working:


results = search_client.search(search_text="RevenueFromContractWithCustomerExcludingAssessedTax", select='type,period,value',filter="period ge 2020-01-01 and period le 2021-12-31", include_total_count=True)

print ('Total Documents Matching Query:', results.get_count())
for result in results:
    print("type: {}, period: {}, value: {}".format(result["type"],result["period"],result["value"]))
    dat= ("type: {}, period: {}, value: {}".format(result["type"],result["period"],result["value"]))
    # print(result)


Would appreciate the feedback, thanks!


Solution

  • try this:

    results = search_client.search(
        search_text="RevenueFromContractWithCustomerExcludingAssessedTax",
        select='type,period,value',
        filter="period/any(p: p/startDate ge 2020-01-01 and p/endDate le 2021-12-31)",
        include_total_count=True
    )
    
    
    print ('Total Documents Matching Query:', results.get_count())
    for result in results:
        print("type: {}, period: {}, value: {}".format(result["type"],result["period"],result["value"]))
        dat= ("type: {}, period: {}, value: {}".format(result["type"],result["period"],result["value"]))
        # print(result)