0

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
              }
            }
          }
        }
      }
    }
  }
}
JohnBigs
  • 2,691
  • 3
  • 31
  • 61

1 Answers1

1

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
}
Sunder R
  • 1,074
  • 1
  • 7
  • 21
  • so using country. keyword is better performance than using match instead of term? i didnt understand why :/ – JohnBigs Dec 06 '18 at 14:17
  • i want to make sure that this query will bring only this country people and then search on them rather than the country just being another field search – JohnBigs Dec 06 '18 at 14:21
  • https://stackoverflow.com/questions/37532648/analyzed-or-not-analyzed-what-to-choose this might help you know why the using term is better performance than match – Sunder R Dec 06 '18 at 14:54
  • as for your second queries, elasticsearch is smart enough to do it for you, for more information you can check https://www.elastic.co/blog/elasticsearch-query-execution-order – Sunder R Dec 06 '18 at 15:00
  • I believe it will perform better if "must" is replaced with "filter" since you want the results filtered by the country. See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html – HypeXR Jul 29 '19 at 18:20