如何对 elasticsearch 进行预过滤以提高性能?

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

我有一个 elasticsearch 查询,可以通过多个字段搜索人员,非常简单,看起来像这样:

{
  "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
}

我的问题是我的数据库中有很多人,我想添加某种性能增强功能,这样我就会收到这个人 "country",然后我将只搜索那个国家/地区的人。

所以我尝试了类似的方法:

{
  "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
}

但它确实有效...我没有得到任何结果,我想...

我的对象看起来像这样:

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

我的映射:

{
  "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
              }
            }
          }
        }
      }
    }
  }
}

这可能会解决您的问题, 只需用 country .keyword 更改 country 字段,它将使用未分析的字段。

{
  "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
}