I try to build a simple filename search. I want that the user can search
for any part of the name.
Let's say the following filenames are indexed:
Then the user might search for:
"ile_20" (finds the first six documents)
"12.txt" (finds 1, 5, 6)
"12" followed by "01" (finds 1, 2, 3 - NOT 7)
"2012" followed by "01" (finds 1, 2, 3 - NOT 7)
(Note: Yes, the user might really search for strings like "ile_20" ... e.g.
because of copy-and-paste mistakes )
Therefore I use a nGram-tokenizer to index each possible input-string. This
works fine so far.
To support the "followed by"-search mentioned above I need a query that
respects the order of the terms, no matter how many text is between these
two terms (okay let's say max. 100 characters ).
Since a "text_phrase"-query with a "slop" does not respect the ordering of
the terms correctly, I decided to use a "span_near" query. This works fine
in most cases.
See here my full example-index: https://gist.github.com/3487909
As mentioned in the example above the query "'2012' followed by '01'" does
not work since the nGram tokenizer generates a position-value for each
token that is not very useful when used by the "span_near" query. While
indexing, the term "2012" is assigned to a position value (50) which is
bigger than the position value for the term "01" (e.g. 10). Since 50 and 10
are not in order the query will have no results. The in-order-thing works
only correct for terms which have the same length (e.g. "'12' followed by
'01'") or if the terms are ordered by length (e.g. "'20' followed by
So how can I achieve the correct search-behaviour? I just want the ability
to search for any part(s) of the filename while respecting the order of the
Maybe there is a way to tell "span_near" to not use the position but
instead the "start_offset"?
Or is there another query I can use?