Lucene: how can I find query hit positions in original contents? - search

Suppose I have a document collection that I have indexed in Lucene. I submit a query and get hits. Now what I want is to find where in a particular document hit(s) occur(s). I know that I can use the Lucene Highlighting classes to obtain relevant fragments. But how can I find out where exactly these fragments appear in the original contents?
A related question is how to make sure the found fragments are actually very close to the original query? I noticed in my experiments with highlighting that often I would have a multi-word query and it would return fragments that would have only some of these words. But what if I want to make sure I get hits with all the words?
Thanks!

Not an actual answer, just a few links to a solution to a similar problem.
First of all, here you can see the actual results of the highlighting (note that were is highlighted though am was in the query. Stemming is an additional feature of this implementation):
http://hunglish.hu/search?huSentence=&enSentence=I%20am%20highlighted&size=20&page=2&doc.genre=-10
Here's the source. Look for these methods: highlightField, highlightBisen
http://code.google.com/p/hunglish-webapp/source/browse/trunk/src/main/java/hu/mokk/hunglish/lucene/Searcher.java
Disclaimer: I wrote this a while ago, it is not very nice code, and it is buggy in special cases: there is an open issue relating to highlighting. Furthermore, it uses version 3.2.0 of the lucene-highlighter, which is possibly not the newest.
Anyway, I hope if you look at how it works, it helps you write a better one, or at least something that works as expected.

Related

Azure Search result highlight snippets

I am using the Hit Highlighting feature in Azure Search and noticed a discrepancy in the way it behaves from the documentation. In the documentation it says that when you use hit highlighting it will return a snippet of the field with the highlight, but it always returns the entire field (with proper highlighting).
Is there a way to have Azure Search instead return just a snippet (say of about 200 characters) that includes the highlight?
Currently, the answer is no, you cannot. The field breaks according to (English) sentence rules, ie. it breaks on ".", "!", "?".
Also see this question for an example on breaking and some more info relating to the delimiters.
Depending on the nature of the field you might be able to add one of the above delimiters to 'emulate' what you want to accomplish (as suggested by Nate Ko).
I want to suggest something else on top of what Nate spoke to. When you look at the document response, also take a look at the Highlights part of the results (as opposed to the Document). For example, you might be currently getting the field results by retrieving something like this:
Results[i].Document.DESCRIPTION
If there is a highlight found for that field, the snipped will be found here:
Results[i].Highlights.DESCRIPTION
What I like to do is to first check if there is a valid Highlight and if so display it. If not, I show the actual field content.
Liam
We recently introduced a change that improves the highlighter performance on large fields and NLP experience. One side effect of the change was that the new highlighter generates snippets based on sentences, breaking the text field on '.' (period).
One way to workaround the issue is to put '.'s in the field. We are working to enforce the snippet size and let you know when it is available.

Lucene, Change Search on one file

Question about Lucene,
I have a file that I would like to index and search by different analyzers. My goal is to be able to change how I search.
In one case I would like to search exact phrase with punctuation IE. for "one,two" and only return exact matchings w/ punctuation.
I would also like to be able to search the exact phrase without punctuation. IE. for "one two." As in the StandardAnalyzer
Essentially I need to change the search functionality on one field.
How can I change the search on the same file. Ive tried using two analyzers (standard and whitespace) however this makes the indexing time very long.
My second thought is to use just a WhitespaceAnalyzer and when searching pass a query that further tokenizes each string if needed? However I am not sure which API has this if any do.
Also is there a good reading on how analyzers and tokens work and are implemented.
Thanks
What do you mean you tried two analyzers? Duplicate the content to 2 seperate fields with different analyzers? That would be my suggestion.

Exclude comments from search results in IntelliJ global search?

