繁体   English   中英

Elasticsearch-使用通配符搜索

[英]Elasticsearch - Search with wildcards

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

POST 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"
        ]
      }
    ]
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM