Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.61k stars 2.82k forks source link

Azure Cognitive Search's search api has different shape than .NET/JS equivalent api #18508

Closed diberry closed 3 years ago

diberry commented 3 years ago

Describe the bug The search api's results for python has a shape of:

{
        "authors": [
            "Ken Follett"
        ],
        "work_ratings_count": 11467,
        "goodreads_book_id": 5052,
        "average_rating": 3.64,
        "original_publication_year": 2000,
        "books_count": 87,
        "ratings_5": 1988,
        "work_id": 2879976,
        "ratings_4": 4531,
        "image_url": "https://images.gr-assets.com/books/1309209804m/5052.jpg",
        "ratings_1": 151,
        "isbn": "451216725",
        "language_code": null,
        "best_book_id": 5052,
        "ratings_count": 9071,
        "original_title": "Code to Zero",
        "small_image_url": "https://images.gr-assets.com/books/1309209804s/5052.jpg",
        "work_text_reviews_count": 666,
        "title": "Code to Zero",
        "id": "9454",
        "ratings_2": 848,
        "isbn13": "9.78045121672e+12",
        "ratings_3": 3949,
        "@search.score": 12.937481,
        "@search.highlights": null
    }

The equivalent api for JS/.NET has the shape of:

{
      score: 12.937481,
      highlights: undefined,
      document: {
        id: '9454',
        goodreads_book_id: 5052,
        best_book_id: 5052,
        work_id: 2879976,
        books_count: 87,
        isbn: '451216725',
        isbn13: '9.78045121672e+12',
        authors: [Array],
        original_publication_year: 2000,
        original_title: 'Code to Zero',
        title: 'Code to Zero',
        language_code: null,
        average_rating: 3.64,
        ratings_count: 9071,
        work_ratings_count: 11467,
        work_text_reviews_count: 666,
        ratings_1: 151,
        ratings_2: 848,
        ratings_3: 3949,
        ratings_4: 4531,
        ratings_5: 1988,
        image_url: 'https://images.gr-assets.com/books/1309209804m/5052.jpg',
        small_image_url: 'https://images.gr-assets.com/books/1309209804s/5052.jpg'    
      }
    }

To Reproduce Steps to reproduce the behavior:

  1. Run js

    const { SearchClient, AzureKeyCredential } = require("@azure/search-documents");
    
    const endpoint = 'https://diberry-search.search.windows.net'
    const key = '...'
    const index_name = 'good-books'
    
    // Create a SearchClient to send queries
    const client = new SearchClient(
        endpoint,
        index_name,
        new AzureKeyCredential(key)
    );
    
    const search = async (q, top, skip) => {
    
        // Creating SearchOptions for query
        let searchOptions = {
            top: top,
            skip: skip,
            includeTotalCount: true
        };
    
        // Sending the search request
        const searchResults = await client.search(q, searchOptions);
    
        const output = [];
        for await (const result of searchResults.results) {
            output.push(result);
        }
    
        return output;
    };
    
    search("code", 10, 0).then(results => {
        console.log(results)
    }).catch(err => console.log(err));
  2. Run PY equivalent

    import logging
    import json
    
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient
    from azure.search.documents.indexes import SearchIndexClient
    from azure.search.documents.indexes.models import SearchIndex
    
    # Get the service endpoint and API key from the environment
    endpoint = 'https://diberry-search.search.windows.net'
    key = '...'
    
    # Your index name
    index_name = 'good-books'
    
    # Create Azure SDK client
    search_client = SearchClient(endpoint, index_name, AzureKeyCredential(key))
    
    def search(q, top, skip):
        returnedDocuments = search_client.search(search_text=q, top=top,skip=skip,include_total_count=True)
    
        return json.dumps(list(returnedDocuments))
    
    docs = search("code", 10, 0)
    print(docs)
  3. Compare output, each individual document has a different shape in python with equivalent default params when compared to .NET and JS.

  4. The shape is important so that tuturials and quickstarts can use the same code after the results are received.

Expected behavior Same result shape across SDK languages.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context If you want my key, just catch me on teams.

xiangyan99 commented 3 years ago

Took offline. This is by design.