0

I have data:

{"NAME": "BRIDGE CAMBODJA", "NAME": "CAMBRIDGE APARTMENTS", "NAME":"KULAMAN BRIDGE"}

I have the following query:

'{"query": {"query_string": {"query": "BRIDGE"}'

and I want what will appear is:

{"NAME": "BRIDGE CAMBODJA", "NAME": "KULAMAN BRIDGE", "NAME": "CAMBRIDGE APARTMENTS"}

How do I modify my search to set the field ranking / priority?

1
  • 1
    Your data is invalid. Your "what I want" is also invalid. Commented Feb 8, 2021 at 3:20

1 Answer 1

1

You can achieve your required result, by using the boost parameter directly in the query and querying for NAME which begins with BRIDGE, ends with BRIDGE, or which contains BRIDGE.

Adding a working example

Index Mapping:

{
  "mappings": {
    "properties": {
      "NAME": {
        "type": "keyword"
      }
    }
  }
}

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "default_field": "NAME",
            "query": "BRIDGE*",
            "boost": 10
          }
        },
        {
          "query_string": {
            "default_field": "NAME",
            "query": "*BRIDGE",
            "boost": 5
          }
        },
        {
          "query_string": {
            "default_field": "NAME",
            "query": "*BRIDGE*",
            "boost": 1
          }
        }
      ]
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "66095448",
        "_type": "_doc",
        "_id": "1",
        "_score": 11.0,
        "_source": {
          "NAME": "BRIDGE CAMBODJA"
        }
      },
      {
        "_index": "66095448",
        "_type": "_doc",
        "_id": "3",
        "_score": 6.0,
        "_source": {
          "NAME": "KULAMAN BRIDGE"
        }
      },
      {
        "_index": "66095448",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "NAME": "CAMBRIDGE APARTMENTS"
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.