简体   繁体   中英

Search substring in elastic search

Below is what I want-

Suppose in elastic search below are stored

  1. Quick Fox Test
  2. Quick Fox
  3. Quick 123 @ Test
  4. Quick
  5. Fox Test
  6. Fox Quick Test

Now when I search for

Quick then I must get

  • Quick Fox Test
  • Quick Fox
  • Quick 123 @ Test
  • Quick
  • Fox Quick Test

fox test then I must get

  • Quick Fox Test
  • Fox Test

Quick Test 123 then I must get

  • Empty response

123 @ then I must get

  • Quick 123 @ Test

123 $ then I must get

  • Empty response

You can use the match_phrase query to get the expected output and you need to use the whitespace tokenizer with a lowercase token filter to make it work. will add the working example.

Index mapping

{
  "settings": {
    "analysis": {
      "analyzer": {
        "lwhitespace": { 
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "lwhitespace"
      }
    }
  }
}

Index sample docs

{
    "message" : "Quick Fox Test"
}

{
    "message" : "Quick Fox"
}

{
    "message" : "Quick 123@ Test"
}

{
    "message" : "Quick"
}

{
    "message" : "Fox Test"
}

{
    "message" : "Fox Quick Test"
}

Search queries

{
    "query": {
        "match_phrase": {
            "message": "Quick"
        }
    }
}

Search result

 "hits": [
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.3147369,
                "_source": {
                    "message": "Quick"
                }
            },
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.25613075,
                "_source": {
                    "message": "Quick Fox"
                }
            },
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.21592417,
                "_source": {
                    "message": "Quick Fox Test"
                }
            },
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.21592417,
                "_source": {
                    "message": "Quick 123@ Test"
                }
            },
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "6",
                "_score": 0.21592417,
                "_source": {
                    "message": "Fox Quick Test"
                }
            }
        ]

Search query fox test

{
    "query": {
        "match_phrase": {
            "message": "fox test"
        }
    }
}
 "hits": [
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "5",
                "_score": 0.93851364,
                "_source": {
                    "message": "Fox Test"
                }
            },
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.79118896,
                "_source": {
                    "message": "Quick Fox Test"
                }
            }
        ]

Search query for 123@

    "query": {
        "match_phrase": {
            "message": "123@"
        }
    }
}

Search response

 "hits": [
            {
                "_index": "66287786",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.3792357,
                "_source": {
                    "message": "Quick 123@ Test"
                }
            }
        ]

Search query for Quick Test 123

{
    "query": {
        "match_phrase": {
            "message": "Quick Test 123"
        }
    }
}

And search result is empty

 "hits": []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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