#
# Part of the ht://Dig package   <http://www.htdig.org/>
# Copyright (c) 2001 The ht://Dig Group
# For copyright details, see the file COPYING in your distribution
# or the GNU General Public License version 2 or later
# <http://www.gnu.org/copyleft/gpl.html>
#
# $Id: t_search,v 1.4 2001/06/29 11:37:25 loic Exp $
#

. ../test/test_functions

export MIFLUZ_CONFIG ; MIFLUZ_CONFIG=${srcdir}/mifluz-search.conf

 [ "$VERBOSE" ] && echo "../htdb/mifluzload $VERBOSE test < $srcdir/search.txt" >&2
../htdb/mifluzload $VERBOSE test < $srcdir/search.txt

#
# Test the query parser
#

#
# Run $1 and expect $2 as a result
#
runparser() {
  command="$1"
  expected="$2"
  [ "$VERBOSE" ] && echo "$command" >&2
  out=`eval "$command"`
  if [ "$expected" != "$out" ]
  then
    echo "running $command: expected 
$expected
but got
$out"
    exit 1
  fi
}

#
# Simple test
#
runparser "./mifluzsearch -B test -n -f '+the +world' $VERBOSE" \
'( optional "" ( mandatory "" the  ) ( mandatory "" world  )  )'

#
# All boolean constructions
#
runparser "./mifluzsearch -B test -n -f '-the world +is coming -to an end'" \
'( optional "" ( not "" the  ) world ( mandatory "" is  ) coming ( not "" to  ) an end  )'

#
# Single word
#
runparser "./mifluzsearch -B test -n -f 'the'" \
'the'

#
# Single mandatory word
#
runparser "./mifluzsearch -B test -n -f '+the'" \
'( mandatory "" the  )'

#
# Single forbiden word
#
runparser "./mifluzsearch -B test -n -f '-the'" \
'( not "" the  )'


rm -f Cmifluz_*
#
# Run queries with various operators on an index built from the content
# of search.txt.
#
#
# Run $1 and expect $2 as a result (all lines starting with match:)
#
runsearch() {
  command="$1"
  expected="$2"
  [ "$VERBOSE" ] && echo "$command" >&2 
  out=`eval "$command"`
  match=`echo "$out" | grep '^match:'`
  count=`echo "$out" | sed -n -e 's/^count: *//p'`
  base=`echo "$out" | sed -n -e 's/^base: *//p'`
  if [ "$expected" != "$match" ]
  then
    echo "running $command: expected 
$expected
but got
$match"
    exit 1
  fi
}

#
# Single forbiden word is equivalent to searching nothing (discarded)
#
runsearch "./mifluzsearch -B test -f '-the' $VERBOSE" \
'match: none'

#
# Single mandatory word is exactly the same as word alone,
# the optimizer takes care of this.
#
runsearch "./mifluzsearch -B test -c 1 -f '+lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	1	11	<UNDEF>	'

#
# Test context restoration on WordTreeLiteral
#
runsearch "./mifluzsearch -B test -c 1 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	1	11	<UNDEF>	'

runsearch "./mifluzsearch -B test -c 1 -d 1 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	1	21	<UNDEF>	'

runsearch "./mifluzsearch -B test -c 1 -d 2 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	5	9	<UNDEF>	'

#
# Test retrieval based on cache (previous searches filled 3 slots in the cache)
#
runsearch "./mifluzsearch -B test -c 1 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	1	11	<UNDEF>	'

#
# Only read one more record
#
runsearch "./mifluzsearch -B test -c 2 -d 2 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	5	9	<UNDEF>	
match: <UNDEF>	<UNDEF>	5	21	<UNDEF>	'

#
# Read all possible records. This implies to read the first 4 records
# from the cache (SearchFromCache) and complete the set of match with 
# the last 3 (SearchFromIndex) despite the fact that the desired start
# of search is at document 200.
#
runsearch "./mifluzsearch -B test -c 100 -d 200 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	1	11	<UNDEF>	
match: <UNDEF>	<UNDEF>	1	21	<UNDEF>	
match: <UNDEF>	<UNDEF>	5	9	<UNDEF>	
match: <UNDEF>	<UNDEF>	5	21	<UNDEF>	
match: <UNDEF>	<UNDEF>	5	53	<UNDEF>	
match: <UNDEF>	<UNDEF>	5	56	<UNDEF>	
match: <UNDEF>	<UNDEF>	6	1	<UNDEF>	'

