Smart search


(Umar Memon) #1

Can elastic search has the feature of smart search, For example if i searches for the computer search result also retrive the content having laptop.
Is it possible with elastic search, if so any refrence.
I am using ES 5.X


(David Pilato) #2

Looks like you can do similar things by using synonyms.

But you need to provide the dictionary you want to use.


(Umar Memon) #3

Thank you for the response,
Can i get the path way for implementing synonyms by providing the Dictionary using the .Net Nest with Elasticsearch 5.X

Thanks in advance.


(David Pilato) #4

I don't write .Net / Nest code. Can't help here. Sorry.
But may be other readers can?


(Russ Cam) #5

Include a text file containing your synonyms in a path that is accessible relative to the elasticsearch config directory. For this example, let's create a computer_synonyms.txt file in <elasticsearch>/config/analysis with the following content

computer, laptop

Now, create an index with a custom analyzer with a synonym token filter that uses the synonyms file we've created

// Define a POCO type to map documents in Elasticsearch to
public class Document
{
    public string Title { get; set; }
}

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "examples";
var connectionSettings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

var client = new ElasticClient(connectionSettings);

var indexResponse = client.CreateIndex(defaultIndex, c => c
    .Settings(s => s
        .Analysis(a => a
            .TokenFilters(tf => tf
                // synonym token filter, using the synonyms file
                .Synonym("computer_synonyms_filter", stf => stf
                    .SynonymsPath("analysis/computer_synonyms.txt")
                )
            )
            .Analyzers(an => an
                // custom analyzer that uses a standard tokenizer
                // along with lowercase and our computer_synonyms token filters
                .Custom("computer_synonyms_analyzer", ca => ca
                    .Tokenizer("standard")
                    .Filters(
                        "lowercase",
                        "computer_synonyms_filter")
                )
            )
        )
    )
    .Mappings(m => m
        // map our document POCO type
        .Map<Document>(d => d
            .AutoMap()
            .Properties(p => p
                .Text(s => s
                    .Name(n => n.Title)
                    // specify our custom analyzer as the analyzer to use
                    // for the Title property
                    .Analyzer("computer_synonyms_analyzer")
                )
            )
        )
    )
);

With the index created, along with the custom analyzer and the mapping for our Document POCO type, let's index a document and then search for it

client.Index(new Document { Title = "laptop" }, i => i.Refresh(Refresh.WaitFor));

var searchResponse = client.Search<Document>(s => s
    .Query(q => q
        .Match(m => m
            .Field(f => f.Title)
            .Query("computer")
        )
    )
);

searchResponse returns the following json, demonstrating that our synonym token filter is working

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.46029136,
    "hits" : [
      {
        "_index" : "examples",
        "_type" : "document",
        "_id" : "AVnLQF4tcWwxFhzvdQ06",
        "_score" : 0.46029136,
        "_source" : {
          "title" : "laptop"
        }
      }
    ]
  }
}

(Umar Memon) #6

Thank You for responding......It will really healpful for me.....Will implement and give the feedback.


(system) #7

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.