2
votes

Searching values in multiple fields is not working when search text contains space. For example if i search for value "price" than i get results but if i search for "price level" than i get following error:

Can only use prefix queries on keyword and text fields - not on which is of type float

I have one date field and one float field in indexed documents. And error is thrown because of this fields.

Following is my code of creating Index:

var createIndexResponse = client.CreateIndex("messages", c => c.Settings(s => s.NumberOfShards(1).NumberOfReplicas(0).Analysis(a => a.Analyzers(anl => anl.Custom("default", ca => ca.Tokenizer("whitespace").Filters(new List<string>() { "lowercase" }))))).Mappings(ms => ms.Map<Object>(m => m.Properties(p => p.Number(s => s.Name("id").Index(false)).Text(s => s.Name("displaytext").Index(false)).Text(s => s.Name("text").Index(false)).Text(s => s.Name("url").Index(false)).Date(d => d.Name("Search_ReceivedOn"))))));

Following is my search query:

Dim funcMust = New List(Of Func(Of Nest.QueryContainerDescriptor(Of Object), Nest.QueryContainer))()
funcMust.Add(Function(sh) sh.Term("From", UserID) Or sh.Term("To", UserID))

Dim resp = client.Search(Of Object)(Function(s) s.Index("messages").IgnoreUnavailable(True) _
                            .Query(Function(qry) qry.QueryString(Function(qs) qs.Fields("Search_*").Query(searchText).DefaultOperator(Nest.Operator.And))) _
                            .PostFilter(Function(pf) pf.Bool(Function(b) b.Must(funcMust))).From((pageIndex - 1) * pageSize).Take(pageSize).Source(Function(x) x.Includes(Function(f) f.Fields(fields))))

I have dynamic columns in indexed document so i can not use term queries with exact field names. Is there any way to search on all fields that starts with "Search_" without error i have mentioned above?

1
where is the prefix query in your search query? You seem to be doing a query_string query?Russ Cam
@RussCam This is first time i am using elastic search and not sure why i am getting error like that. May be because my searchText variable has wildcard value like "price level".Sandeep
@Sandeep Did you a get a fix for this issue?Richa Sinha
final SimpleQueryStringBuilder queryBuilder = simpleQueryStringQuery(query) .field("*") // search all fields .analyzeWildcard(true).setLenient(true); // analyze wildcard to handle number based fields Maybe this helpsSimonH

1 Answers

0
votes

In my case this occurred when i perform search with spacial character.

I fixed this with following,

Elasticsearch - v6.8.6

Map<String, Float> fields = new HashMap<>();
        fields.put("content.keyword", 1f);
        fields.put("name.keyword", 2f);
        fields.put("tag.keyword", 3f);

SimpleQueryStringBuilder queryBuilder = new SimpleQueryStringBuilder(searchQuery);
queryBuilder.fields(fields);
queryBuilder.analyzeWildcard(Boolean.TRUE);