Thursday, 30 May 2019

Giving priority to prefix match in elasticsearch in php

Is there a way in elasticsearch to give more priority for the prefix match than to the string that contains that word?

For ex.- priorities of words if I search for ram should be like this:

Ram Reddy
Joy Ram Das
Kiran Ram Goel
Swati Ram Goel
Ramesh Singh

I have tried mapping as given in here. I have done like this:

$params = ['body' => []];
        foreach ($records as $key => $record) {
            $params['body'][] = [
                'index' => [
                    '_index' => $myIndex,
                    '_type'  => $myType,
                ],
                "mappings"=>[
                    "doc"=>[
                        "properties"=>[
                            "label"=>[
                                "type"=>"text",
                                "fielddata"=> true,
                                "analyzer"=>"whitespace",
                                "fields"=>[
                                    "keyword"=>[
                                        "type"=>"keyword"
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ];
            $record['sr']=intval($record['sr']);
            $params['body'][] = $record;
            if ($key%1000 == 0) { // Every 1000 documents stop and send the bulk request
                $response = $client->bulk($params);
                $params = ['body' => []];  // erase the old bulk request
                unset($response);      // unset the bulk response when you are done to save memory
            }
        }
        // Send the last batch if it exists
        if (!empty($params['body'])) {
            $responses = $client->bulk($params);
        }

and searching like this:

$body = [
    "size" => 100,
    'sort' => [
        ["type"=>["order"=>"asc"]],
        ["sr"=>["order"=>"asc"]],
        ["propLabels"=>["order"=>"asc"]],
        ["value"=>["order"=>"asc"]]
    ],
    '_source' => $select,
    'query' => [
        'bool' => [
            'must' => $must_query,
            'must_not'=>$removeFilter
         ]
     ]
];

$params = [
    'index' => $myIndex,
    'type' => $myType,
    'body' => []
];
$params['body'] = $body;
$response = $client->search($params);

But while searching I'm getting the error:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"filter_200_12662","node":"ePcgM3YVRzKz59RH5Hcz1g","reason":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}}],"caused_by":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}},"status":400}

Is there any other way to sort the results for the search in the relational database using the search method in php?



from Giving priority to prefix match in elasticsearch in php

No comments:

Post a Comment