Elasticsearch - Search with wildcards

前端 未结 1 742
猫巷女王i
猫巷女王i 2020-12-20 09:04

I\'ve managed to populate my index with 4 documents using this bulk request:

POST localhost:9200/titles/movies/_bulk

{\"index\":{\"_id\"         


        
相关标签:
1条回答
  • 2020-12-20 09:48

    Elasticsearch provides regex support in the the regular match query

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

    Gives you this

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

    To update to your question URI search, I'm not sure if it is possible, if you do it with curl you just omit the query dsl as data

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

    Update to latest question:

    Well if you want to sort by level, you need to provide a mapping for elasticsearch. What I did:

    Delete index

    DELETE titles
    

    Add mapping

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

    Refine Query DSL

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

    That gives me

    {
      "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"
            ]
          }
        ]
      }
    }
    
    0 讨论(0)
提交回复
热议问题