runsearch "./mifluzsearch -B test -c 3 -d 6 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	6	1	<UNDEF>	'

runsearch "./mifluzsearch -B test -c 3 -d 9 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	6	1	<UNDEF>	'

#
# "the world" shows twice in the same document (0 5) with the
# realm field set to 1 and 2. It must only show once in the result list.
#
runsearch "./mifluzsearch -B test -f '+the +world' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	0	5	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	20	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	21	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	51	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	81	<UNDEF>	(world the proximity)'

#
# Include documents that only contain 'the' or 'world' in the results
#
runsearch "./mifluzsearch -B test -f 'the world' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	0	5	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	20	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	21	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	51	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	81	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	71	<UNDEF>	(world )
match: <UNDEF>	<UNDEF>	0	11	<UNDEF>	(world )
match: <UNDEF>	<UNDEF>	0	3	<UNDEF>	(the )'

#
# Document 20 is excluded because it contains 'an'
#
runsearch "./mifluzsearch -B test -f '+the -an +world' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	0	5	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	21	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	51	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	81	<UNDEF>	(world the proximity)'

#
# Document 20 is excluded because it contains 'an'
#
runsearch "./mifluzsearch -B test -f  'the -an world' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	0	5	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	21	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	51	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	81	<UNDEF>	(world the proximity)
match: <UNDEF>	<UNDEF>	0	71	<UNDEF>	(world )
match: <UNDEF>	<UNDEF>	0	11	<UNDEF>	(world )
match: <UNDEF>	<UNDEF>	0	3	<UNDEF>	(the )'

#
# Exercise realm reinitialization
#
runsearch "./mifluzsearch -B test -f  'comes end' $VERBOSE" \
'match: <UNDEF>	<UNDEF>	0	6	<UNDEF>	(comes )
match: <UNDEF>	<UNDEF>	0	20	<UNDEF>	(end )'

exit 0

# ---------------------------------- OLD ---------------------------------

#
# Or : each word matches only once in separate documents
#
runsearch "./mifluzsearch -B test -c 2 -f '( or \"\" comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	20	<UNDEF>	(end )'

#
# Or : each word matches only once in separate documents
# docid 20 contains 'the' and 'end', therefore first
# docid 6 contains 'comes', is second before any document
#         containing 'the' alone because 'comes' is less frequent than 'the'
# other docid only contain 'the'.
#
runsearch "./mifluzsearch -B test -c 8 -f '( or \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	(the )
match: <UNDEF>	0	0	5	<UNDEF>	(the )
match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	20	<UNDEF>	(the end )
match: <UNDEF>	0	0	21	<UNDEF>	(the )
match: <UNDEF>	0	0	51	<UNDEF>	(the )
match: <UNDEF>	0	0	71	<UNDEF>	(the )
match: <UNDEF>	0	0	81	<UNDEF>	(the )'

#
# Run the same search in 3 times using context to resume search
#
runsearch "./mifluzsearch -B test -c 2 -f '( or \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	(the )
match: <UNDEF>	0	0	5	<UNDEF>	(the )'

runsearch "./mifluzsearch -B test -c 2 -C '$context' -f '( or \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	20	<UNDEF>	(the end )'

runsearch "./mifluzsearch -B test -c 5 -C '$context' -f '( or \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	21	<UNDEF>	(the )
match: <UNDEF>	0	0	51	<UNDEF>	(the )
match: <UNDEF>	0	0	71	<UNDEF>	(the )
match: <UNDEF>	0	0	81	<UNDEF>	(the )'

#
# After a search that stopped because there was not matches left, there
# must be no context for resuming.
#
if test "$context" != ""
then
	echo "Expected empty context after fulfilled search"
	exit 1
