Skip to content

Commit

Permalink
space should present an OR operation in query
Browse files Browse the repository at this point in the history
filtering part of the query was messed up when I added search over multiple
fields.

fixes #147
fixes #149
  • Loading branch information
garbas committed Aug 21, 2020
1 parent 818074f commit 405f8d6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/Page/Options.elm
Expand Up @@ -165,8 +165,8 @@ viewResultItem channel show item =
-- DEBUG: |> List.append
-- DEBUG: [ tr []
-- DEBUG: [ td [ colspan 1 ]
-- DEBUG: [ p [] [ text <| "score: " ++ String.fromFloat item.score ]
-- DEBUG: , p []
-- DEBUG: [ div [] [ text <| "score: " ++ String.fromFloat (Maybe.withDefault 0 item.score) ]
-- DEBUG: , div []
-- DEBUG: [ text <|
-- DEBUG: "matched queries: "
-- DEBUG: , ul []
Expand Down
113 changes: 48 additions & 65 deletions src/Search.elm
Expand Up @@ -737,81 +737,65 @@ filter_by_type type_ =
filter_by_query :
List String
-> String
-> List (List ( String, Json.Encode.Value ))
-> ( String, Json.Encode.Value )
filter_by_query fields queryRaw =
let
query =
queryRaw
|> String.trim
in
query
|> String.replace "." " "
|> String.words
|> List.indexedMap
(\i query_word ->
let
isLast =
List.length (String.words query) == i + 1
in
if isLast then
[ ( "bool"
, Json.Encode.object
[ ( "should"
, Json.Encode.list Json.Encode.object
(List.concatMap
(\field ->
[ [ ( "match"
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query_word )
, ( "fuzziness", Json.Encode.string "1" )
, ( "_name", Json.Encode.string <| "filter_queries_" ++ String.fromInt (i + 1) ++ "_should_match" )
( "bool"
, Json.Encode.object
[ ( "should"
, Json.Encode.list Json.Encode.object
(query
|> String.words
|> List.indexedMap
(\i query_word ->
[ ( "bool"
, Json.Encode.object
[ ( "should"
, Json.Encode.list Json.Encode.object
(List.concatMap
(\field ->
[ [ ( "match"
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query_word )
, ( "fuzziness", Json.Encode.string "1" )
, ( "_name", Json.Encode.string <| "filter_queries_" ++ String.fromInt i ++ "_should_match" )
]
)
]
)
]
)
]
, [ ( "match_bool_prefix"
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query_word )
, ( "_name"
, Json.Encode.string <| "filter_queries_" ++ String.fromInt (i + 1) ++ "_should_prefix"
)
]
, [ ( "match_bool_prefix"
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query_word )
, ( "_name"
, Json.Encode.string <| "filter_queries_" ++ String.fromInt (i + 1) ++ "_should_prefix"
)
]
)
]
)
)
]
]
)
]
]
)
fields
)
fields
)
)
]
)
]
)
]

else
List.map
(\field ->
( "match_bool_prefix"
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query_word )
, ( "_name"
, Json.Encode.string <| "filter_queries_" ++ String.fromInt (i + 1) ++ "_prefix"
)
]
)
]
)
)
fields
)
)
)
]
)


makeRequestBody :
Expand Down Expand Up @@ -849,10 +833,9 @@ makeRequestBody query from sizeRaw sort type_ sort_field query_fields should_que
, Json.Encode.object
[ ( "filter"
, Json.Encode.list Json.Encode.object
(List.append
[ [ filter_by_type type_ ] ]
(filter_by_query query_fields query)
)
[ [ filter_by_type type_ ]
, [ filter_by_query query_fields query ]
]
)
, ( "should"
, Json.Encode.list Json.Encode.object should_queries
Expand Down

0 comments on commit 405f8d6

Please sign in to comment.