Skip to content

Commit

Permalink
rework ranking queries
Browse files Browse the repository at this point in the history
fixes #156
  • Loading branch information
garbas committed Aug 28, 2020
1 parent f10911f commit 8ded181
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 98 deletions.
137 changes: 47 additions & 90 deletions src/Page/Packages.elm
Expand Up @@ -206,8 +206,15 @@ viewResultItem channel show item =
-- DEBUG: |> List.append
-- DEBUG: [ tr []
-- DEBUG: [ td [ colspan 4 ]
-- DEBUG: [ p [] [ text <| "score: " ++ String.fromFloat item.score ]
-- DEBUG: , p []
-- DEBUG: [ div []
-- DEBUG: [ text <|
-- DEBUG: "score: "
-- DEBUG: ++ (item.score
-- DEBUG: |> Maybe.map String.fromFloat
-- DEBUG: |> Maybe.withDefault "No score"
-- DEBUG: )
-- DEBUG: ]
-- DEBUG: , div []
-- DEBUG: [ text <|
-- DEBUG: "matched queries: "
-- DEBUG: , ul []
Expand Down Expand Up @@ -404,120 +411,70 @@ makeRequest options channel queryRaw from size sort =
queryRaw
|> String.trim

delimiters =
Maybe.withDefault Regex.never (Regex.fromString "[. ]")

should_match boost_base =
makeMatchQueries queryType queryIndex queryWord =
List.indexedMap
(\i ( field, boost ) ->
[ ( "match"
(\fieldIndex field ->
[ ( queryType
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query )
, ( "boost", Json.Encode.float <| boost_base * boost )
, ( "analyzer", Json.Encode.string "whitespace" )
, ( "fuzziness", Json.Encode.string "1" )
, ( "_name"
, Json.Encode.string <|
"should_match_"
++ String.fromInt (i + 1)
)
[ ( "query", Json.Encode.string queryWord )
, ( "boost", Json.Encode.float <| ((fieldIndex + 1 |> toFloat) * 10) * (queryIndex + 1 |> toFloat) )
, ( "_name", Json.Encode.string <| "ranking_queries_" ++ queryType ++ "_" ++ queryWord ++ "_" ++ field )
]
)
]
)
]
)
[ ( "package_attr_name", 5 )
, ( "package_attr_name_query", 3 )
, ( "package_pname", 4 )
, ( "package_description", 2 )
, ( "package_longDescription", 1 )
[ "package_attr_name"
, "package_pname"
, "package_attr_name_query"
, "package_description"
, "package_longDescription"
]

should_match_bool_prefix boost_base =
makeTermQueries queryIndex queryWord =
List.indexedMap
(\i ( field, boost ) ->
[ ( "match_bool_prefix"
(\fieldIndex field ->
[ ( "term"
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query )
, ( "boost", Json.Encode.float <| boost_base * boost )
, ( "analyzer", Json.Encode.string "whitespace" )
, ( "fuzziness", Json.Encode.string "1" )
, ( "_name"
, Json.Encode.string <|
"should_match_bool_prefix_"
++ String.fromInt (i + 1)
)
[ ( "value", Json.Encode.string queryWord )
, ( "boost", Json.Encode.float <| 10 * ((fieldIndex + 1 |> toFloat) * 10) * (queryIndex + 1 |> toFloat) )
, ( "_name", Json.Encode.string <| "ranking_queries_term_" ++ queryWord ++ "_" ++ field )
]
)
]
)
]
)
[ ( "package_attr_name", 2 )
, ( "package_attr_name_query", 1 )
, ( "package_pname", 3 )
[ "package_pname"
, "package_attr_name"
, "package_attr_name_query"
]

should_terms boost_base =
List.indexedMap
(\i ( field, boost ) ->
[ ( "terms"
, Json.Encode.object
[ ( field
, Json.Encode.list Json.Encode.string (Regex.split delimiters query)
)
, ( "boost", Json.Encode.float <| boost_base * boost )
, ( "_name"
, Json.Encode.string <|
"should_terms_"
++ String.fromInt (i + 1)
)
]
)
]
)
[ ( "package_attr_name", 3 )
, ( "package_attr_name_query", 2 )
, ( "package_pname", 4 )
, ( "package_attr_set", 1 )
]
match_queries =
String.words query
|> List.indexedMap (makeMatchQueries "match")
|> List.concat

should_term boost_base =
List.indexedMap
(\i ( field, boost ) ->
[ ( "term"
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "value", Json.Encode.string query )
, ( "boost", Json.Encode.float <| boost_base * boost )
, ( "_name"
, Json.Encode.string <|
"should_term_"
++ String.fromInt (i + 1)
)
]
)
]
)
]
)
[ ( "package_attr_name", 2 )
, ( "package_attr_name_query", 1 )
, ( "package_pname", 3 )
]
match_bool_prefix_queries =
String.words query
|> List.indexedMap (makeMatchQueries "match_bool_prefix")
|> List.concat

term_queries =
String.words query
|> List.indexedMap makeTermQueries
|> List.concat

should_queries =
ranking_queries =
[]
|> List.append (should_term 10000)
|> List.append (should_terms 1000)
|> List.append (should_match_bool_prefix 100)
|> List.append (should_match 10)
|> List.append match_queries
|> List.append match_bool_prefix_queries
|> List.append term_queries
in
Search.makeRequest
(Search.makeRequestBody query
Expand All @@ -531,7 +488,7 @@ makeRequest options channel queryRaw from size sort =
, "package_description"
, "package_longDescription"
]
should_queries
ranking_queries
)
("latest-" ++ String.fromInt options.mappingSchemaVersion ++ "-" ++ channel)
decodeResultItemSource
Expand Down
15 changes: 7 additions & 8 deletions src/Search.elm
Expand Up @@ -752,7 +752,7 @@ filter_by_query fields queryRaw =
(query
|> String.words
|> List.indexedMap
(\i query_word ->
(\i queryWord ->
[ ( "bool"
, Json.Encode.object
[ ( "should"
Expand All @@ -763,9 +763,8 @@ filter_by_query fields queryRaw =
, 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" )
[ ( "query", Json.Encode.string queryWord )
, ( "_name", Json.Encode.string <| "filter_queries_should_match_" ++ field ++ "_" ++ queryWord )
]
)
]
Expand All @@ -775,9 +774,9 @@ filter_by_query fields queryRaw =
, Json.Encode.object
[ ( field
, Json.Encode.object
[ ( "query", Json.Encode.string query_word )
[ ( "query", Json.Encode.string queryWord )
, ( "_name"
, Json.Encode.string <| "filter_queries_" ++ String.fromInt (i + 1) ++ "_should_prefix"
, Json.Encode.string <| "filter_queries_should_match_bool_prefix_" ++ field ++ "_" ++ queryWord
)
]
)
Expand Down Expand Up @@ -809,7 +808,7 @@ makeRequestBody :
-> List String
-> List (List ( String, Json.Encode.Value ))
-> Http.Body
makeRequestBody query from sizeRaw sort type_ sort_field query_fields should_queries =
makeRequestBody query from sizeRaw sort type_ sort_field query_fields ranking_queries =
let
-- you can not request more then 10000 results otherwise it will return 404
size =
Expand Down Expand Up @@ -839,7 +838,7 @@ makeRequestBody query from sizeRaw sort type_ sort_field query_fields should_que
]
)
, ( "should"
, Json.Encode.list Json.Encode.object should_queries
, Json.Encode.list Json.Encode.object ranking_queries
)
]
)
Expand Down

0 comments on commit 8ded181

Please sign in to comment.