Trying to boost the frequency of repeated keywords via the “field: keywords”. Suggestions?


(Ledenko) #1

Here is my code:

def self.parts(params)
page = (params[:page] || 1).to_i rescue 1
search_size = 27

  if params[:query].present?
  	query = params[:query].strip

    queries = [
      # exact part id match
      { term: { id: query.upcase }},
      
      # partial part id matching with and without dashes
      { multi_match: { query: query.upcase, type: 'phrase_prefix', fields: ['id.nodash', 'id.keyword'] }},

      # iphone 6 expanded results from phrase_prefix against keywords
      { multi_match: { query: query, operator: 'and', fields: ['summary_description', 'keywords', 'visual_description'] }},

      #{ multi_match: { query: query, , fields: ['keywords']}}
      #{ multi_match: { query: query, fields: ['summary_description', 'keywords'] }}
      #{ match_phrase_prefix: { _all: query }},
      #{ match: { _all: { query: query, operator: 'and', fuzziness: 2, prefix_length: 1 }}}, 
      #{ query_string: { query: query } } # fails to parse characters like " and / in query
    ]

    # if theres only one search term add a wildcard search, this is for searches like "holga"
    if(query.split(/\s+/).length == 1)
      queries << { wildcard: {'id.nodash' => "*#{query.downcase}*"}}
      queries << { wildcard: {'id.keyword' => "*#{query.upcase}*"}}
    end

  	q = {
      function_score: {
        query: {
      		bool: { should: queries }
        },
        functions: [
        	#{ filter: { term: { solution_type: 'Component'}}, boost_factor: 0.1 },
        	#{ filter: { term: { solution_type: 'Kit'}}, boost_factor: 1.6 },
        	#{ script_score: { script: "(_score + 1) * (doc.rank_3.value  * 50)" } }
          #{ script_score: { script: "(_score) * log((doc.rank_3.value * 100) + 1)" } }
          { script_score: { script: "doc.normalized_factor.value" } }
        ],
        boost_mode: "replace"
      }
    }
      
    #sort = "_score", { rank_3: "desc" }
    else
    	#q = { match_all: {} }
    #sort = '_id'
    q = {
      function_score: {
        query: { match_all: {} },
        functions: [
          { script_score: { script: "doc.normalized_factor.value" } }
        ],
        boost_mode: "replace"
      }
    }
    end

  sort = "_score"

    h = {
    	query: {
    		filtered: {
    			query: q,
    			filter: create_filters(params[:facets])
    		}
    	}
    }

  h[:facets] = create_facet_hash_for(:search)
  h[:from] = (page -1) * search_size
  h[:size] = search_size
  h[:explain] = false
  #h[:min_score] = 0.01
    h[:sort] = sort if sort

  Tire.search 'parts', h
end

def self.components(params)
  page = (params[:page] || 1).to_i rescue 1
  search_size = 27

  if params[:query].present?
    query = params[:query].strip

    queries = [
      # exact part id match
      { term: { id: query.upcase }},
      
      # partial part id matching with and without dashes
      { multi_match: { query: query.upcase, type: 'phrase_prefix', fields: ['id.nodash', 'id.keyword'] }},

      # iphone 6 expanded results from phrase_prefix against keywords
      { multi_match: { query: query, operator: 'and', type: 'phrase_prefix', fields: ['summary_description', 'keywords'] }},

      #{ multi_match: { query: query, , fields: ['keywords']}}
      #{ multi_match: { query: query, fields: ['summary_description', 'keywords'] }}
      #{ match_phrase_prefix: { _all: query }},
      #{ match: { _all: { query: query, operator: 'and', fuzziness: 2, prefix_length: 1 }}}, 
      #{ query_string: { query: query } } # fails to parse characters like " and / in query
    ]

    # if theres only one search term add a wildcard search, this is for searches like "holga"
    if(query.split(/\s+/).length == 1)
      queries << { wildcard: {'id.nodash' => "*#{query.downcase}*"}}
      queries << { wildcard: {'id.keyword' => "*#{query.upcase}*"}}
    end

    q = {
      function_score: {
        query: {
          bool: { should: queries }
        },
        functions: [
          { script_score: { script: "doc.normalized_factor.value" } }
        ]
      }
    }
  else
    q = {
      function_score: {
        query: { match_all: {} },
        functions: [
          { script_score: { script: "doc.normalized_factor.value" } }
        ],
        boost_mode: "replace"
      }
    }
  end

(system) #2