0

i am using a azure search, and i have a console app with the code as below, which is working fine.

        DocumentSearchResult<Hotel> results;
        Console.WriteLine("Search started\n"); 
        results = indexClient.Documents.Search<Hotel>("smart", new SearchParameters { Top=5 });
        WriteDocuments(results);

currently its searching a text with word "smart". this is straight forword, what i need is i have several fields in the table, i want to search based on the feild .

for example let i have two fields 1)Title 2)SoldDate

I have to write code for finding items which has title 'john' and which has a sold date < current date.

what should i do to achieve this?

hilda_sonica_vish
  • 727
  • 3
  • 10
  • 31

1 Answers1

2

You can achieve what you want with search and a filter:

// Approach #1
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
    Filter = "soldDate lt " + currentDate,
    Top = 5
}

results = indexClient.Documents.Search<Hotel>("john", parameters);

This will filter the documents to only those with a soldDate before currentDate, and then searches the filtered documents such that documents match if any of the searchable fields contain "john". You can narrow this down to just the title field like this:

// Approach #2
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
    Filter = "soldDate lt " + currentDate,
    SearchFields = new[] { "title" },
    Top = 5
}

results = indexClient.Documents.Search<Hotel>("john", parameters);

Or like this:

// Approach #3
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
    Filter = "soldDate lt " + currentDate,
    QueryType = QueryType.Full,
    Top = 5
}

results = indexClient.Documents.Search<Hotel>("title:john", parameters);

Which way you use depends on whether you want all search terms to be limited to a specific set of fields (Approach #2), or if you want specific terms to match specific fields (Approach #3).

The reference for SearchParameters is on learn.microsoft.com.

Bruce Johnston
  • 8,344
  • 3
  • 32
  • 42
  • what should i have to give, if i have a field 'model' and i need to search whether model contains 100 (Like query) ? – hilda_sonica_vish Apr 20 '17 at 05:07
  • what should i do to search tilte="john" and job="engineer" ? basically both should be like query – hilda_sonica_vish Apr 20 '17 at 12:49
  • @hildasonica You've asked this question elsewhere, so I will try to answer it there: http://stackoverflow.com/questions/43510832/how-to-set-a-query-for-like-query-in-azure-search-query-syntax – Bruce Johnston Apr 20 '17 at 15:45
  • you didint answer it yet. im waiting for your answer – hilda_sonica_vish Apr 21 '17 at 06:00
  • @hildasonica Now that I understand your other question, it seems it's already been answered elsewhere: http://stackoverflow.com/questions/40893802/azure-search-find-matches-within-a-word-like-contains – Bruce Johnston Apr 21 '17 at 15:37
  • its not what i expect, i am not looking for matching 2 terms, i want to do filter. i should get results which matches both condition eg: search result should have Title field with term " john" as well as Job field should contain "Engineer ". – hilda_sonica_vish Apr 24 '17 at 06:34
  • "Filter" in Azure Search means strictly matching values. For strings, this means a match is based on case-sensitive equality. This is not what you want since you're looking for "contains"-like behavior. The other answers I have pointed you to explain how to get "contains"-like behavior; Now it sounds like you need to combine different search expressions. You can do this using the AND operator. Please read about the full Lucene query syntax here: https://learn.microsoft.com/rest/api/searchservice/lucene-query-syntax-in-azure-search – Bruce Johnston Apr 24 '17 at 19:53
  • 2
    @hildasonica Also, please try to avoid asking different questions in the comments as it makes it more difficult for other Stack Overflow users to find the answers. Thanks – Bruce Johnston Apr 24 '17 at 19:54
  • I can see that when I add a date comparison in the filter section it works, however not in the search section. Any suggestions on how I can boost for certain date ranges? – nmishr Apr 05 '18 at 18:46
  • @nmishr Answered here: https://stackoverflow.com/questions/49680139/boosting-docs-based-on-date-range – Bruce Johnston Apr 06 '18 at 00:12