Elastic Search 中带有附加条件的 Completion Suggester

Completion Suggester with additional conditions in Elastic Search

我有一个索引 returns 不同语言的职位。

我需要根据针对单一语言的单一文本搜索类似的工作。比方说,我已将 1 设置为英语的 LanguageId。我想搜索与帐户匹配的工作。所以如果我在下面写查询,它会获取所有不同语言的工作。所以基本上 "must" 查询没有任何影响。

GET jobs/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "languageid": "1"
          }
        }
      ]
    }
  },
  "suggest": {
    "suggestions": {
      "text": "acce",
      "completion": {
        "field": "jobs.suggest",
        "size": 30
      }
    }
  }
}

我的映射如下所示

   "mappings": {
"jobs": {
        "properties": {
          "@timestamp": {
            "type": "date"
          },
          "@version": {
            "type": "text"
          },
          "industytype": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "jobs": {
            "properties": {
              "suggest": {
                "type": "completion",
                "analyzer": "simple",
                "preserve_separators": true,
                "preserve_position_increments": true,
                "max_input_length": 50
              }
            }
          },
          "language": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "type": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "updateddate": {
            "type": "date"
          }
        }
      }
    }
}

无法在查询时过滤掉建议,因为 completion suggester 使用 FST - 在索引时构建的特殊内存数据结构:

Suggesters in Lucene are built in-memory by loading the completion values from the index, then building the FST. This can be a slow, resource intensive process. And, as soon as the index changes, the FST needs to be rebuilt. "Real time search" is a mantra of Elasticsearch. It is not acceptable to return out of date suggestions, nor to require a full rebuild whenever the index changes. Instead of building the FST at search time, we now build an FST per-segment at index time.

所以你所能做的就是添加context for your suggester. Context also filled at index time along with completion field and therefore can be used at query time in suggest query. Also, this article可能对你有用。