0

new to ElasticSearch - start loving it. I am working on a Rails application (using elasticsearch-rails / elasticsearch-model).

I have two fields - both strings consisting of Tags.

about_me & about_you

  • Now I was to query the about_you of another user with the current users about_me.
  • At the same time, I wish to query the about_me of the other users with the about_you of the current user.

Does this make sense? Like two fields, two queries and each query is aimed at a particular field.

I just need a hint how this can be achieved in ES. For the sake of completeness, here is the part method I created in my rails model - it is incomplete:

def home_search(query_you, query_me)
  search_definition =
      {
          query: {
              multi_match: {
                  query: query_me,
                  fields: ['about_you']
              }
              ..... SOMETHINGs MISSING HERE ..... ?
          },
          suggest: {
              text: query,
              about_me: {
                  term: {
                      size: 1,
                      field: :about_me
                  }
              },
              about_you: {
                  term: {
                      size: 1,
                      field: :about_you
                  }
              }
          }
      }
  self.class.__elasticsearch__.search(search_definition)
end

Any help, link or donations are welcome. Thank you!

0

1 Answer 1

1

I'm not sure I've understood your question but I can suggest two options:
First Use a bool query of type should and minimum_should_match=1. In this case you can write two queries for you'r searches. and If you want to distinguish between results you can pass a _name parameter in each query. something like this:

{
   "query": {
      "bool": {
         "minimum_should_match": 1,
         "should": [
            {
               "multi_match": {
                  "query": "query_me",
                  "fields": [
                     "about_you"
                  ],
                  "_name": "about_you"
               }
            },
            {
               "multi_match": {
                  "query": "query_you",
                  "fields": [
                     "about_me"
                  ],
                  "_name": "about_you"
               }
            }
         ]
      }
   }
}

By providing _name you can see which queries are hitted in your search result. The second approach could be a _msearch query which in which you can pass multiple queries to the endpoint and get the results back. Here are some useful links:

Bool Query
Named Queries

Sign up to request clarification or add additional context in comments.

1 Comment

Precisely what I was looking for. Yes I was reading up on bool queries last night. A lot of into to take in - but promised myself to master it this week! Thank you for your input - very helpful!

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.