Elasticsearch:模糊查询和过滤结果

Elasticsearch : Fuzzy query and filter results

我是 Elasticsearch 的新手,我正在尝试创建一个带有模糊查询的搜索引擎。

我可以使用此代码通过模糊搜索获得结果:

{
  "query": {
    "match": {
      "skill": {
        "query": "Project management",
        "fuzziness": 2,
        "prefix_length": 1
      }
    }
  }
}

结果非常好,但我想增加根据其他参数过滤查询结果的可能性:例如,我想只保留字段“天文台”是这些值之一的文档:[" ROME", "ESCO"](我需要将这些值作为数组提供)

我试过类似的方法,但不确定为什么它不起作用:

{
  "query": {
    "match": {
      "skill": {
        "query": "Project management",
        "fuzziness": 2,
        "prefix_length": 1
      }
    },
    "filter" : {
        "bool": {
          "must": {
            "terms": {
              "observatory": ["ROME", "ESCO"],
              "minimum_should_match": 3
            }
          }
        }
      }
  }
}

我的问题是:是否可以这样进行搜索?模糊搜索和过滤 ? 如果是:怎么做?

我的映射如下:

{
  "skills": {
    "mappings": {
      "properties": {
        "referentiel_id": {
          "type": "text"
        },
        "observatory": {
          "type": "text"
        },
        "language": {
          "type": "text"
        },
        "type": {
          "type": "text"
        },
        "skill_id_ds_db": {
          "type": "text"
        },
        "skill_id_sm_db": {
          "type": "text"
        },
        "skill": {
          "type": "text"
        },
        "competence_id": {
          "type": "text"
        }
      }
    }
  }
}

感谢您的帮助!

编辑:这是我的 skill 索引中的一些示例值,对于输出,我需要相同的字段

{
  "_index": "skills",
  "_type": "_doc",
  "_id": "kUgpiXkB8y6qOrWRteCU",
  "_version": 1,
  "_score": 0,
  "fields": {
    "observatory": [
      "ONET"
    ],
    "skill_id_sm_db": [
      "null"
    ],
    "skill_id_ds_db": [
      "1065629"
    ],
    "skill": [
      "Calibrate and test anesthesia equipment."
    ],
    "competence_id": [
      "null"
    ],
    "language": [
      "en"
    ],
    "referentiel_id": [
      "null"
    ],
    "type": [
      "hard"
    ]
  }
},
{
  "_index": "skills",
  "_type": "_doc",
  "_id": "PUgpiXkB8y6qOrWRrbKF",
  "_version": 1,
  "_score": 0,
  "fields": {
    "observatory": [
      "ESCO"
    ],
    "skill_id_sm_db": [
      "null"
    ],
    "skill_id_ds_db": [
      "1049331"
    ],
    "skill": [
      "Types of engraving stone"
    ],
    "competence_id": [
      "null"
    ],
    "language": [
      "en"
    ],
    "referentiel_id": [
      "null"
    ],
    "type": [
      "hard"
    ]
  }
},
{
  "_index": "skills",
  "_type": "_doc",
  "_id": "kkgpiXkB8y6qOrWRkASr",
  "_version": 1,
  "_score": 0,
  "fields": {
    "observatory": [
      "null"
    ],
    "skill_id_sm_db": [
      "2254"
    ],
    "skill_id_ds_db": [
      "null"
    ],
    "skill": [
      "Fédérateur et sait innover pour mobiliser le management, les équipes et les salariés "
    ],
    "competence_id": [
      "null"
    ],
    "language": [
      "fr"
    ],
    "referentiel_id": [
      "8"
    ],
    "type": [
      "null"
    ]
  }
}

您可以使用 bool/must/filter 子句的组合

添加包含索引数据、映射、搜索查询和搜索结果的工作示例

索引数据:

{
  "name": "Vaccuuum",
  "observatory": "ABC"
}
{
  "name": "Vaccuum",
  "observatory": "ESCO"
}
{
  "name": "Vaccuum",
  "observatory": "ABC"
}

搜索查询:

{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": {
            "query": "Vacuumm",
            "fuzziness": "auto"
          }
        }
      },
      "filter": {
        "terms": {
          "observatory": [
            "rome",
            "esco"
          ]
        }
      }
    }
  }
}

搜索结果:

 "hits": [
      {
        "_index": "67619660",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.7295573,
        "_source": {
          "name": "Vacuum",
          "observatory": "ROME"
        }
      },
      {
        "_index": "67619660",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.6253348,
        "_source": {
          "name": "Vaccuum",
          "observatory": "ESCO"
        }
      }
    ]