JohnBigs
JohnBigs

Reputation: 2811

How to have a pre filter to elasticsearch to enhance performance?

I have an elasticsearch query that search people by multiple fields, pretty simple, it looks like this:

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "joe",
            "fields": [
              "personName^-1.0",
              "person.city^-1.0",
              "person.street^-1.0"
            ],
            "type": "phrase_prefix",
            "lenient": "true"
          }
        }
      ],
      "boost": 1.0,
      "minimum_should_match": "1"
    }
  },
  "from": 0,
  "size": 20
}

my issue is that I have allot of people in my database and I want to add some kind of performance enhancement so I will recieve the person "country" and than I will search only people in that country.

so I tried something like:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "country": {
              "boost": "0.0",
              "value": "US"
            }
          }
        }
      ],
      "should": [
        {
          "multi_match": {
            "query": "joe",
            "fields": [
              "personName^-1.0",
              "person.city^-1.0",
              "person.street^-1.0"
            ],
            "type": "phrase_prefix",
            "lenient": "true"
          }
        }
      ],
      "boost": 1.0,
      "minimum_should_match": "1"
    }
  },
  "from": 0,
  "size": 20
}

but it dosent work...i get no result and I suppose to..

my object looks like this:

{
  "personName": "joey",
  "country": "US",
   "city": "LA",
   "street": "hollywood",
}

my mappings:

{
  "people": {
    "mappings": {
      "vendors": {
        "properties": {
          "country": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "personName": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "street": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "city": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

Upvotes: 0

Views: 1839

Answers (1)

Sunder R
Sunder R

Reputation: 1094

This might fix your issue, just change the country field with the country .keyword, it will use the non-analyzed field.

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "country.keyword": {
              "boost": "0.0",
              "value": "US"
            }
          }
        }
      ],
      "should": [
        {
          "multi_match": {
            "query": "joe",
            "fields": [
              "personName^-1.0",
              "person.city^-1.0",
              "person.street^-1.0"
            ],
            "type": "phrase_prefix",
            "lenient": "true"
          }
        }
      ],
      "boost": 1.0,
      "minimum_should_match": "1"
    }
  },
  "from": 0,
  "size": 20
}

Upvotes: 1

Related Questions