当前位置: 首页 > 面试题库 >

Elasticsearch-使用通配符搜索

谷梁弘深
2023-03-14
问题内容

我已经使用此批量请求设法用4个文档填充了索引:

开机自检 localhost:9200/titles/movies/_bulk

{"index":{"_id":"1"}}
{"id": "1","level": "first","titles": [{"value": "The Bad and the Beautiful","type": "Catalogue","main": true},{"value": "The Bad and the Beautiful (1945)","type": "International","main": false}]}
{"index":{"_id":"2"}}
{"id": "2","level": "first","titles": [{"value": "Bad Day at Black Rock","type": "Drama","main": true}]}
{"index":{"_id":"3"}}
{"id": "3","level": "second","titles": [{"value": "Baker's Wife","type": "AnotherType","main": true},{"value": "Baker's Wife (1940)","type": "Trasmitted","main": false}]}
{"index":{"_id":"4"}}
{"id": "4","level": "second","titles": [{"value": "Bambi","type": "Educational","main": true},{"value": "The Baby Deer and the hunter (1942)","type": "Fantasy","main": false}]}

现在如何在所有可用 标题* 上使用 通配符 进行 搜索 ***

类似于 localhost:9200/titles/movies/_search?q=*&sort=level:asc
但提供一个或多个通配符的东西。例如,搜索“ The % the %”并解析来自elasticsearch的响应以最终返回如下内容:

{
    "count":2,
    "results":[{
        "id":"1",
        "level":"first",
        "foundInTitleTypes":["Catalogue","International"]
    },{
        "id":"4",
        "level":"second",
        "foundInTitleTypes":["Fantasy"]
    }]
}

谢谢!


问题答案:

Elasticsearch在常规匹配查询中提供正则表达式支持

GET titles/movies/_search
{
    "query": {
        "match" : { "titles.value" : "The * the *" }
    }
}

给你这个

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.6406528,
    "hits": [
      {
        "_index": "titles",
        "_type": "movies",
        "_id": "4",
        "_score": 1.6406528,
        "_source": {
          "id": "4",
          "level": "second",
          "titles": [
            {
              "value": "Bambi",
              "type": "Educational",
              "main": true
            },
            {
              "value": "The Baby Deer and the hunter (1942)",
              "type": "Fantasy",
              "main": false
            }
          ]
        }
      },
      {
        "_index": "titles",
        "_type": "movies",
        "_id": "1",
        "_score": 0.9026783,
        "_source": {
          "id": "1",
          "level": "first",
          "titles": [
            {
              "value": "The Bad and the Beautiful",
              "type": "Catalogue",
              "main": true
            },
            {
              "value": "The Bad and the Beautiful (1945)",
              "type": "International",
              "main": false
            }
          ]
        }
      }
    ]
  }
}

要更新到您的问题URI搜索,我不确定是否可行,如果使用curl进行操作,则只需将查询dsl省略为数据即可

curl localhost:9200/titles/movies/_search -d '{"query":{"match":{"titles.value":"The * the *"}}}'

{"took":46,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.6406528,"hits":[{"_index":"titles","_type":"movies","_id":"4","_score":1.6406528,"_source":{"id": "4","level": "second","titles": [{"value": "Bambi","type": "Educational","main": true},{"value": "The Baby Deer and the hunter (1942)","type": "Fantasy","main": false}]}},{"_index":"titles","_type":"movies","_id":"1","_score":0.9026783,"_source":{"id": "1","level": "first","titles": [{"value": "The Bad and the Beautiful","type": "Catalogue","main": true},{"value": "The Bad and the Beautiful (1945)","type": "International","main": false}]}}]}}

更新至最新问题:

好吧,如果您想按级别排序,则需要为elasticsearch提供一个映射。我做了什么:

删除索引

DELETE titles

添加映射

PUT titles
{
  "settings": {
    "number_of_shards": 1
  }, 
  "mappings": {
    "movies": {
      "properties": {
        "level": {
          "type": "keyword"
        }
      }
    }
  }
}

优化查询DSL

GET titles/movies/_search
{
  "_source": [
    "id",
    "level",
    "titles.value"
  ],
  "sort": [
    {
      "level": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "match": {
      "titles.value": "The * the *"
    }
  }
}

那给我

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "titles",
        "_type": "movies",
        "_id": "1",
        "_score": null,
        "_source": {
          "level": "first",
          "id": "1",
          "titles": [
            {
              "value": "The Bad and the Beautiful"
            },
            {
              "value": "The Bad and the Beautiful (1945)"
            }
          ]
        },
        "sort": [
          "first"
        ]
      },
      {
        "_index": "titles",
        "_type": "movies",
        "_id": "4",
        "_score": null,
        "_source": {
          "level": "second",
          "id": "4",
          "titles": [
            {
              "value": "Bambi"
            },
            {
              "value": "The Baby Deer and the hunter (1942)"
            }
          ]
        },
        "sort": [
          "second"
        ]
      }
    ]
  }
}


 类似资料:
  • 问题内容: 我目前正在使用Tire Client进行elasticsearch。可以说我有一个字段,该字段在我的elasticsearch映射中被索引为很长的类型的字段。我正在尝试实现以下目标: “ id”是我一直在谈论的领域。但是由于我在查询中指定了字段,所以通配符不起作用,最终我得到了唯一匹配的结果。 但是对_all搜索也可以执行相同的操作,因为字段类型无关紧要。我希望此通配符搜索有效,同时还

  • 问题内容: 我正在尝试为最终用户提供搜索类型,这更像sqlserver。我能够为给定的SQL场景实现ES查询: 在ES中,我使用ngram tokenizer来达到预期的结果: 所以,如果我的文档行像 上面的查询只显示了两个文档,但是当我尝试输入Peter sims或Peter simson时,除非我输入Peter tomson robert sims或Peter tomson robert si

  • 问题内容: 我想获取带有通配符的搜索模式的文件名列表。喜欢: 我怎样才能做到这一点? 问题答案: 您可以这样做: 注意 :如果目录中包含以开头的文件,则默认情况下将不匹配它们。例如,考虑包含和的目录: 这直接来自这里:http : //docs.python.org/library/glob.html

  • 问题内容: 弹性搜寻1.6 我想索引包含连字符的文本,例如U-12,U-17,WU-12,T恤…,并能够使用“简单查询字符串”查询来搜索它们。 数据样本(简体): 所以我去了这个映射: 使用以下查询进行搜索: 什么有效: “ U-12”,“ U ”,“ t ”,“ ts *” 什么不起作用: “ U-”,“ u-1 ”,“ t-”,“ t-sh ”,… 看来char过滤器未在搜索字符串上执行?我该

  • 问题内容: 我有一个类似以下设置和映射的索引; 我正在努力实现现场通配符搜索的实现。我的示例数据如下: 当我执行以下查询时; 它返回,。我认为,它仍然标记数据。它只能返回。 您能帮上忙吗? 提前致谢 问题答案: 我的解决方案历险记 如您在我的问题中所见,我已经开始审理案件。每当我更改了一部分设置后,一部分便开始工作,而另一部分则停止工作。让我给出我的解决方案历史记录: 1.) 我已将数据索引为默认

  • 该字段中的映射是: 所以,我想我误解了通配符在ES中是如何工作的。有人知道为什么不匹配文本字段中的“任何字符”吗? 谢了。 > 创建索引