Wilcard search or partial matching in Elastic search

前端 未结 2 1604
广开言路
广开言路 2021-01-17 01:22

I am trying to provide the search to end user with type as they go which is is more like sqlserver. I was able to implement ES query for the given sql scenario:



        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 01:53

    Well my solution is not perfect and I am not sure about performance. So you should try it on your own risk :)

    This is es 5 version

    PUT likequery
    {
      "mappings": {
        "typename": {
          "properties": {
            "name": {
              "type": "string",
              "fields": {
                "raw": {
                  "type": "keyword"
                }
              }
            },
            "type": {
              "type": "string"
            }
          }
        }
      }
    }
    

    in ES 2.1 change "type": "keyword" to "type": "string", "index": "not_analyzed"

    PUT likequery/typename/1
    {
      "name": "peter tomson"
    }
    
    PUT likequery/typename/2
    {
      "name": "igor tkachenko"
    }
    
    PUT likequery/typename/3
    {
      "name": "taras shevchenko"
    }
    

    Query is case sensetive

    POST likequery/_search
    {
      "query": {
        "regexp": {
          "name.raw": ".*taras shev.*"
        }
      }
    }
    

    Response

    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "likequery",
            "_type": "typename",
            "_id": "3",
            "_score": 1,
            "fields": {
              "raw": [
                "taras shevchenko"
              ]
            }
          }
        ]
      }
    }
    

    PS. Once again I am not sure about performance of this query since it will use scan and not index.

提交回复
热议问题