fi

#
# Or search with word not in database (klklk)
#
runsearch "./mifluzsearch -B test -f '( or \"\" the klkl )' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	(the )
match: <UNDEF>	0	0	5	<UNDEF>	(the )
match: <UNDEF>	0	0	20	<UNDEF>	(the )
match: <UNDEF>	0	0	21	<UNDEF>	(the )
match: <UNDEF>	0	0	51	<UNDEF>	(the )
match: <UNDEF>	0	0	71	<UNDEF>	(the )
match: <UNDEF>	0	0	81	<UNDEF>	(the )'

#
# Compound boolean query: nested 'and'
#
runsearch "./mifluzsearch -B test -f '( and \"\" the ( and \"\" an end ) )' $VERBOSE" \
'match: <UNDEF>	0	0	20	<UNDEF>	(an end  the )'

#
# Compound boolean query: nested 'and' that fails immediately
# because 'foo' is not in the inverted index.
#
runsearch "./mifluzsearch -B test -f '( and \"\" the ( and \"\" an foo ) )' $VERBOSE" \
'match: none'

#
# Compound boolean query: 'or' & 'and' 
#
runsearch "./mifluzsearch -B test -f '( and \"\" the ( or \"\" comes end ) )' $VERBOSE" \
'match: <UNDEF>	0	0	20	<UNDEF>	(end  the )'

runsearch "./mifluzsearch -B test -f '( or \"\" comes ( and \"\" the world ) )' $VERBOSE" \
'match: <UNDEF>	0	0	5	<UNDEF>	(world the  )
match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	20	<UNDEF>	(world the  )
match: <UNDEF>	0	0	21	<UNDEF>	(world the  )
match: <UNDEF>	0	0	51	<UNDEF>	(world the  )
match: <UNDEF>	0	0	71	<UNDEF>	(world the  )
match: <UNDEF>	0	0	81	<UNDEF>	(world the  )'

runsearch "./mifluzsearch -B test -P 1 -f '( or \"\" comes ( near \"\" lazy dog ) )' $VERBOSE" \
'match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	1	11	<UNDEF>	(lazy dog proximity )
match: <UNDEF>	0	1	21	<UNDEF>	(lazy dog proximity )
match: <UNDEF>	0	5	21	<UNDEF>	(lazy dog proximity )
match: <UNDEF>	0	5	56	<UNDEF>	(lazy dog proximity )'

#
# Compound boolean query: limit to 2 documents
#
runsearch "./mifluzsearch -B test -f '( or \"\" comes ( or \"\" the world ) )' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	(the  )
match: <UNDEF>	0	0	5	<UNDEF>	(the world  )
match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	11	<UNDEF>	(world  )
match: <UNDEF>	0	0	20	<UNDEF>	(the world  )
match: <UNDEF>	0	0	21	<UNDEF>	(the world  )
match: <UNDEF>	0	0	51	<UNDEF>	(the world  )
match: <UNDEF>	0	0	71	<UNDEF>	(the world  )
match: <UNDEF>	0	0	81	<UNDEF>	(the world  )'

runsearch "./mifluzsearch -B test -c 1 -f '( or \"\" comes ( or \"\" the world ) )' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	(the  )'

runsearch "./mifluzsearch -B test -c 4 -C '$context' -f '( or \"\" comes ( or \"\" the world ) )' $VERBOSE" \
'match: <UNDEF>	0	0	5	<UNDEF>	(the world  )
match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	11	<UNDEF>	(world  )
match: <UNDEF>	0	0	20	<UNDEF>	(the world  )'


#
# Compound boolean query: nested 'optional'
#
runsearch "./mifluzsearch -B test -f '( optional \"\" the ( optional \"\" world foo ) )' $VERBOSE" \
'match: <UNDEF>	0	0	5	<UNDEF>	(world  the proximity)
match: <UNDEF>	0	0	20	<UNDEF>	(world  the )
match: <UNDEF>	0	0	21	<UNDEF>	(world  the )
match: <UNDEF>	0	0	51	<UNDEF>	(world  the )
match: <UNDEF>	0	0	71	<UNDEF>	(world  the )
match: <UNDEF>	0	0	81	<UNDEF>	(world  the )
match: <UNDEF>	0	0	11	<UNDEF>	(world  )
match: <UNDEF>	0	0	3	<UNDEF>	(the )'

