Hibernate Search Lucene. Suggestion but almost like SQL “LIKE”2019 Community Moderator ElectionHibernate Search: How to use wildcards correctly?Comparison of full text search engine - Lucene, Sphinx, Postgresql, MySQL?Hibernate show real SQLwhat analzyer is good for my situation? hibernate search caseAnd operator in Hibernate (lucene) searchHibernate lucene search include “the” and “a” as part of searchLucene Hibernate-Search - Filtering Query-StringSearching Solr index for concatenated wordsHow to handle synonyms and stop words when building a fuzzy query with Hibernate Search Query DSLhibernate query not searching beyond 2 characters for some strings and not working on multi word searchhow to retrieve exact search results with multiple query strings in hibernate search

How can I manipulate the output of Information?

Do I really need to have a scientific explanation for my premise?

Giving a career talk in my old university, how prominently should I tell students my salary?

What's the 'present simple' form of the word "нашла́" in 3rd person singular female?

What is Earthy controling in the ISS cupola?

What are some noteworthy "mic-drop" moments in math?

Create chunks from an array

Are all players supposed to be able to see each others' character sheets?

Why does liquid water form when we exhale on a mirror?

What do you call someone who likes to pick fights?

Why does cron require MTA for logging?

Virginia employer terminated employee and wants signing bonus returned

Trig Subsitution When There's No Square Root

What do *foreign films* mean for an American?

Minimizing with differential evolution

What materials can be used to make a humanoid skin warm?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

Conservation of Mass and Energy

Should I take out a loan for a friend to invest on my behalf?

How to draw dashed arc of a circle behind pyramid?

Crossing a border with an infant of a different citizenship

Why couldn't the separatists legally leave the Republic?

Am I understanding this Storm King's Thunder map wrong?

Is divide-by-zero a security vulnerability?



Hibernate Search Lucene. Suggestion but almost like SQL “LIKE”



2019 Community Moderator ElectionHibernate Search: How to use wildcards correctly?Comparison of full text search engine - Lucene, Sphinx, Postgresql, MySQL?Hibernate show real SQLwhat analzyer is good for my situation? hibernate search caseAnd operator in Hibernate (lucene) searchHibernate lucene search include “the” and “a” as part of searchLucene Hibernate-Search - Filtering Query-StringSearching Solr index for concatenated wordsHow to handle synonyms and stop words when building a fuzzy query with Hibernate Search Query DSLhibernate query not searching beyond 2 characters for some strings and not working on multi word searchhow to retrieve exact search results with multiple query strings in hibernate search










0















Its my first time dealing with optimized search functionality, and part of my proficiency is on the front end of android development, but I'm willing to take the adventure of hibernate-search. I do understand the functionality of SQL "LIKE" query, what it does and its limitation, thats the reason why I jumped straight ahead to hibernate-search (lucene), my goal is to have an auto suggestion based on inputs(input queries). This is what I got so far



@Indexed
@Table (name = "shop_table")
@Entity
@AnalyzerDef(name = "myanalyzer",
tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class), //
filters = //
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = WordDelimiterFilterFactory.class),
@TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
@Parameter(name = "maxGramSize", value = "1024") ),)
@Analyzer(definition = "myanalyzer")
public class Shop implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
enter code here

@Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
@Column(name = "name")
private String name;

... other methods


My query



 Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


Its just a basic query, and I focus solely on the analyzer configuration to get what I want, its really confusing which part should I focus on to achieve what I want, the Tokenizing? the Filtering? or the Query itself?
anyway I have these 2 phrases already indexed.



"Apache Lychee Department" 
"Apache Strawberry Club Large"


When I process/query "Straw" it gives me the Apache Strawberry Club Large
but when I process/query "Lychee" or "Apache Lychee" the query gives me both? Im only expecting Apache Lychee Department



The way I understand all my configuration is/are



EdgeNGramFilterFactory (1024) will give me a series of 1,024 index of EdgeNGrams



LowerCaseFilterFactory will give me all lower-cased indeces



