Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search based on a set of keywords

I need to make a search based on a set of keywords, that return all the Ads related with those keywords. Then the result is a list of Categories with the Ads Count for each Category.

The search is made in a KeywordSearch Table:

public class KeywordSearch
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Keyword Keyword { get; set; }
}

Where the Keyword Table is:

public class Keyword
{
    public int Id { get; set; }
    public string Name { get; set; }
}

The Ads are related with the Keywords using the following Table:

public class KeywordAdCategory
{
    [Key]
    [Column("Keyword_Id", Order = 0)]
    public int Keyword_Id { get; set; }

    [Key]
    [Column("Ad_Id", Order = 1)]
    public int Ad_Id { get; set; }

    [Key]
    [Column("Category_Id", Order = 2)]
    public int Category_Id { get; set; }
}

Finally, the Category table:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Example:

  • Keywords: "Mercedes-Benz" and "GLK"
  • KeywordSearch: "Mercedes" and "Benz" for the Keyword "Mercedes-Benz" "GLK" for the Keyword "GLK"
  • Category: "Cars" and "Trucks"
  • Ads: Car - Mercedes-Benz GLK Truck - Mercedes-Benz Citan

    If I search "Mercedes-Benz" I get:

    • Cars: 1
    • Trucks: 1

    If I search "Mercedes-Benz GLK" I get:

    • Cars: 1

    If I search "Mercedes Citan" I get:

    • Trucks: 1

What I get until now:

var keywordIds = from k in keywordSearchQuery
                    where splitKeywords.Contains(k.Name)
                    select k.Keyword.Id;

var matchingKac = from kac in keywordAdCategoryQuery
                    where keywordIds.Distinct().Contains(kac.Keyword_Id)
                    select kac;

var addIDs = from kac in matchingKac
             group kac by kac.Ad_Id into d
             where d.Count() == splitKeywords.Count()
             select d.Key;

var groupedKac = from kac in keywordAdCategoryQuery
                    where addIDs.Contains(kac.Ad_Id)               <--- EDIT2
                    group kac by new { kac.Category_Id, kac.Ad_Id };

var result = from grp in groupedKac
                group grp by grp.Key.Category_Id into final
                join c in categoryQuery on final.Key equals c.Id
                select new CategoryGetAllBySearchDto
                {
                    Id = final.Key,
                    Name = c.Name,
                    ListController = c.ListController,
                    ListAction = c.ListAction,
                    SearchCount = final.Count()
                };

The problem is that I can't get only the Ads that match all Keywords.

EDIT:

When a keyword is made of 2 or more KeywordSearches like "Mercedes-Benz", the line "where d.Count() == splitKeywords.Count()" fails, because d.count = 1 and splitkeywords.Count = 2 for "Mercedes-Benz"

Any Help?

like image 289
Patrick Avatar asked Nov 05 '13 18:11

Patrick


People also ask

What is keyword based search?

Keyword research is the process of finding and analyzing search terms that people enter into search engines with the goal of using that data for a specific purpose, often for search engine optimization (SEO) or general marketing.

How do you search multiple keywords at once?

Search for multiple items You can enter more than one query into Google at a time to view all options. Simply key on “OR” between the terms.

How do I search using keywords?

To search by keyword, select Keyword from the search options and type the word(s) you wish to search. Keyword searches can retrieve a large number of results. Several options are available to help refine your search and results. Quick Limits can be used when doing a keyword search.

What is keyword search algorithm?

Abstract: Search engines prominently use inverted indexing technique to locate the Web pages having the keyword contained in the users query. The performance of inverted index, fundamentally, depends upon the searching of keyword in the list maintained by search engines.


1 Answers

this may not be the direct answer, but in such "multiple parameter search" situations i just forget about anything and do the simple thing, for ex: Search By Car Manufacturer, CategoryId, MillageMax, Price :

var searchResults = from c in carDb.Cars
where (c.Manufacturer.Contains(Manufacturer) || Manufacturer == null) &&
                 (c.CategoryId == CategoryId || CategoryId == null) &&
                    (c.Millage <= MillageMax || MillageMax== null) &&
                          (c.Price <= Price  || Price == null) 
select c

now if any of the parameters is null it cancels the containing line by making the whole expression in brackets True and so it does not take a part in search any more

like image 108
m4ngl3r Avatar answered Sep 29 '22 18:09

m4ngl3r