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

**URL:** <https://discuss.elastic.co/t/trying-to-boost-the-frequency-of-repeated-keywords-via-the-field-keywords-suggestions/26728>\
**Category:** Elasticsearch\
**Created:** [August 3, 2015, 3:18pm UTC](https://discuss.elastic.co/t/trying-to-boost-the-frequency-of-repeated-keywords-via-the-field-keywords-suggestions/26728 "2015-08-03T15:18:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonyledenko](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@tonyledenko](https://discuss.elastic.co/u/tonyledenko)\
**Post date:** [August 3, 2015, 3:18pm UTC](https://discuss.elastic.co/t/trying-to-boost-the-frequency-of-repeated-keywords-via-the-field-keywords-suggestions/26728/1 "2015-08-03T15:18:12Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:57pm UTC](https://discuss.elastic.co/t/trying-to-boost-the-frequency-of-repeated-keywords-via-the-field-keywords-suggestions/26728/2 "2017-07-05T23:57:42Z")

</div>


