Joost
Joost

Reputation: 97

Elasticsearch 5: Boost 1 field

I have 3 types with the same fields names indexed in Elasticsearch 5:

TypeA

TypeB

TypeC - integer id - string name

GET myindex/TypeA,TypeB,TypeC/_search
{
  "_source": ["id", "name"],
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "fields": [
              "_all",
              "name^3"
            ],
            "query": "Foo bar*",
            "default_operator": "and"
          }
        }
      ]
    }
  }
}

I only want to boost the name field for TypeA. In this scenario the name field for TypeA, TypeB and TypeC are boosted.

How can I only boost the name for TypeA?

I'm looking for something like this:

"fields": [
     "_all",
     "TypeA.name^3"
]

Thank You.

Upvotes: 0

Views: 108

Answers (1)

Val
Val

Reputation: 217554

You need to create one subquery for type A and another subquery for types B and C

GET myindex/_search
{
  "_source": [
    "id",
    "name"
  ],
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "_type": "TypeA"
                }
              },
              {
                "query_string": {
                  "fields": [
                    "_all",
                    "name^3"
                  ],
                  "query": "Foo baar*",
                  "default_operator": "and"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "_type": [
                    "TypeB",
                    "TypeC"
                  ]
                }
              },
              {
                "query_string": {
                  "fields": [
                    "_all",
                    "name"
                  ],
                  "query": "Foo baar*",
                  "default_operator": "and"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions