如何在elasticsearch中配置synonym_path

2024-04-19

我对 elasticsearch 很陌生,我想使用同义词,我在配置文件中添加了这些行:

index :
    analysis :
        analyzer : 
            synonym :
                type : custom
                tokenizer : whitespace
                filter : [synonym]
        filter :
            synonym :
                type : synonym
                synonyms_path: synonyms.txt

然后我创建了一个索引测试:

"mappings" : {
  "test" : {
     "properties" : {
        "text_1" : {
           "type" : "string",
           "analyzer" : "synonym"
        },
        "text_2" : {
           "search_analyzer" : "standard",
           "index_analyzer" : "synonym",
           "type" : "string"
        },
        "text_3" : {
           "type" : "string",
           "analyzer" : "synonym"
        }
     }
  }

}

并使用此数据插入类型测试:

{
"text_3" : "foo dog cat",
"text_2" : "foo dog cat",
"text_1" : "foo dog cat"
}

synonym.txt 包含“foo,bar,baz”,当我搜索 foo 时,它返回我所期望的内容,但是当我搜索 baz 或 bar 时,它返回零结果:

{
"query":{
"query_string":{
    "query" : "bar",
    "fields" : [ "text_1"],
    "use_dis_max" : true,
    "boost" : 1.0
}}} 

result:

{
"took":1,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":0,
"max_score":null,
"hits":[
]
}
}

我不知道你的问题是否是因为你定义了“bar”的同义词不好。正如您所说,您是个新手,我将举一个与您类似的有效示例。我想展示elasticsearch 如何在搜索时和索引时处理同义词。希望能帮助到你。

首先创建同义词文件:

foo => foo bar, baz

现在,我使用您尝试测试的特定设置创建索引:

curl -XPUT 'http://localhost:9200/test/' -d '{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "whitespace",
            "filter": ["synonym"]
          }
        },
        "filter" : {
          "synonym" : {
              "type" : "synonym",
              "synonyms_path" : "synonyms.txt"
          }
        }
      }
    }
  },
  "mappings": {

    "test" : {
      "properties" : {
        "text_1" : {
           "type" : "string",
           "analyzer" : "synonym"
        },
        "text_2" : {
           "search_analyzer" : "standard",
           "index_analyzer" : "standard",
           "type" : "string"
        },
        "text_3" : {
           "type" : "string",
           "search_analyzer" : "synonym",
           "index_analyzer" : "standard"
        }
      }
    }
  }
}'

请注意,synonyms.txt 必须与配置文件位于同一目录中,因为该路径是相对于配置目录的。

现在索引一个文档:

curl -XPUT 'http://localhost:9200/test/test/1' -d '{
  "text_3": "baz dog cat",
  "text_2": "foo dog cat",
  "text_1": "foo dog cat"
}'

现在的搜索

在字段 text_1 中搜索

curl -XGET 'http://localhost:9200/test/_search?q=text_1:baz'
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.15342641,
    "hits": [
      {
        "_index": "test",
        "_type": "test",
        "_id": "1",
        "_score": 0.15342641,
        "_source": {
          "text_3": "baz dog cat",
          "text_2": "foo dog cat",
          "text_1": "foo dog cat"
        }
      }
    ]
  }
}

您获得该文档是因为 baz 是 foo 的同义词,并且在索引时 foo 用其同义词进行了扩展

在字段 text_2 中搜索

curl -XGET 'http://localhost:9200/test/_search?q=text_2:baz'

result:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

我没有得到点击,因为我在索引时没有扩展同义词(标准分析器)。而且,由于我正在搜索 baz 而 baz 不在文本中,所以我没有得到任何结果。

在字段 text_3 中搜索

curl -XGET 'http://localhost:9200/test/_search?q=text_3:foo'
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.15342641,
    "hits": [
      {
        "_index": "test",
        "_type": "test",
        "_id": "1",
        "_score": 0.15342641,
        "_source": {
          "text_3": "baz dog cat",
          "text_2": "foo dog cat",
          "text_1": "foo dog cat"
        }
      }
    ]
  }
}

注:text_3是“baz狗猫”

text_3 是没有扩展同义词的索引。当我搜索 foo 时,它的同义词之一是“baz”,我得到了结果。

如果你想调试你可以使用_analyze端点例如:

curl -XGET 'http://localhost:9200/test/_analyze?text=foo&analyzer=synonym&pretty=true'

result:

