From f4b7ebca1e004a9de81a67809c3b376062d12b5b Mon Sep 17 00:00:00 2001 From: Rok Garbas Date: Wed, 20 May 2020 13:01:29 +0200 Subject: [PATCH] Bool query (#36) * Use unsigned boto s3 requests Without this change you need s3 credentials, even though the bucket is public * Use custom attrname analyzer * Adapt query to new schema Use pname/pversion to not clash with elasticsearch parsing of version * Elasticsearch: Start work on new bool query * encode query string for bool query Co-authored-by: adisbladis --- src/ElasticSearch.elm | 117 ++++++++++++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 28 deletions(-) diff --git a/src/ElasticSearch.elm b/src/ElasticSearch.elm index 5ff063a..0cf819a 100644 --- a/src/ElasticSearch.elm +++ b/src/ElasticSearch.elm @@ -443,45 +443,106 @@ makeRequestBody : -> Http.Body makeRequestBody field query from size = -- Prefix Query + -- example query for "python" -- { - -- "query": { - -- "multi_match" : { - -- "query": "python37Packages.requests", + -- "from": 0, + -- "size": 10, + -- "query": { + -- "bool": { + -- "should": [ + -- { + -- "multi_match": { + -- "query": "python", + -- "boost": 1, -- "fields": [ - -- "attr_name.raw", - -- "attr_name", - -- "pname", - -- "pversion", - -- "description", - -- "longDescription" - -- ] + -- "attr_name.raw", + -- "attr_name" + -- ], + -- "type": "most_fields" + -- } + -- }, + -- { + -- "term": { + -- "pname": { + -- "value": "python", + -- "boost": 2 + -- } + -- } + -- }, + -- { + -- "term": { + -- "pversion": { + -- "value": "python", + -- "boost": 0.2 + -- } + -- } + -- }, + -- { + -- "term": { + -- "description": { + -- "value": "python", + -- "boost": 0.3 + -- } + -- } + -- }, + -- { + -- "term": { + -- "longDescription": { + -- "value": "python", + -- "boost": 0.1 + -- } + -- } -- } + -- ] -- } - Http.jsonBody - (Json.Encode.object - [ ( "from", Json.Encode.int from ) - , ( "size", Json.Encode.int size ) - , ( "query" + -- } + -- } + let + listIn name type_ value = + [ ( name, Json.Encode.list type_ value ) ] + + objectIn name value = + [ ( name, Json.Encode.object value ) ] + + encodeTerm ( name, boost ) = + [ ( "term" , Json.Encode.object - [ ( "multi_match" + [ ( name , Json.Encode.object - [ ( "query", Json.Encode.string query ) - , ( "fields" - , Json.Encode.list Json.Encode.string - [ "attr_name.raw" - , "attr_name" - , "pname" - , "pversion" - , "description" - , "longDescription" - ] - ) + [ ( "value", Json.Encode.string query ) + , ( "boost", Json.Encode.float boost ) ] ) ] ) ] - ) + in + [ ( "pname", 2.0 ) + , ( "pversion", 0.2 ) + , ( "description", 0.3 ) + , ( "longDescription", 0.1 ) + ] + |> List.map encodeTerm + |> List.append + [ [ "attr_name.raw" + , "attr_name" + ] + |> listIn "fields" Json.Encode.string + |> List.append + [ ( "query", Json.Encode.string query ) + , ( "boost", Json.Encode.float 1.0 ) + ] + |> objectIn "multi_match" + ] + |> listIn "should" Json.Encode.object + |> objectIn "bool" + |> objectIn "query" + |> List.append + [ ( "from", Json.Encode.int from ) + , ( "size", Json.Encode.int size ) + ] + |> Json.Encode.object + |> Http.jsonBody makeRequest :