WordDelimiterFilterFactory filter it by making the query as one word, and give me the matching data.



and every entry/data will be tokenized as a Keyword by KeywordTokenizerFactory and will be indexed 1,024 by EdgeNGram



I tried to query a phrase, but still getting the same output



 Query luceneQuery = qb.phrase().onField("name").sentence(searchTerm).createQuery();


My Goal is to have an auto-suggestion.. or atleast start with mimicking "LIKE" of sql..










share|improve this question









New contributor




Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    0















    Its my first time dealing with optimized search functionality, and part of my proficiency is on the front end of android development, but I'm willing to take the adventure of hibernate-search. I do understand the functionality of SQL "LIKE" query, what it does and its limitation, thats the reason why I jumped straight ahead to hibernate-search (lucene), my goal is to have an auto suggestion based on inputs(input queries). This is what I got so far



    @Indexed
    @Table (name = "shop_table")
    @Entity
    @AnalyzerDef(name = "myanalyzer",
    tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class), //
    filters = //
    @TokenFilterDef(factory = LowerCaseFilterFactory.class),
    @TokenFilterDef(factory = WordDelimiterFilterFactory.class),
    @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
    @Parameter(name = "maxGramSize", value = "1024") ),)
    @Analyzer(definition = "myanalyzer")
    public class Shop implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    enter code here

    @Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
    @Column(name = "name")
    private String name;

    ... other methods


    My query



     Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


    Its just a basic query, and I focus solely on the analyzer configuration to get what I want, its really confusing which part should I focus on to achieve what I want, the Tokenizing? the Filtering? or the Query itself?
    anyway I have these 2 phrases already indexed.



    "Apache Lychee Department" 
    "Apache Strawberry Club Large"


    When I process/query "Straw" it gives me the Apache Strawberry Club Large
    but when I process/query "Lychee" or "Apache Lychee" the query gives me both? Im only expecting Apache Lychee Department



    The way I understand all my configuration is/are



    EdgeNGramFilterFactory (1024) will give me a series of 1,024 index of EdgeNGrams



    LowerCaseFilterFactory will give me all lower-cased indeces



    WordDelimiterFilterFactory filter it by making the query as one word, and give me the matching data.



    and every entry/data will be tokenized as a Keyword by KeywordTokenizerFactory and will be indexed 1,024 by EdgeNGram



    I tried to query a phrase, but still getting the same output



     Query luceneQuery = qb.phrase().onField("name").sentence(searchTerm).createQuery();


    My Goal is to have an auto-suggestion.. or atleast start with mimicking "LIKE" of sql..










    share|improve this question









    New contributor




    Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      0












      0








      0








      Its my first time dealing with optimized search functionality, and part of my proficiency is on the front end of android development, but I'm willing to take the adventure of hibernate-search. I do understand the functionality of SQL "LIKE" query, what it does and its limitation, thats the reason why I jumped straight ahead to hibernate-search (lucene), my goal is to have an auto suggestion based on inputs(input queries). This is what I got so far



      @Indexed
      @Table (name = "shop_table")
      @Entity
      @AnalyzerDef(name = "myanalyzer",
      tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class), //
      filters = //
      @TokenFilterDef(factory = LowerCaseFilterFactory.class),
      @TokenFilterDef(factory = WordDelimiterFilterFactory.class),
      @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
      @Parameter(name = "maxGramSize", value = "1024") ),)
      @Analyzer(definition = "myanalyzer")
      public class Shop implements Serializable {

      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      enter code here

      @Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
      @Column(name = "name")
      private String name;

      ... other methods


      My query



       Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


      Its just a basic query, and I focus solely on the analyzer configuration to get what I want, its really confusing which part should I focus on to achieve what I want, the Tokenizing? the Filtering? or the Query itself?
      anyway I have these 2 phrases already indexed.



      "Apache Lychee Department" 
      "Apache Strawberry Club Large"


      When I process/query "Straw" it gives me the Apache Strawberry Club Large
      but when I process/query "Lychee" or "Apache Lychee" the query gives me both? Im only expecting Apache Lychee Department



      The way I understand all my configuration is/are



      EdgeNGramFilterFactory (1024) will give me a series of 1,024 index of EdgeNGrams



      LowerCaseFilterFactory will give me all lower-cased indeces



      WordDelimiterFilterFactory filter it by making the query as one word, and give me the matching data.



      and every entry/data will be tokenized as a Keyword by KeywordTokenizerFactory and will be indexed 1,024 by EdgeNGram



      I tried to query a phrase, but still getting the same output



       Query luceneQuery = qb.phrase().onField("name").sentence(searchTerm).createQuery();


      My Goal is to have an auto-suggestion.. or atleast start with mimicking "LIKE" of sql..










      share|improve this question









      New contributor




      Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      Its my first time dealing with optimized search functionality, and part of my proficiency is on the front end of android development, but I'm willing to take the adventure of hibernate-search. I do understand the functionality of SQL "LIKE" query, what it does and its limitation, thats the reason why I jumped straight ahead to hibernate-search (lucene), my goal is to have an auto suggestion based on inputs(input queries). This is what I got so far



      @Indexed
      @Table (name = "shop_table")
      @Entity
      @AnalyzerDef(name = "myanalyzer",
      tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class), //
      filters = //
      @TokenFilterDef(factory = LowerCaseFilterFactory.class),
      @TokenFilterDef(factory = WordDelimiterFilterFactory.class),
      @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
      @Parameter(name = "maxGramSize", value = "1024") ),)
      @Analyzer(definition = "myanalyzer")
      public class Shop implements Serializable {

      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      enter code here

      @Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
      @Column(name = "name")
      private String name;

      ... other methods


      My query



       Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


      Its just a basic query, and I focus solely on the analyzer configuration to get what I want, its really confusing which part should I focus on to achieve what I want, the Tokenizing? the Filtering? or the Query itself?
      anyway I have these 2 phrases already indexed.



      "Apache Lychee Department" 
      "Apache Strawberry Club Large"


      When I process/query "Straw" it gives me the Apache Strawberry Club Large
      but when I process/query "Lychee" or "Apache Lychee" the query gives me both? Im only expecting Apache Lychee Department



      The way I understand all my configuration is/are



      EdgeNGramFilterFactory (1024) will give me a series of 1,024 index of EdgeNGrams



      LowerCaseFilterFactory will give me all lower-cased indeces



      WordDelimiterFilterFactory filter it by making the query as one word, and give me the matching data.



      and every entry/data will be tokenized as a Keyword by KeywordTokenizerFactory and will be indexed 1,024 by EdgeNGram



      I tried to query a phrase, but still getting the same output



       Query luceneQuery = qb.phrase().onField("name").sentence(searchTerm).createQuery();


      My Goal is to have an auto-suggestion.. or atleast start with mimicking "LIKE" of sql..







      hibernate lucene hibernate-search lexical-analysis






      share|improve this question









      New contributor




      Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Mar 7 at 5:26







      Nestor Briaga













      New contributor




      Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Mar 7 at 5:18









      Nestor BriagaNestor Briaga

      32




      32




      New contributor




      Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          0














          There are two things you should take into account:



          • By default, when there are multiple terms in a query, the results will include documents that match any of the terms, not all of the terms.

          • By default, your queries will be analyzed using the same analyzer you used when indexing.

          This means in particular that your query "Lychee" will be analyzed to something like "L Ly Lyc Lych Lyche Lychee" (because of the edge ngram filter). The string "Apache Strawberry Club Large" because it was previously analyzed and the term "Large" was expanded to "L La Lar Larg Large" because of the edge ngram filter. So the query "Lychee" will match "Apache Strawberry Club Large", simply because they both contain a word that starts with L...



          That's obviously undesired behavior.



          The first step would be to change the way your query is analyzed, so that you don't end up matching completely unrelated documents.
          Basically you will need to define another analyzer which is almost identical, but does not have the "edge ngram" filter. Then you will need to tell Hibernate Search to use that analyzer to analyze your query.



          See this answer for a detailed explanation.



          As a second step, you need to make your query match if all the included terms are present in a document. To that end, the easiest solution would be to use the simple query string query instead of the keyword query.



          Replace this:



          Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


          With this:



          Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(searchTerm).createQuery();


          The key being the call to .withAndAsDefaultOperator().



          This change will have several other effects, such as enabling special syntax in the input string, so I'd encourage your to read the reference documentation to know what simpleQueryString is about exactly.






          share|improve this answer























          • Sorry for the late response, Thank you so much for this very informative assistance, I'm atleast on the right direction. Thank you

            – Nestor Briaga
            3 hours ago











          • And also, I didnt realize that NGram will create Index for each word, i though it will only index per entry.. didnt expect that for every white space, it will create an index..

            – Nestor Briaga
            2 hours ago











          • Hello again, I made it work, i checked the link you gave me and I didnt know you can create a separate analyzer apart from creating a first analyzer, but would you mind checking the code I did, if Im making the approach right?

            – Nestor Briaga
            1 hour ago


















          0














          I made it work by this, thanks to @yrodiere



          @Indexed
          @Table (name = "shop_table")
          @Entity
          @AnalyzerDef(name = "edgeNgram",
          tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
          filters =
          @TokenFilterDef(factory = LowerCaseFilterFactory.class),
          @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
          @Parameter(name = "maxGramSize", value = "1024") ),
          )
          @AnalyzerDef(name = "search_query_analyzer",
          tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
          filters =
          @TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
          @TokenFilterDef(factory = LowerCaseFilterFactory.class)
          )
          public class Shop implements Serializable

          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Long id;


          @Field(store = Store.YES, analyze = Analyze.YES)
          @Column(name = "name")
          @Analyzer(definition = "edgeNgram")
          private String name;

          public void setName(String name)
          this.name = name;


          public String getName()
          return this.name;




          and my query



           QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Shop.class)
          .overridesForField("name", "search_query_analyzer").get();

          Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(shopSearchTerm).createQuery();


          But im not sure if Im implementing it on proper approach..






          share|improve this answer










          New contributor




          Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.



















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Nestor Briaga is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036564%2fhibernate-search-lucene-suggestion-but-almost-like-sql-like%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            There are two things you should take into account:



            • By default, when there are multiple terms in a query, the results will include documents that match any of the terms, not all of the terms.

            • By default, your queries will be analyzed using the same analyzer you used when indexing.

            This means in particular that your query "Lychee" will be analyzed to something like "L Ly Lyc Lych Lyche Lychee" (because of the edge ngram filter). The string "Apache Strawberry Club Large" because it was previously analyzed and the term "Large" was expanded to "L La Lar Larg Large" because of the edge ngram filter. So the query "Lychee" will match "Apache Strawberry Club Large", simply because they both contain a word that starts with L...



            That's obviously undesired behavior.



            The first step would be to change the way your query is analyzed, so that you don't end up matching completely unrelated documents.
            Basically you will need to define another analyzer which is almost identical, but does not have the "edge ngram" filter. Then you will need to tell Hibernate Search to use that analyzer to analyze your query.



            See this answer for a detailed explanation.



            As a second step, you need to make your query match if all the included terms are present in a document. To that end, the easiest solution would be to use the simple query string query instead of the keyword query.



            Replace this:



            Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


            With this:



            Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(searchTerm).createQuery();


            The key being the call to .withAndAsDefaultOperator().



            This change will have several other effects, such as enabling special syntax in the input string, so I'd encourage your to read the reference documentation to know what simpleQueryString is about exactly.






            share|improve this answer























            • Sorry for the late response, Thank you so much for this very informative assistance, I'm atleast on the right direction. Thank you

              – Nestor Briaga
              3 hours ago











            • And also, I didnt realize that NGram will create Index for each word, i though it will only index per entry.. didnt expect that for every white space, it will create an index..

              – Nestor Briaga
              2 hours ago











            • Hello again, I made it work, i checked the link you gave me and I didnt know you can create a separate analyzer apart from creating a first analyzer, but would you mind checking the code I did, if Im making the approach right?

              – Nestor Briaga
              1 hour ago















            0














            There are two things you should take into account:



            • By default, when there are multiple terms in a query, the results will include documents that match any of the terms, not all of the terms.

            • By default, your queries will be analyzed using the same analyzer you used when indexing.

            This means in particular that your query "Lychee" will be analyzed to something like "L Ly Lyc Lych Lyche Lychee" (because of the edge ngram filter). The string "Apache Strawberry Club Large" because it was previously analyzed and the term "Large" was expanded to "L La Lar Larg Large" because of the edge ngram filter. So the query "Lychee" will match "Apache Strawberry Club Large", simply because they both contain a word that starts with L...



            That's obviously undesired behavior.



            The first step would be to change the way your query is analyzed, so that you don't end up matching completely unrelated documents.
            Basically you will need to define another analyzer which is almost identical, but does not have the "edge ngram" filter. Then you will need to tell Hibernate Search to use that analyzer to analyze your query.



            See this answer for a detailed explanation.



            As a second step, you need to make your query match if all the included terms are present in a document. To that end, the easiest solution would be to use the simple query string query instead of the keyword query.



            Replace this:



            Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


            With this:



            Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(searchTerm).createQuery();


            The key being the call to .withAndAsDefaultOperator().



            This change will have several other effects, such as enabling special syntax in the input string, so I'd encourage your to read the reference documentation to know what simpleQueryString is about exactly.






            share|improve this answer























            • Sorry for the late response, Thank you so much for this very informative assistance, I'm atleast on the right direction. Thank you

              – Nestor Briaga
              3 hours ago











            • And also, I didnt realize that NGram will create Index for each word, i though it will only index per entry.. didnt expect that for every white space, it will create an index..

              – Nestor Briaga
              2 hours ago











            • Hello again, I made it work, i checked the link you gave me and I didnt know you can create a separate analyzer apart from creating a first analyzer, but would you mind checking the code I did, if Im making the approach right?

              – Nestor Briaga
              1 hour ago













            0












            0








            0







            There are two things you should take into account:



            • By default, when there are multiple terms in a query, the results will include documents that match any of the terms, not all of the terms.

            • By default, your queries will be analyzed using the same analyzer you used when indexing.

            This means in particular that your query "Lychee" will be analyzed to something like "L Ly Lyc Lych Lyche Lychee" (because of the edge ngram filter). The string "Apache Strawberry Club Large" because it was previously analyzed and the term "Large" was expanded to "L La Lar Larg Large" because of the edge ngram filter. So the query "Lychee" will match "Apache Strawberry Club Large", simply because they both contain a word that starts with L...



            That's obviously undesired behavior.



            The first step would be to change the way your query is analyzed, so that you don't end up matching completely unrelated documents.
            Basically you will need to define another analyzer which is almost identical, but does not have the "edge ngram" filter. Then you will need to tell Hibernate Search to use that analyzer to analyze your query.



            See this answer for a detailed explanation.



            As a second step, you need to make your query match if all the included terms are present in a document. To that end, the easiest solution would be to use the simple query string query instead of the keyword query.



            Replace this:



            Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


            With this:



            Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(searchTerm).createQuery();


            The key being the call to .withAndAsDefaultOperator().



            This change will have several other effects, such as enabling special syntax in the input string, so I'd encourage your to read the reference documentation to know what simpleQueryString is about exactly.






            share|improve this answer













            There are two things you should take into account:



            • By default, when there are multiple terms in a query, the results will include documents that match any of the terms, not all of the terms.

            • By default, your queries will be analyzed using the same analyzer you used when indexing.

            This means in particular that your query "Lychee" will be analyzed to something like "L Ly Lyc Lych Lyche Lychee" (because of the edge ngram filter). The string "Apache Strawberry Club Large" because it was previously analyzed and the term "Large" was expanded to "L La Lar Larg Large" because of the edge ngram filter. So the query "Lychee" will match "Apache Strawberry Club Large", simply because they both contain a word that starts with L...



            That's obviously undesired behavior.



            The first step would be to change the way your query is analyzed, so that you don't end up matching completely unrelated documents.
            Basically you will need to define another analyzer which is almost identical, but does not have the "edge ngram" filter. Then you will need to tell Hibernate Search to use that analyzer to analyze your query.



            See this answer for a detailed explanation.



            As a second step, you need to make your query match if all the included terms are present in a document. To that end, the easiest solution would be to use the simple query string query instead of the keyword query.



            Replace this:



            Query lucenQuery = qb.keyword().onField("name").matching(searchTerm).createQuery();


            With this:



            Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(searchTerm).createQuery();


            The key being the call to .withAndAsDefaultOperator().



            This change will have several other effects, such as enabling special syntax in the input string, so I'd encourage your to read the reference documentation to know what simpleQueryString is about exactly.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 days ago









            yrodiereyrodiere

            2,7961315




            2,7961315












            • Sorry for the late response, Thank you so much for this very informative assistance, I'm atleast on the right direction. Thank you

              – Nestor Briaga
              3 hours ago











            • And also, I didnt realize that NGram will create Index for each word, i though it will only index per entry.. didnt expect that for every white space, it will create an index..

              – Nestor Briaga
              2 hours ago











            • Hello again, I made it work, i checked the link you gave me and I didnt know you can create a separate analyzer apart from creating a first analyzer, but would you mind checking the code I did, if Im making the approach right?

              – Nestor Briaga
              1 hour ago

















            • Sorry for the late response, Thank you so much for this very informative assistance, I'm atleast on the right direction. Thank you

              – Nestor Briaga
              3 hours ago











            • And also, I didnt realize that NGram will create Index for each word, i though it will only index per entry.. didnt expect that for every white space, it will create an index..

              – Nestor Briaga
              2 hours ago











            • Hello again, I made it work, i checked the link you gave me and I didnt know you can create a separate analyzer apart from creating a first analyzer, but would you mind checking the code I did, if Im making the approach right?

              – Nestor Briaga
              1 hour ago
















            Sorry for the late response, Thank you so much for this very informative assistance, I'm atleast on the right direction. Thank you

            – Nestor Briaga
            3 hours ago





            Sorry for the late response, Thank you so much for this very informative assistance, I'm atleast on the right direction. Thank you

            – Nestor Briaga
            3 hours ago













            And also, I didnt realize that NGram will create Index for each word, i though it will only index per entry.. didnt expect that for every white space, it will create an index..

            – Nestor Briaga
            2 hours ago





            And also, I didnt realize that NGram will create Index for each word, i though it will only index per entry.. didnt expect that for every white space, it will create an index..

            – Nestor Briaga
            2 hours ago













            Hello again, I made it work, i checked the link you gave me and I didnt know you can create a separate analyzer apart from creating a first analyzer, but would you mind checking the code I did, if Im making the approach right?

            – Nestor Briaga
            1 hour ago





            Hello again, I made it work, i checked the link you gave me and I didnt know you can create a separate analyzer apart from creating a first analyzer, but would you mind checking the code I did, if Im making the approach right?

            – Nestor Briaga
            1 hour ago













            0














            I made it work by this, thanks to @yrodiere



            @Indexed
            @Table (name = "shop_table")
            @Entity
            @AnalyzerDef(name = "edgeNgram",
            tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
            filters =
            @TokenFilterDef(factory = LowerCaseFilterFactory.class),
            @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
            @Parameter(name = "maxGramSize", value = "1024") ),
            )
            @AnalyzerDef(name = "search_query_analyzer",
            tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
            filters =
            @TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
            @TokenFilterDef(factory = LowerCaseFilterFactory.class)
            )
            public class Shop implements Serializable

            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            private Long id;


            @Field(store = Store.YES, analyze = Analyze.YES)
            @Column(name = "name")
            @Analyzer(definition = "edgeNgram")
            private String name;

            public void setName(String name)
            this.name = name;


            public String getName()
            return this.name;




            and my query



             QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Shop.class)
            .overridesForField("name", "search_query_analyzer").get();

            Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(shopSearchTerm).createQuery();


            But im not sure if Im implementing it on proper approach..






            share|improve this answer










            New contributor




            Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.
























              0














              I made it work by this, thanks to @yrodiere



              @Indexed
              @Table (name = "shop_table")
              @Entity
              @AnalyzerDef(name = "edgeNgram",
              tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
              filters =
              @TokenFilterDef(factory = LowerCaseFilterFactory.class),
              @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
              @Parameter(name = "maxGramSize", value = "1024") ),
              )
              @AnalyzerDef(name = "search_query_analyzer",
              tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
              filters =
              @TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
              @TokenFilterDef(factory = LowerCaseFilterFactory.class)
              )
              public class Shop implements Serializable

              @Id
              @GeneratedValue(strategy = GenerationType.IDENTITY)
              private Long id;


              @Field(store = Store.YES, analyze = Analyze.YES)
              @Column(name = "name")
              @Analyzer(definition = "edgeNgram")
              private String name;

              public void setName(String name)
              this.name = name;


              public String getName()
              return this.name;




              and my query



               QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Shop.class)
              .overridesForField("name", "search_query_analyzer").get();

              Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(shopSearchTerm).createQuery();


              But im not sure if Im implementing it on proper approach..






              share|improve this answer










              New contributor




              Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                0












                0








                0







                I made it work by this, thanks to @yrodiere



                @Indexed
                @Table (name = "shop_table")
                @Entity
                @AnalyzerDef(name = "edgeNgram",
                tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
                filters =
                @TokenFilterDef(factory = LowerCaseFilterFactory.class),
                @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
                @Parameter(name = "maxGramSize", value = "1024") ),
                )
                @AnalyzerDef(name = "search_query_analyzer",
                tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
                filters =
                @TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
                @TokenFilterDef(factory = LowerCaseFilterFactory.class)
                )
                public class Shop implements Serializable

                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private Long id;


                @Field(store = Store.YES, analyze = Analyze.YES)
                @Column(name = "name")
                @Analyzer(definition = "edgeNgram")
                private String name;

                public void setName(String name)
                this.name = name;


                public String getName()
                return this.name;




                and my query



                 QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Shop.class)
                .overridesForField("name", "search_query_analyzer").get();

                Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(shopSearchTerm).createQuery();


                But im not sure if Im implementing it on proper approach..






                share|improve this answer










                New contributor




                Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                I made it work by this, thanks to @yrodiere



                @Indexed
                @Table (name = "shop_table")
                @Entity
                @AnalyzerDef(name = "edgeNgram",
                tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
                filters =
                @TokenFilterDef(factory = LowerCaseFilterFactory.class),
                @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params =
                @Parameter(name = "maxGramSize", value = "1024") ),
                )
                @AnalyzerDef(name = "search_query_analyzer",
                tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
                filters =
                @TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
                @TokenFilterDef(factory = LowerCaseFilterFactory.class)
                )
                public class Shop implements Serializable

                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private Long id;


                @Field(store = Store.YES, analyze = Analyze.YES)
                @Column(name = "name")
                @Analyzer(definition = "edgeNgram")
                private String name;

                public void setName(String name)
                this.name = name;


                public String getName()
                return this.name;




                and my query



                 QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Shop.class)
                .overridesForField("name", "search_query_analyzer").get();

                Query lucenQuery = qb.simpleQueryString().onField("name").withAndAsDefaultOperator().matching(shopSearchTerm).createQuery();


                But im not sure if Im implementing it on proper approach..







                share|improve this answer










                New contributor




                Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer








                edited 32 mins ago





















                New contributor




                Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 1 hour ago









                Nestor BriagaNestor Briaga

                32




                32




                New contributor




                Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Nestor Briaga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                    Nestor Briaga is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    Nestor Briaga is a new contributor. Be nice, and check out our Code of Conduct.












                    Nestor Briaga is a new contributor. Be nice, and check out our Code of Conduct.











                    Nestor Briaga is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036564%2fhibernate-search-lucene-suggestion-but-almost-like-sql-like%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived