Home > Blockchain >  I want to display only one record in Elasticsearch by searching
I want to display only one record in Elasticsearch by searching

Time:12-26

With the following query :

GET feeds/_search
{
  "query": {
    "nested": {
      "path": "comment",
      "query": {
        "multi_match": {
          "query": "ali",
          "fields": ["body","title","comment.c_text"]
        }
      }
    }
  }
}

I am getting the result

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.8025915,
    "hits" : [
      {
        "_index" : "feeds",
        "_type" : "_doc",
        "_id" : "NeoTNIUB1Qg_7rFp-8RV",
        "_score" : 0.8025915,
        "_source" : {
          "feed_id" : "1",
          "title" : "Mateen",
          "body" : "This is mateen",
          "comment" : [
            {
              "c_id" : "11",
              "feed_id" : "1",
              "c_text" : "get well soon mateen"
            },
            {
              "c_id" : "12",
              "feed_id" : "1",
              "c_text" : " Ali here"
            }
          ]
        }
      }
    ]
  }
}

I don't want to see my first record as it does not contain "ali" in body,title,comment_text. Can anyone help? I want that if it is searching "ali" so it will return the feed id title body as well as comment id feed id and c_text but don't show me the content of array that does not contain ali hide it

{
  "c_id" : "11",
  "feed_id" : "1",
  "c_text" : "get well soon mateen"
}

i dont want to see this content in my out put

CodePudding user response:

First, update your mapping like this:

POST feeds/_mapping
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "lowercase"
    },
    "body": {
      "type": "text",
      "analyzer": "lowercase"
    },
    "comment": {
      "type": "nested",
      "properties": {
        "c_text": {
          "type": "text",
          "analyzer": "lowercase"
        }
      }
    }
  }
}

Then run this query

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": {
              "query": "ali",
              "analyzer": "lowercase"
            }
          }
        },
        {
          "match": {
            "body": {
              "query": "ali",
              "analyzer": "lowercase"
            }
          }
        },
        {
          "nested": {
            "path": "comment",
            "query": {
              "match": {
                "comment.c_text": {
                  "query": "ali",
                  "analyzer": "lowercase"
                }
              }
            }
          }
        }
      ]
    }
  }
}

CodePudding user response:

    GET feeds/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "comment",
            "query": { 
              "match": {
                "comment.c_text": "hey"
              }
            },"inner_hits": {
              "highlight": {
                "require_field_match": "true"
                
              }
            }
          }
        },
        {
          "term": {
            "title": {
              "value": "hey"
            }
          }
        },
        {
          "term": {
            "body": {
              "value": "hey"
            }
          }
        }
      ]
    }
  }
}

in this way we can get results through inner hits

  • Related