user11118940
user11118940

Reputation:

Elasticsearch auto-complete not working correctly

I have 2 product name called "satiny" when i try to type sat it doesn't appear. enter image description here

But when i type "sati" then "satiny" appears first then some other product and then my second "satiny" product. enter image description here

I have implemented Fuzzy search, Edge-ngram, Synonym search.

This is my Index :

{"settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym" : {
                    "type": "synonym",
                    "synonyms_path": "analysis/synonym.txt"                            
                        }

        },
        "analyzer": 
            {
              "synonym" : {
                    "tokenizer" : "whitespace",
                    "filter" : ["lowercase",
                    "synonym"]
                },

          "keyword_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding",
              "trim"
            ],
            "char_filter": [],
            "type": "custom",
            "tokenizer": "keyword"
          },
          "edge_ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "edge_ngram_tokenizer"
          },
          "edge_ngram_search_analyzer": {
            "tokenizer": "lowercase"
          }
            },  

        "tokenizer": {
          "edge_ngram_tokenizer": {
            "type": "edge_ngram",
            "min_gram": 1,
            "max_gram": 25,
            "token_chars": [
              "letter"
            ]
          }

  },
    "mappings": {
            "properties" : {
            "firebaseId":{
            "type":"text"
            },
                "name" : {
                   "type" : "text",
                   "analyzer" : "synonym"

                },
                "name_auto" : {
                "type": "text",

    "fields": {
      "edgengram": {
        "type": "text",
        "analyzer": "edge_ngram_analyzer",
        "search_analyzer": "edge_ngram_search_analyzer"
      },
      "completion": {
        "type": "completion"
      }

    }
                },

                "category_name" : {
                            "type": "text",
                                "fields": {
                                  "keyword": { 
                                    "type": "keyword"
                            }
                }
                },
                "storeName" : {
                    "type": "keyword"
                },
                "sku" : {
                    "type" : "text"
                },
                "price" : {
                    "type": "text",
                        "fields": {
                            "keyword": { 
                                "type": "keyword"
                            }
                }
                },
                "magento_id" : {
                    "type" : "text"
                },
                "seller_id" : {
                    "type" : "text"
                },
                "square_item_id" : {
                    "type" : "text"
                },
                "square_variation_id" : {
                    "type" : "text"
                },
                "typeId" : {
                    "type" : "text"
                }
            }
    }
}
}
}
}

And This is my query :

{
    "from": 0,
    "size": 50,
    "query": {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "match_phrase": {
                            "name_auto.edgengram": "sati"
                        }
                    }, {
                        "match_phrase": {
                            "name_auto": "sati"
                        }
                    }, {
                        "match": {
                            "name_auto": "sati"
                        }
                    }, {
                        "match": {
                            "category_name": {
                                "query": "sati",
                                "analyzer": "synonym"
                            }
                        }
                    }, {
                        "match_phrase": {
                            "category_name": "sati"
                        }
                    }, {
                        "match": {
                            "name": {
                                "query": "sati",
                                "fuzziness": 2,
                                "prefix_length": 1
                            }
                        }
                    }]
                }
            }]
        }
    }
}

when i type "sat" then the edge-ngram should start and show me the product named "satiny" first and then the fuzzy search implementation should start and show me the products with "sol" or "set" as such

I got it partially solved but still i want all the product names with "charcoal" to appear first .

enter image description here

Upvotes: 0

Views: 923

Answers (1)

Val
Val

Reputation: 217254

The problem seems to be that your mappings section is not located properly at the top-level next to settings. It is located inside settings.index.analysis instead, hence none of your fields is properly defined. It would help to properly format your JSON.

Create your index like this and it should work:

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "whitespace",
            "filter": [
              "lowercase"
            ]
          },
          "keyword_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding",
              "trim"
            ],
            "char_filter": [],
            "type": "custom",
            "tokenizer": "keyword"
          },
          "edge_ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "edge_ngram_tokenizer"
          },
          "edge_ngram_search_analyzer": {
            "tokenizer": "lowercase"
          }
        },
        "tokenizer": {
          "edge_ngram_tokenizer": {
            "type": "edge_ngram",
            "min_gram": 1,
            "max_gram": 25,
            "token_chars": [
              "letter"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "firebaseId": {
        "type": "text"
      },
      "name": {
        "type": "text",
        "analyzer": "synonym"
      },
      "name_auto": {
        "type": "text",
        "fields": {
          "edgengram": {
            "type": "text",
            "analyzer": "edge_ngram_analyzer",
            "search_analyzer": "edge_ngram_search_analyzer"
          },
          "completion": {
            "type": "completion"
          }
        }
      },
      "category_name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "storeName": {
        "type": "keyword"
      },
      "sku": {
        "type": "text"
      },
      "price": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "magento_id": {
        "type": "text"
      },
      "seller_id": {
        "type": "text"
      },
      "square_item_id": {
        "type": "text"
      },
      "square_variation_id": {
        "type": "text"
      },
      "typeId": {
        "type": "text"
      }
    }
  }
}

Upvotes: 2

Related Questions