#
#
# Most simple search : single word
#
runsearch "./mifluzsearch -B test -f 'the' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	
match: <UNDEF>	0	0	5	<UNDEF>	
match: <UNDEF>	0	0	20	<UNDEF>	
match: <UNDEF>	0	0	21	<UNDEF>	
match: <UNDEF>	0	0	51	<UNDEF>	
match: <UNDEF>	0	0	71	<UNDEF>	
match: <UNDEF>	0	0	81	<UNDEF>	'

#
# Get all we can
#
runsearch "./mifluzsearch -B test -f '( and \"\" the world )' $VERBOSE" \
'match: <UNDEF>	0	0	5	<UNDEF>	(world the )
match: <UNDEF>	0	0	20	<UNDEF>	(world the )
match: <UNDEF>	0	0	21	<UNDEF>	(world the )
match: <UNDEF>	0	0	51	<UNDEF>	(world the )
match: <UNDEF>	0	0	71	<UNDEF>	(world the )
match: <UNDEF>	0	0	81	<UNDEF>	(world the )'

#
# First two
#
runsearch "./mifluzsearch -B test -c 2 -f '( and \"\" the world )' $VERBOSE" \
'match: <UNDEF>	0	0	5	<UNDEF>	(world the )
match: <UNDEF>	0	0	20	<UNDEF>	(world the )'

#
# The next two
#
runsearch "./mifluzsearch -B test -b 2 -c 2 -f '( and \"\" the world )' $VERBOSE" \
'match: <UNDEF>	0	0	21	<UNDEF>	(world the )
match: <UNDEF>	0	0	51	<UNDEF>	(world the )'

#
# First four
#
runsearch "./mifluzsearch -B test -c 4 -f '( and \"\" the world )' $VERBOSE" \
'match: <UNDEF>	0	0	5	<UNDEF>	(world the )
match: <UNDEF>	0	0	20	<UNDEF>	(world the )
match: <UNDEF>	0	0	21	<UNDEF>	(world the )
match: <UNDEF>	0	0	51	<UNDEF>	(world the )'

#
# Next document, using last document returned
#
runsearch "./mifluzsearch -B test -c 1 -C '$context' -f '( and \"\" the world )' $VERBOSE" \
'match: <UNDEF>	0	0	71	<UNDEF>	(world the )'

#
# Implicit or : each word matches only once in separate documents
#
runsearch "./mifluzsearch -B test -c 2 -f '( optional \"\" comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	20	<UNDEF>	(end )'

#
# Implicit or : each word matches only once in separate documents
# docid 20 contains 'the' and 'end', therefore first
# docid 6 contains 'comes', is second before any document
#         containing 'the' alone because 'comes' is less frequent than 'the'
# other docid only contain 'the'.
#
runsearch "./mifluzsearch -B test -c 8 -f '( optional \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	20	<UNDEF>	(end the proximity)
match: <UNDEF>	0	0	6	<UNDEF>	(comes )
match: <UNDEF>	0	0	3	<UNDEF>	(the )
match: <UNDEF>	0	0	5	<UNDEF>	(the )
match: <UNDEF>	0	0	21	<UNDEF>	(the )
match: <UNDEF>	0	0	51	<UNDEF>	(the )
match: <UNDEF>	0	0	71	<UNDEF>	(the )
match: <UNDEF>	0	0	81	<UNDEF>	(the )'

#
# Run the same search in 3 times using context to resume search
#
runsearch "./mifluzsearch -B test -c 2 -f '( optional \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	20	<UNDEF>	(end the proximity)
match: <UNDEF>	0	0	6	<UNDEF>	(comes )'

runsearch "./mifluzsearch -B test -c 2 -C '$context' -f '( optional \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	(the )
match: <UNDEF>	0	0	5	<UNDEF>	(the )'

