Searching names: increasing relevance for proximate matches in multivalued field












0















I'm trying to get a name field working reasonably in Elasticsearch, and am having trouble finding guidance. Please help me, Internet!



My documents have several authors, and so a multi-valued name field. Let's say I have a search for paul f tompkins, and two documents: {"authors": ["Paul Tompkins", "Dietrich Kohl"]} and {"authors": ["Paul Wang", "Darlene Tompkins"]}.



My search will retrieve both documents easily enough, but both will have the same score from the authors query. I'd like the fact that I matched multiple terms within the same item of the authors array to boost the score of the first document.



How can I do that? The two techniques that I know of for boosting for proximity are shingles (which I believe would generate the paul_f and f_tompkins shingles, neither of which match) and a phrase query with slop (which would fail because the f token isn't there).



Ideally I'd want something like a phrase slop query with minimum_should_match: I give it four words, it matches if at least two are present in the same array element, with each additional matching term in the same array element raising the score. I couldn't figure out how to do that.



(It wouldn't work for me to have client-side logic that tries to strip the f out of the query -- this is a simplified example, but assume that I also want to be able handle a queries like paul francis tompkins or paul f tompkins there will be blood.)










share|improve this question



























    0















    I'm trying to get a name field working reasonably in Elasticsearch, and am having trouble finding guidance. Please help me, Internet!



    My documents have several authors, and so a multi-valued name field. Let's say I have a search for paul f tompkins, and two documents: {"authors": ["Paul Tompkins", "Dietrich Kohl"]} and {"authors": ["Paul Wang", "Darlene Tompkins"]}.



    My search will retrieve both documents easily enough, but both will have the same score from the authors query. I'd like the fact that I matched multiple terms within the same item of the authors array to boost the score of the first document.



    How can I do that? The two techniques that I know of for boosting for proximity are shingles (which I believe would generate the paul_f and f_tompkins shingles, neither of which match) and a phrase query with slop (which would fail because the f token isn't there).



    Ideally I'd want something like a phrase slop query with minimum_should_match: I give it four words, it matches if at least two are present in the same array element, with each additional matching term in the same array element raising the score. I couldn't figure out how to do that.



    (It wouldn't work for me to have client-side logic that tries to strip the f out of the query -- this is a simplified example, but assume that I also want to be able handle a queries like paul francis tompkins or paul f tompkins there will be blood.)










    share|improve this question

























      0












      0








      0








      I'm trying to get a name field working reasonably in Elasticsearch, and am having trouble finding guidance. Please help me, Internet!



      My documents have several authors, and so a multi-valued name field. Let's say I have a search for paul f tompkins, and two documents: {"authors": ["Paul Tompkins", "Dietrich Kohl"]} and {"authors": ["Paul Wang", "Darlene Tompkins"]}.



      My search will retrieve both documents easily enough, but both will have the same score from the authors query. I'd like the fact that I matched multiple terms within the same item of the authors array to boost the score of the first document.



      How can I do that? The two techniques that I know of for boosting for proximity are shingles (which I believe would generate the paul_f and f_tompkins shingles, neither of which match) and a phrase query with slop (which would fail because the f token isn't there).



      Ideally I'd want something like a phrase slop query with minimum_should_match: I give it four words, it matches if at least two are present in the same array element, with each additional matching term in the same array element raising the score. I couldn't figure out how to do that.



      (It wouldn't work for me to have client-side logic that tries to strip the f out of the query -- this is a simplified example, but assume that I also want to be able handle a queries like paul francis tompkins or paul f tompkins there will be blood.)










      share|improve this question














      I'm trying to get a name field working reasonably in Elasticsearch, and am having trouble finding guidance. Please help me, Internet!



      My documents have several authors, and so a multi-valued name field. Let's say I have a search for paul f tompkins, and two documents: {"authors": ["Paul Tompkins", "Dietrich Kohl"]} and {"authors": ["Paul Wang", "Darlene Tompkins"]}.



      My search will retrieve both documents easily enough, but both will have the same score from the authors query. I'd like the fact that I matched multiple terms within the same item of the authors array to boost the score of the first document.



      How can I do that? The two techniques that I know of for boosting for proximity are shingles (which I believe would generate the paul_f and f_tompkins shingles, neither of which match) and a phrase query with slop (which would fail because the f token isn't there).



      Ideally I'd want something like a phrase slop query with minimum_should_match: I give it four words, it matches if at least two are present in the same array element, with each additional matching term in the same array element raising the score. I couldn't figure out how to do that.



      (It wouldn't work for me to have client-side logic that tries to strip the f out of the query -- this is a simplified example, but assume that I also want to be able handle a queries like paul francis tompkins or paul f tompkins there will be blood.)







      elasticsearch search full-text-search






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 19:33









      michaelmichael

      1,5682109




      1,5682109
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The reason both of the docs have same score is because the author field is array of text values. If we change the way we store authors we can get the desired result. To do so lets make authors as a nested type. So we have the following mapping:



          "mappings": {
          "_doc": {
          "properties": {
          "authors": {
          "type": "nested",
          "properties": {
          "name": {
          "type": "text",
          "fields": {
          "raw": {
          "type": "keyword"
          }
          }
          }
          }
          }
          }
          }
          }


          Note: Sub field raw can be used for some other scenarios and has no relation to the solution.



          Now lets index the docs as below:



          Doc 1:



          {
          "authors": [
          {
          "name": "Paul Tompkins"
          },
          {
          "name": "Dietrich Kohl"
          }
          ]
          }


          Doc 2:



          {
          "authors": [
          {
          "name": "Paul Wang"
          },
          {
          "name": "Darlene Tompkins"
          }
          ]
          }


          Lets query them as follows:



          {
          "explain": true,
          "query": {
          "nested": {
          "path": "authors",
          "query": {
          "query_string": {
          "query": "paul l tompkins",
          "fields": [
          "authors.name"
          ]
          }
          }
          }
          }
          }


          Result:



            "hits": {
          "total": 2,
          "max_score": 1.3862944,
          "hits": [
          {
          "_index": "test",
          "_type": "_doc",
          "_id": "1",
          "_score": 1.3862944,
          "_source": {
          "authors": [
          {
          "name": "Paul Tompkins"
          },
          {
          "name": "Dietrich Kohl"
          }
          ]
          }
          },
          {
          "_index": "test",
          "_type": "_doc",
          "_id": "2",
          "_score": 0.6931472,
          "_source": {
          "authors": [
          {
          "name": "Paul Wang"
          },
          {
          "name": "Darlene Tompkins"
          }
          ]
          }
          }
          ]
          }


          NOTE: In the query I have also used explain:true. This gives the explanation of the score calculation.(I have not included the explain output above as it is very long. You can try it though).



          When we look at the scoring mechanism, we can see the difference while querying on nested field and when querying on an array. Broadly speaking since nested fields are stored as separate docs, Doc 1 is scored higher because the child doc 1 i.e. :



          {
          "name": "Paul Tompkins"
          }


          will have higher score because both the terms paul and tompkins are in the same child doc.



          In case of array all the names belong same field and not as separate child docs and hence the difference.



          This way we can achieve the desired result.






          share|improve this answer

























            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437139%2fsearching-names-increasing-relevance-for-proximate-matches-in-multivalued-field%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The reason both of the docs have same score is because the author field is array of text values. If we change the way we store authors we can get the desired result. To do so lets make authors as a nested type. So we have the following mapping:



            "mappings": {
            "_doc": {
            "properties": {
            "authors": {
            "type": "nested",
            "properties": {
            "name": {
            "type": "text",
            "fields": {
            "raw": {
            "type": "keyword"
            }
            }
            }
            }
            }
            }
            }
            }


            Note: Sub field raw can be used for some other scenarios and has no relation to the solution.



            Now lets index the docs as below:



            Doc 1:



            {
            "authors": [
            {
            "name": "Paul Tompkins"
            },
            {
            "name": "Dietrich Kohl"
            }
            ]
            }


            Doc 2:



            {
            "authors": [
            {
            "name": "Paul Wang"
            },
            {
            "name": "Darlene Tompkins"
            }
            ]
            }


            Lets query them as follows:



            {
            "explain": true,
            "query": {
            "nested": {
            "path": "authors",
            "query": {
            "query_string": {
            "query": "paul l tompkins",
            "fields": [
            "authors.name"
            ]
            }
            }
            }
            }
            }


            Result:



              "hits": {
            "total": 2,
            "max_score": 1.3862944,
            "hits": [
            {
            "_index": "test",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.3862944,
            "_source": {
            "authors": [
            {
            "name": "Paul Tompkins"
            },
            {
            "name": "Dietrich Kohl"
            }
            ]
            }
            },
            {
            "_index": "test",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.6931472,
            "_source": {
            "authors": [
            {
            "name": "Paul Wang"
            },
            {
            "name": "Darlene Tompkins"
            }
            ]
            }
            }
            ]
            }


            NOTE: In the query I have also used explain:true. This gives the explanation of the score calculation.(I have not included the explain output above as it is very long. You can try it though).



            When we look at the scoring mechanism, we can see the difference while querying on nested field and when querying on an array. Broadly speaking since nested fields are stored as separate docs, Doc 1 is scored higher because the child doc 1 i.e. :



            {
            "name": "Paul Tompkins"
            }


            will have higher score because both the terms paul and tompkins are in the same child doc.



            In case of array all the names belong same field and not as separate child docs and hence the difference.



            This way we can achieve the desired result.






            share|improve this answer






























              0














              The reason both of the docs have same score is because the author field is array of text values. If we change the way we store authors we can get the desired result. To do so lets make authors as a nested type. So we have the following mapping:



              "mappings": {
              "_doc": {
              "properties": {
              "authors": {
              "type": "nested",
              "properties": {
              "name": {
              "type": "text",
              "fields": {
              "raw": {
              "type": "keyword"
              }
              }
              }
              }
              }
              }
              }
              }


              Note: Sub field raw can be used for some other scenarios and has no relation to the solution.



              Now lets index the docs as below:



              Doc 1:



              {
              "authors": [
              {
              "name": "Paul Tompkins"
              },
              {
              "name": "Dietrich Kohl"
              }
              ]
              }


              Doc 2:



              {
              "authors": [
              {
              "name": "Paul Wang"
              },
              {
              "name": "Darlene Tompkins"
              }
              ]
              }


              Lets query them as follows:



              {
              "explain": true,
              "query": {
              "nested": {
              "path": "authors",
              "query": {
              "query_string": {
              "query": "paul l tompkins",
              "fields": [
              "authors.name"
              ]
              }
              }
              }
              }
              }


              Result:



                "hits": {
              "total": 2,
              "max_score": 1.3862944,
              "hits": [
              {
              "_index": "test",
              "_type": "_doc",
              "_id": "1",
              "_score": 1.3862944,
              "_source": {
              "authors": [
              {
              "name": "Paul Tompkins"
              },
              {
              "name": "Dietrich Kohl"
              }
              ]
              }
              },
              {
              "_index": "test",
              "_type": "_doc",
              "_id": "2",
              "_score": 0.6931472,
              "_source": {
              "authors": [
              {
              "name": "Paul Wang"
              },
              {
              "name": "Darlene Tompkins"
              }
              ]
              }
              }
              ]
              }


              NOTE: In the query I have also used explain:true. This gives the explanation of the score calculation.(I have not included the explain output above as it is very long. You can try it though).



              When we look at the scoring mechanism, we can see the difference while querying on nested field and when querying on an array. Broadly speaking since nested fields are stored as separate docs, Doc 1 is scored higher because the child doc 1 i.e. :



              {
              "name": "Paul Tompkins"
              }


              will have higher score because both the terms paul and tompkins are in the same child doc.



              In case of array all the names belong same field and not as separate child docs and hence the difference.



              This way we can achieve the desired result.






              share|improve this answer




























                0












                0








                0







                The reason both of the docs have same score is because the author field is array of text values. If we change the way we store authors we can get the desired result. To do so lets make authors as a nested type. So we have the following mapping:



                "mappings": {
                "_doc": {
                "properties": {
                "authors": {
                "type": "nested",
                "properties": {
                "name": {
                "type": "text",
                "fields": {
                "raw": {
                "type": "keyword"
                }
                }
                }
                }
                }
                }
                }
                }


                Note: Sub field raw can be used for some other scenarios and has no relation to the solution.



                Now lets index the docs as below:



                Doc 1:



                {
                "authors": [
                {
                "name": "Paul Tompkins"
                },
                {
                "name": "Dietrich Kohl"
                }
                ]
                }


                Doc 2:



                {
                "authors": [
                {
                "name": "Paul Wang"
                },
                {
                "name": "Darlene Tompkins"
                }
                ]
                }


                Lets query them as follows:



                {
                "explain": true,
                "query": {
                "nested": {
                "path": "authors",
                "query": {
                "query_string": {
                "query": "paul l tompkins",
                "fields": [
                "authors.name"
                ]
                }
                }
                }
                }
                }


                Result:



                  "hits": {
                "total": 2,
                "max_score": 1.3862944,
                "hits": [
                {
                "_index": "test",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.3862944,
                "_source": {
                "authors": [
                {
                "name": "Paul Tompkins"
                },
                {
                "name": "Dietrich Kohl"
                }
                ]
                }
                },
                {
                "_index": "test",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.6931472,
                "_source": {
                "authors": [
                {
                "name": "Paul Wang"
                },
                {
                "name": "Darlene Tompkins"
                }
                ]
                }
                }
                ]
                }


                NOTE: In the query I have also used explain:true. This gives the explanation of the score calculation.(I have not included the explain output above as it is very long. You can try it though).



                When we look at the scoring mechanism, we can see the difference while querying on nested field and when querying on an array. Broadly speaking since nested fields are stored as separate docs, Doc 1 is scored higher because the child doc 1 i.e. :



                {
                "name": "Paul Tompkins"
                }


                will have higher score because both the terms paul and tompkins are in the same child doc.



                In case of array all the names belong same field and not as separate child docs and hence the difference.



                This way we can achieve the desired result.






                share|improve this answer















                The reason both of the docs have same score is because the author field is array of text values. If we change the way we store authors we can get the desired result. To do so lets make authors as a nested type. So we have the following mapping:



                "mappings": {
                "_doc": {
                "properties": {
                "authors": {
                "type": "nested",
                "properties": {
                "name": {
                "type": "text",
                "fields": {
                "raw": {
                "type": "keyword"
                }
                }
                }
                }
                }
                }
                }
                }


                Note: Sub field raw can be used for some other scenarios and has no relation to the solution.



                Now lets index the docs as below:



                Doc 1:



                {
                "authors": [
                {
                "name": "Paul Tompkins"
                },
                {
                "name": "Dietrich Kohl"
                }
                ]
                }


                Doc 2:



                {
                "authors": [
                {
                "name": "Paul Wang"
                },
                {
                "name": "Darlene Tompkins"
                }
                ]
                }


                Lets query them as follows:



                {
                "explain": true,
                "query": {
                "nested": {
                "path": "authors",
                "query": {
                "query_string": {
                "query": "paul l tompkins",
                "fields": [
                "authors.name"
                ]
                }
                }
                }
                }
                }


                Result:



                  "hits": {
                "total": 2,
                "max_score": 1.3862944,
                "hits": [
                {
                "_index": "test",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.3862944,
                "_source": {
                "authors": [
                {
                "name": "Paul Tompkins"
                },
                {
                "name": "Dietrich Kohl"
                }
                ]
                }
                },
                {
                "_index": "test",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.6931472,
                "_source": {
                "authors": [
                {
                "name": "Paul Wang"
                },
                {
                "name": "Darlene Tompkins"
                }
                ]
                }
                }
                ]
                }


                NOTE: In the query I have also used explain:true. This gives the explanation of the score calculation.(I have not included the explain output above as it is very long. You can try it though).



                When we look at the scoring mechanism, we can see the difference while querying on nested field and when querying on an array. Broadly speaking since nested fields are stored as separate docs, Doc 1 is scored higher because the child doc 1 i.e. :



                {
                "name": "Paul Tompkins"
                }


                will have higher score because both the terms paul and tompkins are in the same child doc.



                In case of array all the names belong same field and not as separate child docs and hence the difference.



                This way we can achieve the desired result.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 '18 at 6:23

























                answered Nov 23 '18 at 6:14









                Nishant SainiNishant Saini

                1,152818




                1,152818






























                    draft saved

                    draft discarded




















































                    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%2f53437139%2fsearching-names-increasing-relevance-for-proximate-matches-in-multivalued-field%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python