Multi-match query with boolean prefix on search_as_you_type is always 1.0

Hi,

I am trying to set up a search system on my java application. I have tried using MatchQuery for searches however I am trying to add a suggestions feature that will suggest autocompletion results.

I have used the https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html and to retrieve the results, I am using a MultiMatch query with boolean prefix type, but when I try to look for any suggestions, all the scores seem 1.0. Is this expected behavior? How do I get more accurate scores?

 private static void search(RestHighLevelClient client) throws IOException
{
    SearchRequest searchRequest = new SearchRequest("posts");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(buildQuery());
    searchSourceBuilder.size(5);
    searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    RestStatus status = searchResponse.status();
    TimeValue took = searchResponse.getTook();
    SearchHits hits = searchResponse.getHits();
    TotalHits totalHits = hits.getTotalHits();
    // the total number of hits, must be interpreted in the context of totalHits.relation
    long numHits = totalHits.value;
    // whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
    TotalHits.Relation relation = totalHits.relation;
    float maxScore = hits.getMaxScore();

    System.out.println("Hits: " + numHits + " max score: "+ maxScore);

    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit : searchHits) {
        String index = hit.getIndex();
        String id = hit.getId();
        float score = hit.getScore();

        System.out.println("ID: " + id + " " + getData(id, client) + " Score:" + score);

    }
    System.out.println("Status: " +  status.getStatus() + "Time: " + took.seconds() + " seconds");

}

This builds the query:

private static QueryBuilder buildQuery()
{
    MultiMatchQueryBuilder matchQueryBuilder = new MultiMatchQueryBuilder("Jo", "user");
    matchQueryBuilder.type(MultiMatchQueryBuilder.Type.BOOL_PREFIX);
    matchQueryBuilder.fuzziness(Fuzziness.AUTO);


    return matchQueryBuilder;
}

And this is a function I am using to insert the data in the beginning:
private static void create(RestHighLevelClient client) throws IOException
{

    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("user", "Joaquin Phoenix");
    jsonMap.put("postDate", new Date());
    jsonMap.put("message", "Hi?");

    Map<String, Object> user = new HashMap<>();
    user.put("type", "search_as_you_type");
    Map<String, Object> properties = new HashMap<>();
    properties.put("user", user);
    jsonMap.put("properties", properties);

    IndexRequest indexRequest = new IndexRequest("posts")
            .id("8").source(jsonMap);

    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
        System.out.println("Index created! " + indexResponse.getId() +  " " +  indexResponse.getIndex());

    } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
        System.out.println("Index updated!" + indexResponse.getId() +  " " +  indexResponse.getIndex());
    }
}

Also when should I be using completion suggester as opposed to search_as_you_type?

This is where your score is coming from now, although it's not clear to me what it's actually hoping to sort on. Try removing that line and see how the scores look.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.