4

i have an index of data in ElasticSearch

{
  "name":"john",
  "type":"main",
  "address":"26, pression road, next to maha maul, Atlanta, USA",
  "description":"i am from atlanta. i a need of help on elastic search. my name is john wosniak"
},
{
  "name":"shailesh",
  "type":"other",
  "address":"c-401, greenfield road, next to cyber city, mumbai, india",
  "description":"i am from mumbai. i dont need. my name shailesh"
},
{
  "name":"pratik",
  "type":"main",
  "address":"c-116, parmar society, john main road, andhari, India",
  "description":"i am from andhari. i a need of help on elastic search. my name is pratik doshi, i am good friend of john"
},...

i want to search using some advance query such that it searches "type":"main" and "john" in rest of all the fields.

for now i am using the query

{
    "query_string" : {
        "fields" : ["name", "type","address", "description"],
        "query" : "main john*"
    }
}

it is not giving me desired result as per my requirement.

hope i have explained clearly. thanks for the help in advance.

1 Answer 1

1

Did you look at bool query?

You can achieve desired results with boosting

 {
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "type": {
              "query": "main",
              "boost": 10
            }
          }
        },
        {
          "multi_match": {
            "query": "john",
            "fields": [
              "name",
              "address",
              "description"
            ]
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

your answer works fine for one of my requirement. just a quick run-up. i could not find/make a suitable solution by Boolean query for a different type of requirement. i needed all the results having "type": "main" at top and rest all below it based on "multi_match": { "query": "john",. can you also help me with that.
do you want both conditions to be true or just any one of the two?basically your requirement is AND or OR?
OR Condition but "type":"main" results should be on top
Is there any way to boost certain string and get those many results in top of the search. In the above example i take all the results where i find john and those many johns who are having type: main comes on the top of the result and rest below it

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.