Пример #1
0
 def test_search_bad_argument(self):
     client = SearchClient("endpoint", "index name", CREDENTIAL)
     with pytest.raises(TypeError) as e:
         client.search(10)
         assert str(
             e) == "Expected a SuggestQuery for 'query', but got {}".format(
                 repr(10))
Пример #2
0
    def test_get_search_simple(self, api_key, endpoint, index_name, **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))
        results = list(client.search(search_text="hotel"))
        assert len(results) == 7

        results = list(client.search(search_text="motel"))
        assert len(results) == 2
Пример #3
0
    def test_get_search_counts(self, api_key, endpoint, index_name, **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))

        results = client.search(search_text="hotel")
        assert results.get_count() is None

        results = client.search(search_text="hotel", include_total_count=True)
        assert results.get_count() == 7
Пример #4
0
    def test_get_search_coverage(self, api_key, endpoint, index_name,
                                 **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))

        results = client.search(search_text="hotel")
        assert results.get_coverage() is None

        results = client.search(search_text="hotel", minimum_coverage=50.0)
        cov = results.get_coverage()
        assert isinstance(cov, float)
        assert cov >= 50.0
Пример #5
0
def search(rootdir, Query, index_name, endpoint, key, fnames, vertical):
    field_names = ['FileName', 'FilePath', 'Score']
    # Create a client
    credential = AzureKeyCredential(key)
    client = SearchClient(endpoint=endpoint,
                          index_name=index_name,
                          credential=credential)
    results = client.search(search_text=Query)
    df = pd.read_csv('TrackerFiles/{}.csv'.format(vertical))
    r = []

    for result in results:
        if result['FileName'] in fnames:
            d = {
                'FileName': result['FileName'],
                'FilePath': result['FilePath'],
                'Score': result['@search.score']
            }
            r.append(d)
            if df.loc[df['FileName'] == str(result['FileName']),
                      'Intent'].isnull().any():
                df.loc[df['FileName'] == str(result['FileName']),
                       'Intent'] = str(Query)
            else:
                df.loc[df['FileName'] == str(result['FileName']),
                       'Intent'] = df.loc[df['FileName'] ==
                                          str(result['FileName']),
                                          'Intent'] + "," + str(Query)
    df.to_csv('TrackerFiles/{}.csv'.format(vertical), index=False)
    with open(rootdir + 'result.csv', 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=field_names)
        writer.writeheader()
        writer.writerows(r)

    return rootdir + 'result.csv'
Пример #6
0
def search_query(search_text, filter_by=None, sort_order=None):
    try:

        # Create a search client
        azure_credential = AzureKeyCredential(search_key)
        search_client = SearchClient(search_endpoint, search_index,
                                     azure_credential)

        # Submit search query
        results = search_client.search(
            search_text,
            search_mode="all",
            include_total_count=True,
            filter=filter_by,
            order_by=sort_order,
            facets=['metadata_author'],
            highlight_fields='merged_content-3,imageCaption-3',
            select=
            "url,metadata_storage_name,metadata_author,metadata_storage_size,"
            "metadata_storage_last_modified,language,sentiment,merged_content,"
            "keyphrases,locations,imageTags,imageCaption")
        return results

    except Exception as ex:
        raise ex
class SearchDocumentsTest(PerfStressTest):
    def __init__(self, arguments):
        super().__init__(arguments)
        api_key = self.get_from_env("AZURE_SEARCH_API_KEY")
        service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
        index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
        key = os.getenv("AZURE_SEARCH_API_KEY")
        self.service_client = SyncClient(service_endpoint, index_name,
                                         AzureKeyCredential(api_key))
        self.async_service_client = AsyncClient(service_endpoint, index_name,
                                                AzureKeyCredential(api_key))

    @staticmethod
    def add_arguments(parser):
        super(SearchDocumentsTest, SearchDocumentsTest).add_arguments(parser)
        parser.add_argument(
            '--num-documents',
            nargs='?',
            type=int,
            help='The number of results expect to be returned.',
            default=-1)

    async def global_setup(self):
        await super().global_setup()

    async def close(self):
        await self.async_service_client.close()
        await super().close()

    def run_sync(self):
        if self.args.num_documents == -1:
            results = len(self.service_client.search(search_text="luxury"))
        else:
            results = len(
                self.service_client.search(search_text="luxury",
                                           top=self.args.num_documents))

    async def run_async(self):
        if self.args.num_documents == -1:
            results = await self.async_service_client.search(
                search_text="luxury")
        else:
            results = await self.async_service_client.search(
                search_text="luxury", top=self.args.num_documents)
        count = 0
        async for result in results:
            count += count
Пример #8
0
    def test_get_search_facets_none(self, api_key, endpoint, index_name,
                                    **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))

        select = ("hotelName", "category", "description")
        results = client.search(search_text="WiFi", select=",".join(select))
        assert results.get_facets() is None
Пример #9
0
def simple_text_query():
    # [START simple_query]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient

    search_client = SearchClient(service_endpoint, index_name,
                                 AzureKeyCredential(key))

    results = search_client.search(query="spa")

    print("Hotels containing 'spa' in the name (or other fields):")
    for result in results:
        print("    Name: {} (rating {})".format(result["HotelName"],
                                                result["Rating"]))
