Questions tagged [search]

Questions about search algorithm mechanics and implementation. *NOT* for questions about using search tools within an API (e.g. Google, Bing, Facebook).

Searching is one of the most common and important tasks in Computer Science.

The most basic search algorithm is a (also called "sequential search"). Each item in a collection of data is examined in sequence until the sought item is found.

If the collection in question has already been sorted, for instance, then a more efficient is possible.

Search can become more complex when, rather than finding one instance of a specific item, we want to find all items meeting a certain set of criteria. For instance a query can specify extremely complex search criteria, and much of relational database design involves the planning of an efficient way to perform those searches.

Another more complex search scenario is graph search, in which a series of nodes and edges must be traversed. Common algorithms for this domain include and . In many cases, a heuristic search algorithm such as can achieve more efficient results by making use of additional information about the problem.


Do not use this tag for questions about specific search tools within an API. Some more relevant tags for that would be the following:

37207 questions
4123
votes
13 answers

grep: show lines surrounding each match

How do I grep and show the preceding and following 5 lines surrounding each matched line?
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
2007
votes
16 answers

How to do case insensitive search in Vim

I'd like to search for an upper case word, for example COPYRIGHT in a file. I tried performing a search like: /copyright/i # Doesn't work but it doesn't work. I know that in Perl, if I give the i flag into a regex it will turn the regex into a…
Haiyuan Zhang
  • 40,802
  • 41
  • 107
  • 134
923
votes
22 answers

Use grep --exclude/--include syntax to not grep through certain files

I'm looking for the string foo= in text files in a directory tree. It's on a common Linux machine, I have bash shell: grep -ircl "foo=" * In the directories are also many binary files which match "foo=". As these results are not relevant and slow…
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
822
votes
16 answers

How to combine multiple QuerySets in Django?

I'm trying to build the search for a Django site I am building, and in that search, I am searching across three different models. And to get pagination on the search result list, I would like to use a generic object_list view to display the results.…
espenhogbakk
  • 11,508
  • 9
  • 39
  • 38
822
votes
9 answers

How to grep commits based on a certain string?

In a Git code repository I want to list all commits that contain a certain word. I tried this git log -p | grep --context=4 "word" but it does not necessarily give me back the filename (unless it's less that five lines away from the word I searched…
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
767
votes
25 answers

Search a list of dictionaries in Python

Given: [ {"name": "Tom", "age": 10}, {"name": "Mark", "age": 5}, {"name": "Pam", "age": 7} ] How do I search by name == "Pam" to retrieve the corresponding dictionary below? {"name": "Pam", "age": 7}
Hellnar
  • 62,315
  • 79
  • 204
  • 279
765
votes
12 answers

Solr vs. ElasticSearch

What are the core architectural differences between these technologies? Also, what use cases are generally more appropriate for each?
Ben ODay
  • 20,784
  • 9
  • 45
  • 68
723
votes
38 answers

Generate an integer that is not among four billion given ones

I have been given this interview question: Given an input file with four billion integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1 GB memory. Follow up with what you would do if you have…
SecureFish
  • 2,391
  • 7
  • 32
  • 43
675
votes
10 answers

What is the difference between re.search and re.match?

What is the difference between the search() and match() functions in the Python re module? I've read the Python 2 documentation (Python 3 documentation), but I never seem to remember it.
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
648
votes
9 answers

How can I exclude one word with grep?

I need something like: grep ^"unwanted_word"XXXXXXXX
john
  • 6,515
  • 3
  • 15
  • 4
646
votes
15 answers

Search all the occurrences of a string in the entire project in Android Studio

I've just started using Android Studio (IntelliJ), and I now look for the feature to find the occurrence of a string in any of the files in my project. For example: I want to find all the files that contain the string ".getUuid()" The search at the…
kramer65
  • 50,427
  • 120
  • 308
  • 488
607
votes
6 answers

Is there a short contains function for lists?

Given a list xs and a value item, how can I check whether xs contains item (i.e., if any of the elements of xs is equal to item)? Is there something like xs.contains(item)? For performance considerations, see Fastest way to check if a value exists…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
569
votes
7 answers

How to design RESTful search/filtering?

I'm currently designing and implementing a RESTful API in PHP. However, I have been unsuccessful implementing my initial design. GET /users # list of users GET /user/1 # get user with id 1 POST /user # create new user PUT /user/1 # modify user with…
Erik B
  • 40,889
  • 25
  • 119
  • 135
549
votes
7 answers

How to find the Git commit that introduced a string in any branch?

I want to be able to find a certain string which was introduced in any commit in any branch, how can I do that? I found something (that I modified for Win32), but git whatchanged doesn't seem to be looking into the different branches (ignore the…
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
505
votes
7 answers

Find a file by name in Visual Studio Code

How can I find a file by name in Visual Studio Code? A Visual Studio shortcut I'm used to is CTRL+,, but it does not work here.
Nenad
  • 24,809
  • 11
  • 75
  • 93
1
2 3
99 100