0

I have an ElasticSearch database that has "pages" in it.

class Page
  field :domain_id 
  field :page_id 
  field :title 
  field :description 
  field :filetype 
  field :content
end 

Each page has an ID of a domain. I'd like to be able to boost the results from a particular domain on all queries. So, if it matches a domain_id of X, set statically, it will be more relevant in every search result we return.

Second, I'd like to boost based on the text (which can be multiple words) matching the title or description with higher relevance, than matching the content for example.

I currently have the following query in the Tire gem.

query { text :_all, search_term } 

What should I add to do the 2 things above? If the user enters "title:policy manuals" (without quotes obviously), how will that affect things?

Williamf
  • 595
  • 4
  • 14

1 Answers1

1

The first problem can be resolved by using Custom Filters Score Query with filter that would match domain_id. The second problem can be resolved by searching each field separately and applying appropriate boost factor. All together it may look something like this:

{
  "custom_filters_score": {
    "query": {
      "bool": {
        "should": [
          {
            "text": {
              "title": "SEARCH_TERM",
              "boost": 2
            }
          },
          {
            "text": {
              "description": "SEARCH_TERM",
              "boost": 2
            }
          },
          {
            "text": {
              "content": "SEARCH_TERM",
              "boost": 1
            }
          }
        ]
      }
    },
    "filters": [
      {
        "filter": { "term": {"domain_id": "X"}},
        "boost": 3
      }
    ]
  }
}
imotov
  • 28,277
  • 3
  • 90
  • 82
  • And you can even add another `should` clause, using the `query_string` query (see eg. [here](https://github.com/karmi/rubygems.org/blob/search-steps/app/controllers/searches_controller.rb#L16-L17)), to expose the Lucene query syntax to your users. – karmi Nov 21 '12 at 08:58