Пример #10
0
 def test_get_count_reset_continuation_token(self, mock_search_post):
     client = SearchClient("endpoint", "index name", CREDENTIAL)
     result = client.search(search_text="search text")
     assert isinstance(result, ItemPaged)
     assert result._page_iterator_class is SearchPageIterator
     search_result = SearchDocumentsResult()
     search_result.results = [
         SearchResult(additional_properties={"key": "val"})
     ]
     mock_search_post.return_value = search_result
     result.__next__()
     result._first_page_iterator_instance.continuation_token = "fake token"
     result.get_count()
     assert not result._first_page_iterator_instance.continuation_token
    def test_get_search_facets_result(self, api_key, endpoint, index_name, **kwargs):
        client = SearchClient(
            endpoint, index_name, AzureKeyCredential(api_key)
        )

        query = SearchQuery(search_text="WiFi", facets=["category"])
        query.select("hotelName", "category", "description")

        results = client.search(query=query)
        assert results.get_facets() == {
            "category": [
                {"value": "Budget", "count": 4},
                {"value": "Luxury", "count": 1},
            ]
        }
def filter_query():
    # [START facet_query]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient

    search_client = SearchClient(service_endpoint, index_name,
                                 AzureKeyCredential(key))

    results = search_client.search(
        search_text="WiFi", facets=["Category,count:3", "ParkingIncluded"])

    facets = results.get_facets()

    print("Catgory facet counts for hotels:")
    for facet in facets["Category"]:
        print("    {}".format(facet))
Пример #13
0
 def test_search_query_argument(self, mock_search_post):
     client = SearchClient("endpoint", "index name", CREDENTIAL)
     result = client.search(search_text="search text")
     assert isinstance(result, ItemPaged)
     assert result._page_iterator_class is SearchPageIterator
     search_result = SearchDocumentsResult()
     search_result.results = [
         SearchResult(additional_properties={"key": "val"})
     ]
     mock_search_post.return_value = search_result
     assert not mock_search_post.called
     next(result)
     assert mock_search_post.called
     assert mock_search_post.call_args[0] == ()
     assert (mock_search_post.call_args[1]["search_request"].search_text ==
             "search text")
Пример #14
0
def filter_query():
    # [START filter_query]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient
    from azure.search.documents.models import SearchQuery

    search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

    query = SearchQuery(search_text="WiFi")
    query.filter("Address/StateProvince eq 'FL' and Address/Country eq 'USA'")
    query.select("HotelName", "Rating")
    query.order_by("Rating desc")

    results = search_client.search(query=query)

    print("Florida hotels containing 'WiFi', sorted by Rating:")
    for result in results:
        print("    Name: {} (rating {})".format(result["HotelName"], result["Rating"]))
def semantic_ranking():
    # [START semantic_ranking]
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents import SearchClient

    endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
    index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
    api_key = os.getenv("AZURE_SEARCH_API_KEY")

    credential = AzureKeyCredential(api_key)
    client = SearchClient(endpoint=endpoint,
                          index_name=index_name,
                          credential=credential)
    results = list(
        client.search(search_text="luxury",
                      query_type="semantic",
                      query_language="en-us"))

    for result in results:
        print("{}\n{}\n)".format(result["HotelId"], result["HotelName"]))
Пример #16
0
    def test_get_search_filter(self, api_key, endpoint, index_name, **kwargs):
        client = SearchClient(endpoint, index_name,
                              AzureKeyCredential(api_key))

        select = ("hotelName", "category", "description")
        results = list(
            client.search(search_text="WiFi",
                          filter="category eq 'Budget'",
                          select=",".join(select),
                          order_by="hotelName desc"))
        assert [x["hotelName"]
                for x in results] == sorted([x["hotelName"] for x in results],
                                            reverse=True)
        expected = {
            "category",
            "hotelName",
            "description",
            "@search.score",
            "@search.highlights",
        }
        assert all(set(x) == expected for x in results)
        assert all(x["category"] == "Budget" for x in results)
Пример #17
0
def query_func(search_query):
    
    endpoint = "https://chatbotsearchservice.search.windows.net/"
    admin_key = "4CF0B5C911347AE7BBCBF5B3B8D9212C"
    index_name = "hotels-sample-index"
    
    search_client = SearchClient(endpoint=endpoint, index_name=index_name, credential=AzureKeyCredential(admin_key))

    features = {
        "search_fields":"HotelName",
        "select":"HotelName, Rating",
        "filter":"Rating gt 4",
        "order_by":"Rating desc"}

    query_result = search_client.search(
                            include_total_count=True,
                            search_text=f"{search_query}", 
                            search_fields=f"{features['search_fields']}", 
                            select=f"{features['select']}", 
                            filter=f"{features['filter']}", 
                            order_by=f"{features['order_by']}")

    return query_result
Пример #18
0
search_index = os.getenv(
    "AZURE_SEARCH_INDEX_NAME")  # Feel free to use > 1 and switch them out
search_api_key = os.getenv("AZURE_SEARCH_ADMIN_API_KEY")

# UI

title = os.getenv("APPLICATION_NAME")
st.header(title)

q = st.text_input("Search query", "Type your search query here")
st.write("The current search query is:", q)

# Query processing

search_client = SearchClient(
    endpoint=search_endpoint,
    index_name=search_index,
    credential=AzureKeyCredential(search_api_key),
)

results = search_client.search(search_text=q, include_total_count=True)

st.text(f"Total Documents Matching Query: {results.get_count()}")

st.subheader("Results found:")

# print(type(results)) # spoiler: custom Azure Search pagedItem object
for result in results:
    # print(result.keys())  # Inspect what you can display
    st.text(result[str(list(result.keys())[0])])  # Show first available item