# Prefix searching

**URL:** <https://discuss.elastic.co/t/prefix-searching/5209>\
**Category:** Elasticsearch\
**Created:** [August 20, 2011, 11:23pm UTC](https://discuss.elastic.co/t/prefix-searching/5209 "2011-08-20T23:23:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcusLongmuir](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@MarcusLongmuir](https://discuss.elastic.co/u/MarcusLongmuir)\
**Post date:** [August 20, 2011, 11:23pm UTC](https://discuss.elastic.co/t/prefix-searching/5209/1 "2011-08-20T23:23:22Z")

</div>

I can't seem to get prefix searching to work at all. I'm using the  
Java API with the following:

QueryBuilder qb = QueryBuilders.boolQuery()  
.must(QueryBuilders.prefixQuery("Path","/Marcus/"));

SearchResponse response = client.prepareSearch("objects")  
.setQuery(qb)  
.setExplain(true)  
.execute()  
.actionGet();

SearchHits sh = response.getHits();

System.out.println("Num of results: " + sh.getTotalHits());

I get zero results every time and the data definitely fits the query.  
I assume that the above search would return a record with a "Path"  
value of "/Marcus/Example". This record exists and so do many others  
like it.

I haven't tuned ElasticSearch at all and I don't know if this requires  
explicit indexing, but it doesn't work via ElasticSearch Head either  
so I think it must be on the server side.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 21, 2011, 12:47am UTC](https://discuss.elastic.co/t/prefix-searching/5209/2 "2011-08-21T00:47:08Z")

</div>

IMO I think that with default mapping, your field "path" is tokenized. You should use a keyword analyzer for this field if you don't want it to be tokenized.

A prefix search on "marcus" with default mapping should give you results.

Hope this helps.

David 😉

Le 21 août 2011 à 01:23, marcuslongmuir [marcuslongmuir@me.com](mailto:marcuslongmuir@me.com) a écrit :

> I can't seem to get prefix searching to work at all. I'm using the  
> Java API with the following:
> 
> QueryBuilder qb = QueryBuilders.boolQuery()  
> .must(QueryBuilders.prefixQuery("Path","/Marcus/"));
> 
> SearchResponse response = client.prepareSearch("objects")  
> .setQuery(qb)  
> .setExplain(true)  
> .execute()  
> .actionGet();
> 
> SearchHits sh = response.getHits();
> 
> System.out.println("Num of results: " + sh.getTotalHits());
> 
> I get zero results every time and the data definitely fits the query.  
> I assume that the above search would return a record with a "Path"  
> value of "/Marcus/Example". This record exists and so do many others  
> like it.
> 
> I haven't tuned Elasticsearch at all and I don't know if this requires  
> explicit indexing, but it doesn't work via Elasticsearch Head either  
> so I think it must be on the server side.
> 
> Thanks for any help.

---

<div class="post-metadata">

**Author:** ![MarcusLongmuir](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@MarcusLongmuir](https://discuss.elastic.co/u/MarcusLongmuir)\
**Post date:** [August 21, 2011, 2:11am UTC](https://discuss.elastic.co/t/prefix-searching/5209/3 "2011-08-21T02:11:47Z")

</div>

Thanks for the advice David. I've only just started using  
Elasticsearch, could you explain how to do this?

From your explanation I understand that the field is being indexed  
_only_ as the keywords, which in this case means that I can't use  
prefix to find records that begin with the exact string.

Assuming no configuration, what do I need to set in order for the  
following to occur?

Index records with "Path" values:  
"/Marcus/Example/One"  
"/Marcus/Example/"  
"/Marcus/"

And these be returned when I use a prefix search with the value "/  
Marcus/".

Thanks

On Aug 21, 1:47 am, David Pilato [da...@pilato.fr](mailto:da...@pilato.fr) wrote:

> IMO I think that with default mapping, your field "path" is tokenized. You should use a keyword analyzer for this field if you don't want it to be tokenized.
> 
> A prefix search on "marcus" with default mapping should give you results.
> 
> Hope this helps.
> 
> David 😉
> 
> Le 21 août 2011 à 01:23, marcuslongmuir [marcuslongm...@me.com](mailto:marcuslongm...@me.com) a écrit :
> 
> > I can't seem to get prefix searching to work at all. I'm using the  
> > Java API with the following:
> 
> > QueryBuilder qb = QueryBuilders.boolQuery()  
> > .must(QueryBuilders.prefixQuery("Path","/Marcus/"));
> 
> > SearchResponse response = client.prepareSearch("objects")  
> > .setQuery(qb)  
> > .setExplain(true)  
> > .execute()  
> > .actionGet();
> 
> > SearchHits sh = response.getHits();
> 
> > System.out.println("Num of results: " + sh.getTotalHits());
> 
> > I get zero results every time and the data definitely fits the query.  
> > I assume that the above search would return a record with a "Path"  
> > value of "/Marcus/Example". This record exists and so do many others  
> > like it.
> 
> > I haven't tuned Elasticsearch at all and I don't know if this requires  
> > explicit indexing, but it doesn't work via Elasticsearch Head either  
> > so I think it must be on the server side.
> 
> > Thanks for any help.

---

<div class="post-metadata">

**Author:** ![MarcusLongmuir](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@MarcusLongmuir](https://discuss.elastic.co/u/MarcusLongmuir)\
**Post date:** [August 21, 2011, 3:01am UTC](https://discuss.elastic.co/t/prefix-searching/5209/4 "2011-08-21T03:01:10Z")

</div>

I've just got it working how I intended, but I couldn't work out how  
to do it via Java and just did it via CURL with the following:

curl -XPUT '[http://127.0.0.1:9200/objects/object/\_mapping](http://127.0.0.1:9200/objects/object/_mapping)' -d  
'{"object" : {"properties" : { "Path" : {"type":"string", "index" :  
"not\_analyzed"}}}}'

Although I've solved the exact issue I was having, how would I go  
about doing this in Java?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 22, 2011, 5:48am UTC](https://discuss.elastic.co/t/prefix-searching/5209/5 "2011-08-22T05:48:35Z")

</div>

I like to define my mappings as json files since it allows me to put them  
into source control:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

In Java, you can either:

- call addMapping when you create an index via the JavaAPI:  
[https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/create/CreateIndexRequestBuilder.java](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/create/CreateIndexRequestBuilder.java)

- or call the map(Map\<String, Object\> map) method on any XContentBuilder:  
[https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L904](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L904)

There are probably other ways to do it.

--  
Ivan

On Sat, Aug 20, 2011 at 11:01 PM, marcuslongmuir [marcuslongmuir@me.com](mailto:marcuslongmuir@me.com)wrote:

> I've just got it working how I intended, but I couldn't work out how  
> to do it via Java and just did it via CURL with the following:
> 
> curl -XPUT '[http://127.0.0.1:9200/objects/object/\_mapping](http://127.0.0.1:9200/objects/object/_mapping)' -d  
> '{"object" : {"properties" : { "Path" : {"type":"string", "index" :  
> "not\_analyzed"}}}}'
> 
> Although I've solved the exact issue I was having, how would I go  
> about doing this in Java?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:56am UTC](https://discuss.elastic.co/t/prefix-searching/5209/6 "2017-07-06T03:56:29Z")

</div>