runsearch "./mifluzsearch -B test -c 5 -C '$context' -f '( optional \"\" the comes end )' $VERBOSE" \
'match: <UNDEF>	0	0	21	<UNDEF>	(the )
match: <UNDEF>	0	0	51	<UNDEF>	(the )
match: <UNDEF>	0	0	71	<UNDEF>	(the )
match: <UNDEF>	0	0	81	<UNDEF>	(the )'

#
# After a search that stopped because there was not matches left, there
# must be no context for resuming.
#
if test "$context" != ""
then
	echo "Expected empty context after fulfilled search"
	exit 1
fi

#
# Or search with word not in database (klklk)
#
runsearch "./mifluzsearch -B test -f '( optional \"\" the klkl )' $VERBOSE" \
'match: <UNDEF>	0	0	3	<UNDEF>	(the )
match: <UNDEF>	0	0	5	<UNDEF>	(the )
match: <UNDEF>	0	0	20	<UNDEF>	(the )
match: <UNDEF>	0	0	21	<UNDEF>	(the )
match: <UNDEF>	0	0	51	<UNDEF>	(the )
match: <UNDEF>	0	0	71	<UNDEF>	(the )
match: <UNDEF>	0	0	81	<UNDEF>	(the )'

#
# And search with word not in database (klklk)
#
runsearch "./mifluzsearch -B test -c 1 -f '( and \"\" comes klkl )' $VERBOSE" \
'match: none'

#
# From there we deal with more complex keys (TAG,SERVER,URL)
# instead of URL alone above.
#

#
# And search with 'dog lazy'
#
runsearch "./mifluzsearch -B test -c 3 -f '( and \"\" dog lazy )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy )
match: <UNDEF>	0	1	21	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	21	<UNDEF>	(dog lazy )'

#
# And search with 'dog lazy' one URL per server only (-S)
#
runsearch "./mifluzsearch -B test -S -f '( and \"\" dog lazy )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	21	<UNDEF>	(dog lazy )
match: <UNDEF>	0	6	1	<UNDEF>	(dog lazy )'

#
# Or search with 'dog lazy' one URL per server only (-S)
#
runsearch "./mifluzsearch -B test -S -f '( optional \"\" dog lazy )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	21	<UNDEF>	(dog lazy )
match: <UNDEF>	0	6	1	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	9	<UNDEF>	(lazy )'

#
# Near search with 'lazy dog' 
#
runsearch "./mifluzsearch -B test -f '( near \"\" lazy dog )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(lazy dog proximity)
match: <UNDEF>	0	1	21	<UNDEF>	(lazy dog proximity)
match: <UNDEF>	0	5	21	<UNDEF>	(lazy dog proximity)
match: <UNDEF>	0	5	56	<UNDEF>	(lazy dog proximity)'

#
# Near search with 'dog lazy' 
#
runsearch "./mifluzsearch -B test -f '( near \"\" dog lazy )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy proximity)'

#
# Near search with 'dog lazy', order of term is not meaningfull
# matching 'dog lazy' and 'lazy dog'
#
runsearch "./mifluzsearch -B test -P -1 -f '( near \"\" dog lazy )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	1	21	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	21	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	56	<UNDEF>	(dog lazy proximity)'

#
# Near search with 'dog lazy', order of term is not meaningfull
# tolerance is -2, adding match for 'dog ? lazy' and 'lazy ? dog'
#
runsearch "./mifluzsearch -B test -P -2 -f '( near \"\" dog lazy )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	1	21	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	21	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	53	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	56	<UNDEF>	(dog lazy proximity)'

#
# Near search with 'dog lazy', order of term is meaningfull
# tolerance is 3, adding match for 'dog ? lazy' and 'dog ? ? lazy'
#
runsearch "./mifluzsearch -B test -P 3 -f '( near \"\" dog lazy )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	6	1	<UNDEF>	(dog lazy proximity)'

#
# Near search with 'lazy dog', only first 2
#
runsearch "./mifluzsearch -B test -c 2 -f '( near \"\" lazy dog )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(lazy dog proximity)
match: <UNDEF>	0	1	21	<UNDEF>	(lazy dog proximity)'

