how to write curl fuzzy search query in elastic search

ghz 8months ago ⋅ 57 views

I have created record in ES and can see it:

curl -XGET http://localhost:9200/companies_test/_search -H 'Content-Type: application/json' -d '{
      "query": {
          "match_all": {}
      }
  }'

returns

{
  "took": 38,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "companies_test",
        "_type": "_doc",
        "_id": "0",
        "_score": 1,
        "_source": {
          "regionName": "North America",
          "name": "OpenAI",
          "id": "0"
        }
      }
    ]
  }
}

But when I try to find it:

curl -XGET http://localhost:9200/companies_test/_search -H 'Content-Type: application/json' -d '{
      "query": {
          "fuzzy": {
            "name": {
              "value": "OpenAI",
              "fuzziness": "AUTO"
            }
          }
        }
  }'

it returns nothing:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

Probably my search query is incorrect. How to fix it? The idea is to fuzzy search companies by name in specific region and without region filter as well.

Answers

Your search query using the fuzzy query is correct, but there might be a problem with the way you indexed the data or how the fuzzy search is performed. Here are a few things you can check and try to fix the issue:

  1. Analyze the "name" Field: Ensure that the "name" field is analyzed properly during indexing. Fuzzy search works based on analyzed tokens, so if the "name" field is not analyzed or analyzed differently from what you expect, it may not match the fuzzy query.

  2. Check Fuzziness Setting: The "fuzziness" parameter determines how similar the matching terms need to be. Setting it to "AUTO" allows Elasticsearch to automatically determine an appropriate fuzziness based on the length of the term. You can try setting it to a specific value to see if it improves the results, such as "1" for edit distance of 1.

  3. Verify Mapping: Ensure that the mapping for the "name" field is correct and allows fuzzy searching. The field should be of type "text" and analyzed appropriately.

  4. Test with Simple Queries: Try performing a simple match query without fuzziness to see if you can retrieve the document. This can help identify if the issue is with the fuzzy search or with the indexing/querying process in general.

  5. Check Analyzers: If you're using custom analyzers or have specified an analyzer for the "name" field, make sure it's configured correctly and produces the expected tokens for fuzzy matching.

  6. Test Fuzzy Query on Other Fields: If possible, try performing a fuzzy query on other fields to see if it works as expected. This can help narrow down whether the issue is specific to the "name" field or with fuzzy search in general.

Once you've checked these aspects and made any necessary adjustments, retry your fuzzy search query to see if it returns the expected results. If the issue persists, further investigation may be needed, such as checking Elasticsearch logs for any errors or inconsistencies.