It is necessary to write a quick search on several columns of the table at the same time with smart sorting (each result should have its own weight, or what term is more correct here?). Example: we have a table Animals with columns (View, Name).

Животные ID Вид Имя 1 кот Пушок 2 кот Котофей 3 кот Рекс 4 пес Рекс 5 пес Пуш 

Request examples:

 "Рекс" (Id, вес): `3:1; 4:1.` "пес Рекс" (Id, вес): `3,1; 4, 2.` "кот" (Id, вес): `1:1; 2:1; 3:1` "кот пес" : `1:1, 2:1, 3:1, 4:1`. "котпес" : `null` (или все результаты, если это не сложно реализовать). "пес котофей" : `2:1`. "Рекссссс": `null` (4:1 , если это не сложно реализовать). "пуш": 1:1, 5:1 (1:0.5, 5:1 , если это не сложно реализовать). 

It is also important that everything works quickly, acceleration sops, etc.

Tell me where to look and / or a quick example, please.

UPD2 . I forgot to write, we need incomplete coincidences (it is desirable that they are more in weight than complete ones, but if this makes it difficult, even if incomplete matches weigh as complete ones). Updated the list of products above.

  • That is, the standard sorting algorithm does not suit you? Do we need to come up better than quicksort? - VladD
  • @VladD, look, please, examples, will quicksort help there? It seems to me that the problem is more in calculating weight than sorting. - Ssss
  • @Batanichek, zapdeytil question with your examples. "In your case, is weight a serial number (in search)?" - Yes. - Ssss
  • @Batanichek, yes, but it is necessary that incomplete matches are also taken into account, for example, incomplete matches can add 0.5 to the weight. Register is not important. - Ssss
  • one
    Look here, maybe this is what you are looking for? selo-blog.blogspot.de/2010/10/sql-ranking.html - Sergej Loos

1 answer 1

Not exactly the answer, but I think this should help you solve the problem yourself:

0) Let initially we have an array of Id, Weight, where the weight everywhere is 0

1) Break the string into words using String.Split

2) For each word, we go through the whole array and increase the weight (for example)

 if(str[i]==value){ вес[i]=вес[i]+1 }else if(str[i].Contains(value)){ вес[i]=вес[i]+0.5 } 

3) Sort by descending weight and remove those where weight is 0

 result.where(t=>t.вес>0).OrderByDescending(x => x.вес); 

I’m not sure about the speed (you can try to do weight calculations in LINQ . And perhaps by syntax you may have written something wrong (there is no possibility to check it))

  • The solution is good, of course, but in the forehead. Performance and indeed the ability to use on big data - xs. - Ssss
  • On the SQL side, this of course should be faster. You can simplify your task and in C # handle an array already in which there is at least one inclusion, for example, who so - Batanichek