1

I've recently started using ElasticSearch and I can't seem to make it search for a part of a word.

Example: I have three documents from my couchdb indexed in ElasticSearch:

{
 "_id" : "1",
 "name" : "John Doeman",
 "function" : "Janitor"
}
{
 "_id" : "2",
 "name" : "Jane Doewoman",
 "function" : "Teacher"
}
{
 "_id" : "3",
 "name" : "Jimmy Jackal",
 "function" : "Student"
} 

So now, I want to search for all documents containing "Doe"

curl http://localhost:9200/my_idx/my_type/_search?q=Doe

That doesn't return any hits. But if I search for

curl http://localhost:9200/my_idx/my_type/_search?q=Doeman

It does return one document (John Doeman).

Am trying with this url http://localhost/el/index.php?str=*doe*.

<?php
require 'vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();

$params = array();
$params['index'] = 's3';
$params['type'] = 's3files';
$params['body']['query']['match']['content'] = $_GET['str'];

$result = $client->search($params);
echo "<pre>";
print_r($result);

How can I make ElasticSearch find both John Doeman and Jane Doewoman when I search for "Doe" ?

Mansoor H
  • 594
  • 1
  • 8
  • 25

2 Answers2

0

You can simply try using a wildcard first:

curl http://localhost:9200/my_idx/my_type/_search?q=Doe*

If that's enough, you can get away with that. Otherwise, there are other more advanced solutions by creating custom analyzers with edge ngram tokenizers and filters.

Val
  • 207,596
  • 13
  • 358
  • 360
  • Yes working fine but am using php code to search, it not showing correct result. – Mansoor H Feb 12 '16 at 06:36
  • 2
    Your PHP code uses a `match` query which is different from what the `q=` query does (i.e. it is a `query_string` query). Try this PHP code: `$params['body']['query']['query_string']['query'] = $_GET['str'];` – Val Feb 12 '16 at 06:40
  • One more doubt, @val `Array ( [index] => s3 [type] => s3files [id] => 1679 [body] => Array ( [filename] => Services.docx [content]=>test [user_id]=>35))` i want to search content and user_id in the query string. how can i do? – Mansoor H Feb 12 '16 at 06:56
  • How can i pass the $param? – Mansoor H Feb 12 '16 at 06:58
  • 1
    In order to search for the `content` field, you can do it like this: `$params['body']['query']['query_string']['query'] = 'content:' . $_GET['str'];` You can check the details of the query syntax [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax) – Val Feb 12 '16 at 07:04
0

You can Try this,Wildcard Query Matches documents that have fields matching a wildcard expression (not analyzed).

      curl http://localhost:9200/my_idx/my_type/_search
      {"query":
        {
       "wildcard" : { "name" : "doe*"  }
         }
      }  
krishna kumar
  • 1,190
  • 12
  • 14