I found the grammar error "it's" as a possessive on one page of a large project. I'm trying to search for any other usages of this on pages to correct it, but I'm getting results containing hundreds of comments. I just want to filter for the important user-facing portions of the project. Is there a way to exclude comments from the results of a global search?
In more recent versions, at least in PyCharm 2018 (similar to IntelliJ), there is a filter option "Except comments," as shown here:
(Click the small filter icon to show the dropdown.)
Note: The selected filter option persists during a session, and the active filter option is not immediately apparent unless you open the dropdown. To prevent accidentally limiting subsequent searches, it may be a good idea to switch back to "Anywhere" afterward.
Another approach would be to enable the "Regular expression" (or "Regex") checkbox in the search dialog, then use some kind of negative lookaround to exclude comments.
In one case, I needed to exclude lines with single-line comments (e.g. # this is a comment) from a search, but not lines with inline comments (e.g. a=b+1 # this is an inline comment). The following did the trick, searching for something (for Python comments, starting with #):
^((?!#).)*something.*$
Please note I'm not a regex-expert, so this regex pattern can probably be improved upon greatly, but it illustrates the idea. You can play around with this on regex101. Any comments to improve the pattern are most welcome, of course.
Note sure if this approach could be extended to multiline comments though. (as in """ several lines here """).
It's difficult to suggest something without really looking at the code, but since it seems like a one-off thing, I would use global search just in comments to temporary replace "it's" with some #temporary-token#, then use global search everywhere, you should everything what's left. Then rollback temporary token for comments. Should be easy to try with VCS. Just an idea.
As you can see, with "Comments only" option, only one #token is found.

Misspelled, wierd split words search in XSLT & Umbraco

Is it possible in XSLT to search and find content, even though the content is misspelled or the words splitted up - even though it shouldn’t?
Example:
I need to find a webshop called bearshop.com, but I search it like this “bear shop”. This will end in a “no results”.
Another example:
I search “progresive” but the right word was “progressive”, and this will end in a “no result” as well.
The most important part is the first example, where the search can be written with or without white spaces and still find the content. Hope someone can help me or lead me in the right direction :)
Kind regards,
Niels
If you are looking for a general way of matching similar words, this is often called fuzzy search and can quite easily be done with Umbraco and Examine.
There may even is a way to use this with XSLT, though I never tested that.
Assuming XSLT/XPath 2.0 you can use //foo[matches(., 'bear\s*shop')].

Semantic difference between "Find" and "Search"?

When building an application, is there any meaningful difference between the idea of "Find" vs "Search" ? Do you think of them more or less as synonymous?
I'm asking in terms of labeling for application UI as well as API design.
Finding is the completion of searching.
If you might not succeed in finding something, call the feature "Search". For example text search in an editor can fail due to no matches - then calling it "Find" would be lying.
On the other hand: in an established job searching site, you can say "Find a PHP job" because you know that for (almost) anything your users want, there will be offerings. This also makes it sound confident, positive and energetic.
According to Steve Krug in Don't Make Me Think, when talking about usability for a publicly-facing web site, use the word Search for a search box and nothing else. (He specifically prohibits "Find", "Quick Find", "Quick Search", and all variations.)
The rationale is that "Search" is the most commonly understood term, so it's what people will look for when they aren't thinking, and you don't want your users to have to think (at all).
I would say that "find" is focused on getting a single, exact match. As in the example above, you "find" the perfect PHP job.
OTOH, you "search" for jobs that meet your criteria. Searching is what you do when you want to graze through several results. "Search" returns pages of results. "Find" is closer to "I'm feeling lucky."
Of course, the terms get used interchangeably sometimes. But, I think that's the essence of the difference.
In many applications, find means "find on the current page/screen", while search means "search the entire database/Internet." Web browsers, online help, and other applications seem to make this distinction.
Within most applications...
Find typically refers to locating text within the document at hand and jumps to the next occurrence.
Search typically refers to locating multiple documents (or other objects) and returns a list.
I wrote the built-in Find command in Acrobat 1.0 and worked on the full text Search engine for Acrobat 2.0 and 3.0.
Most software at that point that handled large amounts of text had a way to locate an exact match to a single word or phrase and called it Find/Find Next. This is what we called it in Acrobat 1.0. We knew from the start that this wasn't enough to handle entire repositories of documents, so we needed a way to scan across a whole set. We couldn't use Find since that was already in the UI and had established behavior, so we settled on Search. The decision was based on little more than the relatively small set of common words that convey the action.
Even harder is to come up with a reasonable icon for it. Our initial take was to use something similar to the old Yellow Pages logo:
(source: yellowpagecity.com)
but the lawyers shot that down - it was too close. We couldn't use a magnifying glass as we had zoom functions tied to that. We went with binoculars.
I don't think that there is any difference.
But then again, I'm Portuguese. :P
Find = Discover exact
Example: We write "Please find attached" in an email. We don't write "Please search attached".
Search = Discover exact + Related match
Example: Google Search
"Seek and ye shall find"
"Search and you will find"
One angle that (surprisingly) no one has mentioned, is that in English when you say you search something, that something is the thing you're searching within, not the thing you're trying to find. So unless you add the word 'for' (as in, to search for something), the two words are fundamentally different.
It becomes obvious with an example:
Find the room.
Search the room.
Two very different tasks! The first defines the object of your search. The second defines the scope of your search.
That's not completely irrelevant when talking about UIs. If your app has a search feature where the user can specify both the source and the object of their search, you might choose to use the words this way. For example:
Search: Current document
Find: "positive and energetic"
Yes, as some others have pointed out, the word 'Find' does imply a successful search, but let's not start calling app designers liars for using it when success isn't guaranteed. It's become a pretty standard term for searching a document for a particular string.
I think search is more generic and more suitable for text search. Find sounds more like 'find a specific record or a group of records'
After searching You find something.
Search for an answer on stackoverflow that you may find it.
For me Find is the success of a Search, that is to Find is to identify the location of something that's known to exist.
Search should always be used when you have no control on what the user is looking for.
Find talks about a specific one.
Search does not talk about a specific one.
Did you find the picture I requested yet?
No? Please search on internet. I need to present it in an hour.
Another one is below
Please find the attachment in this email.
(or)
You'll find the attachment below.
(or)
Please find attached.
here, we use find because it is a specific document which is attached to email.
we don't use the search here, as there is nothing to search in a larger domain.
Search is the primary interface to the Web for many users. Search should be global (not scoped to a subsite) and available from every page; booleans should be made intimidating since users usually use them wrong
Read this: https://www.nngroup.com/articles/search-and-you-may-find/

Resources