3

I am trying to do a query which should be similar to the phrase query except that there can be gaps within consecutive tokens. Example:

Document: "a b c d"
Search: "a c" >> This should return the document,
Search: "c a" >> This should not return the document.

Phrase query ensures that the order will remain only when no slop value is being used. Once I add some slop value (2, 3 etc), it starts to break order, and with sufficiently large slop value, it will probably just be as normal match query as mentioned in elasticsearch documentation.

Note, I am using the AND operator, hence all the search tokens must be present.

Is it possible to do queries like this in Elasticsearch?

[EDIT] As for being marked as duplicate to this question, I am using spring-data-elasticsearch for my project and the stack overflow question does not have an answer that is related to spring-data elasticsearch java-api (ElasticsearchTemplate preferably).

Community
  • 1
  • 1
Zobayer Hasan
  • 2,187
  • 6
  • 22
  • 38
  • I have answered exact question [here](http://stackoverflow.com/questions/34452808/analyze-and-match-all-terms-in-same-order/34453643#34453643) – ChintanShah25 Feb 10 '16 at 19:23
  • @ChintanShah25 Correct me if I am wrong, span query is actually a form of term query and not a match query. Also, no I don't have the tokens prepared. My java project that supports search feature handles like 100+ fields and the last thing I want is to tokenize the search string myself. Infact, the queries are generated based on which fields are present or not from the ui. It will be hard to do like you did in your solution. – Zobayer Hasan Feb 10 '16 at 22:41
  • I have also read the other s.o. question you linked in your original reply, that one also requires me to tokenize the searchstring myself. This is very useful feature and I believe there is some more elegant solution that suits programming approaches. I am using spring-data-elasticsearch for my project. – Zobayer Hasan Feb 10 '16 at 22:44
  • I can't think of anything else. These are only 2 solutions I am aware of. – ChintanShah25 Feb 10 '16 at 22:54
  • @ChintanShah25 I have searched internet for last 2 days and found your solution and the other one you have pointed in that post. But I don't know how to get the tokenized list in java api. – Zobayer Hasan Feb 11 '16 at 05:53

0 Answers0