

class Public::SearchController < ApplicationController
  layout "public"
  def search
    @lang = LangSite.first
    
    if params[:q].nil?
      @articles = []
    else
      #@articles = Article.search params[:q]
      params[:per_page] ||= 10
      params[:from] = params[:page].to_i * params[:per_page]

      @articles = Article.__elasticsearch__.search(
        query: { 
          query_string: {
            query: "*#{params[:q]}*",
            fuzzy_prefix_length: 3,
          }
        },
        
        
        #size: params[:per_page],
       # from: params[:from]
      )
      
      @articles = Article.__elasticsearch__.search(
      "query"=> {
          "multi_match"=> {
            "fields" => ["lang_articles.title^5", "lang_articles.description^3", "lang_articles.html"],
              "query"=>     "*#{params[:q]}*",
              "fuzziness"=> "AUTO",
              #"operator"=>  "and"
            },
            
          
            
          
        }
      )
      
      @pages = Page.__elasticsearch__.search(
      "query"=> {
          "multi_match"=> {
            "fields" => ["lang_pages.title^10", "lang_pages.description^3", "lang_pages.html"],
              "query"=>     "*#{params[:q]}*",
              "fuzziness"=> "AUTO",
              #"operator"=>  "and"
            },
            
          
            
          
        }
      )
      
      
      
      
         
      #query: "*text search terms* AND type:\"blog\" AND status:\"published\""
    end
  end
  
  
end
