【问题标题】:Elasticsearch : Fuzzy query and filter resultsElasticsearch:模糊查询和过滤结果
【发布时间】:2021-08-09 15:57:54
【问题描述】:

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

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

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

结果非常好,但我想添加对其他参数的查询结果进行过滤的可能性:例如,我只想保留字段“observatory”是这些值之一的文档:[“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"
    ]
  }
}

【问题讨论】:

  • 能否分享一些示例索引数据和预期的搜索结果?

标签: elasticsearch lucene


【解决方案1】:

你可以使用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"
        }
      }
    ]

【讨论】:

  • @Agudolive 在您执行匹配查询的示例索引数据中的name 字段在哪里?
  • 对不起,我在我的代码中打错了,我已修复它;)没有“名称”字段,它是“技能”字段!对不起
  • @Agudolive 没问题 :-) 请仔细阅读我上面回答中的工作示例,如果这是您期望得到的,请告诉我?这与您期望获得的搜索结果相同吗?
  • 我确实尝试过,但它返回了我的不匹配:/
  • @Agudolive 是我上面得到的搜索结果,和你期望得到的一样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多