#
# Near search with 'lazy dog', resume from previous search 
# and get 2 more.
#
runsearch "./mifluzsearch -B test -c 2 -C '$context' -f '( near \"\" lazy dog )' $VERBOSE" \
'match: <UNDEF>	0	5	21	<UNDEF>	(lazy dog proximity)
match: <UNDEF>	0	5	56	<UNDEF>	(lazy dog proximity)'

#
# Near search with non existent word
#
runsearch "./mifluzsearch -B test -f '( near \"\" lazy bar )' $VERBOSE" \
'match: none'

#
# Or search using proximity  (document 0 5 53 contains lazy ? dog)
# order of term is meaningfull.
#
runsearch "./mifluzsearch -B test -P 2 -f '( optional \"\" lazy dog )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	1	21	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	21	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	53	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	56	<UNDEF>	(dog lazy )
match: <UNDEF>	0	6	1	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	9	<UNDEF>	(lazy )'

#
# Or search using proximity  (document 0 5 53 contains lazy ? dog)
# order of term is not meaningfull.
#
runsearch "./mifluzsearch -B test -P -2 -f '( optional \"\" lazy dog )' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	1	21	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	21	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	53	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	5	56	<UNDEF>	(dog lazy proximity)
match: <UNDEF>	0	6	1	<UNDEF>	(dog lazy )
match: <UNDEF>	0	5	9	<UNDEF>	(lazy )'

#
# Test getting the last valid range even if bottom is above all
# valid matches.
#
runsearch "./mifluzsearch -B test -a -b 9 -c 5 -f 'lazy' $VERBOSE" \
'match: <UNDEF>	0	5	56	<UNDEF>	
match: <UNDEF>	0	6	1	<UNDEF>	'

if [ "$bottom" != 5 ] ; then echo "expected bottom = 5 but got $bottom" ; exit 1 ; fi

runsearch "./mifluzsearch -B test -a -b 9 -c 5 -f 'to' $VERBOSE" \
'match: <UNDEF>	0	0	1	<UNDEF>	'

if [ "$bottom" != 0 ] ; then echo "expected bottom = 0 but got $bottom" ; exit 1 ; fi

#
# Test lower bound set
#
runsearch "./mifluzsearch -B test -l '<UNDEF> 0 5 0 <UNDEF>' -f 'lazy' $VERBOSE" \
'match: <UNDEF>	0	5	9	<UNDEF>	
match: <UNDEF>	0	5	21	<UNDEF>	
match: <UNDEF>	0	5	53	<UNDEF>	
match: <UNDEF>	0	5	56	<UNDEF>	
match: <UNDEF>	0	6	1	<UNDEF>	'

#
# Test higher bound set
#
runsearch "./mifluzsearch -B test -h '<UNDEF> 0 5 56 <UNDEF>' -f 'lazy' $VERBOSE" \
'match: <UNDEF>	0	1	11	<UNDEF>	
match: <UNDEF>	0	1	21	<UNDEF>	
match: <UNDEF>	0	5	9	<UNDEF>	
match: <UNDEF>	0	5	21	<UNDEF>	
match: <UNDEF>	0	5	53	<UNDEF>	'

#
# Test higher bound and lower bound set
#
runsearch "./mifluzsearch -B test -l '<UNDEF> 0 5 0 <UNDEF>' -h '<UNDEF> 0 5 56 <UNDEF>' -f 'lazy' $VERBOSE" \
'match: <UNDEF>	0	5	9	<UNDEF>	
match: <UNDEF>	0	5	21	<UNDEF>	
match: <UNDEF>	0	5	53	<UNDEF>	'

#
# Test higher bound and lower bound set and compound expression
#
runsearch "./mifluzsearch -B test -l '<UNDEF> 0 5 0 <UNDEF>' -h '<UNDEF> 0 5 56 <UNDEF>' -f '( near \"\" lazy dog )' $VERBOSE" \
'match: <UNDEF>	0	5	21	<UNDEF>	(lazy dog proximity)'