{
  "tokens": [
    {
      "token": "foo",
      "start_offset": 0,
      "end_offset": 3,
      "type": "SYNONYM",
      "position": 1
    },
    {
      "token": "baz",
      "start_offset": 0,
      "end_offset": 3,
      "type": "SYNONYM",
      "position": 1
    },
    {
      "token": "bar",
      "start_offset": 0,
      "end_offset": 3,
      "type": "SYNONYM",
      "position": 2
    }
  ]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在elasticsearch中配置synonym_path 的相关文章

  • ElasticSearch:从 Painless 脚本中的嵌套字段计算 arcDistance

    我需要计算 Painless 脚本内的弧距 但在这种情况下还没有找到访问 geo API 的方法 即 第一点作为参数传递给脚本 这意味着我只获得原始值 第二点是从嵌套文档中读取的 这意味着我无法使用doc myGeoField value
  • 根据对象变量搜索对象列表

    我有一个对象列表 这些对象具有三个变量 ID 名称和值 这个列表中可能有很多对象 我需要根据ID或Name找到一个对象 并更改值 例子 class objec public string Name public int UID public
  • 在嵌套热门点击聚合中包含父 _source 字段

    我正在尝试聚合一个字段并使用top hits但我想在响应中包含未包含在嵌套属性映射中的其他字段 目前如果我指定 source include 我只能获取当前嵌套属性中的字段 这是我的映射 my cart mappings propertie
  • MySQL - 通过部分单词匹配和相关性评分进行高效搜索(全文)

    如何进行 MySQL 搜索 既匹配部分单词 又提供准确的相关性排序 SELECT name MATCH name AGAINST math IN BOOLEAN MODE AS relevance FROM subjects WHERE M
  • 使 IPTC 数据可搜索

    我对 IPTC 元数据有疑问 是否可以通过 IPTC 元数据 关键字 搜索不在数据库中的图像并显示它们 我将如何执行此操作 我只需要一个基本的想法 我知道 PHP 有 iptcparse 函数 我已经编写了一个函数来获取画廊文件夹和所有子目
  • 为什么这个 ElasticSearch 扫描和滚动不断返回相同的滚动 id?

    所以首先我运行以下命令 curl s XGET http localhost 9200 my index search scroll 1m search type scan size 10 这会返回一个滚动 ID 然后我在第一个滚动请求中使
  • Elasticsearch 中的组合非嵌套和嵌套查询

    我想使用 ES 进行书籍搜索 所以我决定将作者姓名和标题 作为嵌套文档 放入索引中 如下所示 curl XPUT localhost 9200 library search books 1 d author one books title
  • 将时间戳转换为日期时间以在 Elasticsearch 聚合中使用

    我有 SendGrid 事件数据的索引 source externalId 9283cc1d b003 xxxx a5af 84fcf31c4181 email email protected cdn cgi l email protect
  • Twitter Bootstrap 行过滤器/搜索框

    我无法找到有关如何为 Twitter Bootstrap 创建简单搜索查询或行过滤器的教程 我已经尝试了很多 我不确定是否我做错了什么或者插件与 Bootstrap 不兼容 如果可以的话请帮忙 我试过了 document ready fun
  • Elasticsearch 与 Cassandra 对比 Elasticsearch 与 Cassandra

    我正在学习 NoSQL 并正在寻找满足客户要求之一的不同选项 在提出这个问题之前我已经查阅了各种资源 一个对NoSQL知之甚少的人 我需要以更快的速度存储数据并读取数据 完全故障安全且易于扩展 能够搜索数据进行分析 我最终得到了一个简短的清
  • Elasticsearch 如何使用通配符进行 OR 查询

    我很难尝试使用 elasticsearch 构建查询 我想查询类似的内容 WHERE field 1 is match string OR field 2 is wildcard match string OR field 3 is fuz
  • 添加任何自定义实体后 jHipster Elasticsearch 问题

    org springframework beans factory UnsatisfiedDependencyException Error creating bean with name countryServiceImpl define
  • 如何管理 Elasticsearch 中的架构/映射迁移/演变?

    Flyway https flywaydb org是 RDBMS 领域中非常方便的模式迁移 演化工具 我正在为 ES 寻找类似的东西 尽管 ES 与 RDBMS 不同 而且我明白 像 Flyway 这样的工具的全部要点基本上是在多种环境例如
  • 如何找到修改文件的最新 git 提交?

    我想找到修改源文件的最新提交 我可以用git blame查看每一行提交的所有日期 但很难准确地看出哪一次提交是最后一次接触文件 如何找到触及 git 存储库中给定文件的最后一次提交 git log https git scm com doc
  • 如何使用docker将metricbeat连接到elasticsearch和kibana

    我已经使用 docker compose 设置了 elasticsearch 和 kibana elasticsearch部署在 localhost 9200当 kibana 部署在localhost 5601 当尝试使用 docker r
  • Elasticsearch,如何使 NEST 地图响应类

    首先 我使用的是NEST 5 5 0 我对远程 elasticsearch index 的使用如下 var node new Uri http distribution virk dk cvr permanent var settings
  • SQL 中的最佳 LIKE 搜索

    我有一个零件数据库 我将不断查询该数据库以获取报价系统 零件数据库有超过 1 400 000 条记录 用户将开始输入零件号 他们希望系统能够在仅几个字符后找到这些零件号 因此我需要能够进行通配符搜索 例如 SELECT NeededFiel
  • 在 Elasticsearch 中将字符串的默认映射更改为“未分析”

    在我的系统中 数据的插入始终是通过 Logstash 通过 csv 文件完成的 我从不预先定义映射 但每当我输入一个字符串时 它总是被认为是analyzed 结果像这样的条目hello I am Sinha被分成hello I am Sin
  • Elasticsearch 聚合过滤器

    因为我在谷歌上找不到任何东西 是否可以在elasticsearch中过滤聚合 我正在考虑这样的事情 获取 SOME object X gt 100 的所有对象 提前致谢 编辑 样本数据 我有以下文档结构 docKey 1 value 2 d
  • 包含 Elasticsearch 中的查询或部分匹配

    我正在寻找一个 amend 这个词 它可能在数据中以 amending amendment 甚至 amend 的形式出现 搜索此类单词的最佳方法是什么 我知道通配符可以实现这一点 但由于我的代码的其他部分 我被限制不使用它 有哪些不同的方式

随机推荐