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).

5
  • I have answered exact question here Commented Feb 10, 2016 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. Commented Feb 10, 2016 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. Commented Feb 10, 2016 at 22:44
  • I can't think of anything else. These are only 2 solutions I am aware of. Commented Feb 10, 2016 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. Commented Feb 11, 2016 at 5:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.